rails/rails — pull request #16297 by calebthompson (2014-07-25 19:25:53+00:00)
Title: Extract iterator method in AR::SchemaDumper

Gems which wish to tie into ActiveRecord::SchemaDumper need to duplicate this
logic currently. [Foreigner] is one such example, as is a library I'm
currently working on but which hasn't been released yet:

```ruby
def tables_with_foreign_keys(stream)
tables_without_foreign_keys(stream)
@connection.tables.sort.each do |table|
next if ['schema_migrations', ignore_tables].flatten.any? do |ignored|
case ignored
when String; table == ignored
when Regexp; table =~ ignored
else
raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
end
end
foreign_keys(table, stream)
end
end
```

[Foreigner]: https://github.com/matthuhiggins/foreigner/blob/master/lib/foreigner/schema_dumper.rb#L36-L43

Extract the skip logic to a method, making it much simpler to follow this same
behavior in gems that are tying into the migration flow and let them dump only
tables that aren't skipped without copying this block of code. The above code
could then be simplified to:

```rails
def tables_with_foreign_keys(stream)
tables_without_foreign_keys(stream)
each_not_ignored(@connection.tables.sort) do
foreign_keys(table, stream)
end
end
```

This _could_ be further simplified to assume tables, but for my use case I'm
trying to add views which wouldn't be returned by the [`tables`] method.

[`tables`]: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L95-L102