Browse files

DRY this up since Foreigner now exposes a method for it

  • Loading branch information...
1 parent bc11c0f commit 03d0fd587a183bd65b43220f6455433f2585e2f2 @jenseng committed May 28, 2012
Showing with 6 additions and 18 deletions.
  1. +2 −2 immigrant.gemspec
  2. +4 −16 lib/immigrant/foreign_key_definition.rb
View
4 immigrant.gemspec
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = 'immigrant'
- s.version = '0.1.1'
+ s.version = '0.1.2'
s.summary = 'Migration generator for Foreigner'
s.description = 'Adds a generator for creating a foreign key migration based on your current model associations'
@@ -16,5 +16,5 @@ Gem::Specification.new do |s|
s.extra_rdoc_files = %w(README.rdoc)
s.files = %w(LICENSE.txt Rakefile README.rdoc lib/generators/USAGE) + Dir['lib/**/*.rb'] + Dir['test/**/*.rb']
s.add_dependency('activerecord', '>= 3.0')
- s.add_dependency('foreigner', '>= 1.1.3')
+ s.add_dependency('foreigner', '>= 1.1.6')
end
View
20 lib/immigrant/foreign_key_definition.rb
@@ -1,7 +1,9 @@
module Immigrant
# add some useful stuff to foreigner's ForeignKeyDefinition
- # TODO: get it in foreigner so we don't need to monkey patch
+ # TODO: get more of this into foreigner so we don't need to monkey patch
module ForeignKeyDefinition
+ include Foreigner::SchemaDumper::ClassMethods
+
def initialize(from_table, to_table, options, *args)
options ||= {}
options[:name] ||= "#{from_table}_#{options[:column]}_fk"
@@ -14,21 +16,7 @@ def hash_key
def to_ruby(action = :add)
if action == :add
- # not DRY ... guts of this are copied from Foreigner :(
- parts = [ ('add_foreign_key ' + from_table.inspect) ]
- parts << to_table.inspect
- parts << (':name => ' + options[:name].inspect)
-
- if options[:column] != "#{to_table.singularize}_id"
- parts << (':column => ' + options[:column].inspect)
- end
- if options[:primary_key] != 'id'
- parts << (':primary_key => ' + options[:primary_key].inspect)
- end
- if options[:dependent].present?
- parts << (':dependent => ' + options[:dependent].inspect)
- end
- parts.join(', ')
+ dump_foreign_key(self)
else
"remove_foreign_key #{from_table.inspect}, " \
":name => #{options[:name].inspect}"

0 comments on commit 03d0fd5

Please sign in to comment.