Permalink
Browse files

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

…e same for foreign_keys.
  • Loading branch information...
1 parent 54bd87f commit de94e96b72c1421a2d6714251a4a15f97c8c7480 @matthuhiggins committed Dec 3, 2012
@@ -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
@@ -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

0 comments on commit de94e96

Please sign in to comment.