Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Rails 4.0 supports adding indexes during create_table. We shall do th…

…e same for foreign_keys.
  • Loading branch information...
commit de94e96b72c1421a2d6714251a4a15f97c8c7480 1 parent 54bd87f
@matthuhiggins authored
View
16 lib/foreigner/connection_adapters/abstract/schema_definitions.rb
@@ -5,9 +5,23 @@ def self.included(base)
base::Table.class_eval do
include Foreigner::ConnectionAdapters::Table
end
+
+ base::TableDefinition.class_eval do
+ include Foreigner::ConnectionAdapters::TableDefinition
+ end
end
end
-
+
+ module TableDefinition
+ def foreign_key(to_table, options = {})
+ foreign_keys[to_table] = options
+ end
+
+ def foreign_keys
+ @foreign_keys ||= {}
+ end
+ end
+
module Table
extend ActiveSupport::Concern
View
6 lib/foreigner/connection_adapters/abstract/schema_statements.rb
@@ -9,6 +9,12 @@ def self.included(base)
end
module AbstractAdapter
+ def create_table(table_name, *args, &block)
+ definition = nil
+ super { |td| definition = td } # This is my trick to get the definition
+ td.foreign_keys.each { |c,o| add_foreign_key table_name, c, o }
+ end
+
def supports_foreign_keys?
false
end
View
0  test/foreigner/connection_adapters/abstract/schema_definitions_test.rb
No changes.
Please sign in to comment.
Something went wrong with that request. Please try again.