Permalink
Browse files

fix habtm/:join_table bug, closes #6

fix an issue introduced in matthuhiggins/foreigner@f29d0f1 ... :join_table
could be a symbol, which causes remove_prefix_and_suffix to blow up.

table names from other associations do not have this issue, as they are
always strings. regardless, this commit adds better test coverage so that
we know if any to_ruby call breaks.
  • Loading branch information...
jenseng committed Dec 14, 2012
1 parent c752dab commit e2cd5b178ee7f2e59a3bac56c9531eec2aed76b5
Showing with 67 additions and 28 deletions.
  1. +2 −2 immigrant.gemspec
  2. +2 −2 lib/immigrant.rb
  3. +63 −24 test/immigrant_test.rb
View
@@ -2,7 +2,7 @@
Gem::Specification.new do |s|
s.name = 'immigrant'
- s.version = '0.1.3'
+ s.version = '0.1.4'
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.6')
+ s.add_dependency('foreigner', '>= 1.2.1')
end
View
@@ -106,13 +106,13 @@ def foreign_keys_for(klass)
when :has_and_belongs_to_many
[
Foreigner::ConnectionAdapters::ForeignKeyDefinition.new(
- reflection.options[:join_table], klass.table_name,
+ reflection.options[:join_table].to_s, klass.table_name,
:column => reflection.send(fk_method).to_s,
:primary_key => klass.primary_key.to_s,
:dependent => nil
),
Foreigner::ConnectionAdapters::ForeignKeyDefinition.new(
- reflection.options[:join_table], reflection.klass.table_name,
+ reflection.options[:join_table].to_s, reflection.klass.table_name,
:column => reflection.association_foreign_key.to_s,
:primary_key => reflection.klass.primary_key.to_s,
:dependent => nil
View
@@ -41,12 +41,14 @@ class Book < MockModel
belongs_to :guy, :class_name => 'Author', :foreign_key => 'author_id'
end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -56,12 +58,14 @@ class Author < MockModel
end
class Book < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -71,12 +75,14 @@ class Author < MockModel
end
class Book < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => :delete
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -86,12 +92,14 @@ class Author < MockModel
end
class Book < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -101,12 +109,14 @@ class Author < MockModel
end
class Book < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => :delete
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -116,6 +126,8 @@ class Author < MockModel
end
class Fan < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Fan]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'authors_fans', 'authors',
@@ -125,7 +137,28 @@ class Fan < MockModel; end
'authors_fans', 'fans',
:column => 'fan_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Fan]).first
+ keys
+ )
+ end
+
+ test 'has_and_belongs_to_many should respect the join_table' do
+ class Author < MockModel
+ has_and_belongs_to_many :fans, :join_table => :lol_wuts
+ end
+ class Fan < MockModel; end
+
+ keys = Immigrant.infer_keys([], [Author, Fan]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
+ assert_equal(
+ [foreign_key_definition(
+ 'lol_wuts', 'authors',
+ :column => 'author_id', :primary_key => 'id', :dependent => nil
+ ),
+ foreign_key_definition(
+ 'lol_wuts', 'fans',
+ :column => 'fan_id', :primary_key => 'id', :dependent => nil
+ )],
+ keys
)
end
@@ -138,6 +171,8 @@ class Author < MockModel
class Book < MockModel; end
class Article < MockModel; end
+ keys = Immigrant.infer_keys([], [Article, Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'articles', 'authors',
@@ -147,7 +182,7 @@ class Article < MockModel; end
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Article, Author, Book]).first
+ keys
)
end
@@ -162,12 +197,14 @@ class Employee < MockModel
class Manager < Employee; end
assert(Manager.reflections.present?)
+ keys = Immigrant.infer_keys([], [Company, Employee, Manager]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'employees', 'companies',
:column => 'company_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Company, Employee, Manager]).first
+ keys
)
end
@@ -179,12 +216,14 @@ class Book < MockModel
belongs_to :author
end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -196,12 +235,14 @@ class Author < MockModel
end
class Book < MockModel; end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
@@ -229,10 +270,8 @@ class Author < MockModel
class Book < MockModel; end
class Article < MockModel; end
- assert_equal(
- [],
- Immigrant.infer_keys(database_keys, [Article, Author, Book]).first
- )
+ keys = Immigrant.infer_keys(database_keys, [Article, Author, Book]).first
+ assert_equal([], keys)
end
test 'finder_sql associations should not generate foreign keys' do
@@ -246,10 +285,8 @@ class Author < MockModel
end
class Book < MockModel; end
- assert_equal(
- [],
- Immigrant.infer_keys([], [Author, Book]).first
- )
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_equal([], keys)
end
test 'polymorphic associations should not generate foreign keys' do
@@ -263,10 +300,8 @@ class Corporation < MockModel
has_many :properties, :as => :owner
end
- assert_equal(
- [],
- Immigrant.infer_keys([], [Corporation, Person, Property]).first
- )
+ keys = Immigrant.infer_keys([], [Corporation, Person, Property]).first
+ assert_equal([], keys)
end
test 'has_many :through should not generate foreign keys' do
@@ -283,6 +318,8 @@ class Fan < MockModel
has_many :authors, :through => :authors_fans
end
+ keys = Immigrant.infer_keys([], [Author, AuthorsFan, Fan]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'authors_fans', 'authors',
@@ -292,7 +329,7 @@ class Fan < MockModel
'authors_fans', 'fans',
:column => 'fan_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, AuthorsFan, Fan]).first
+ keys
)
end
@@ -303,12 +340,14 @@ class Book < MockModel
belongs_to :invalid
end
+ keys = Immigrant.infer_keys([], [Author, Book]).first
+ assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :dependent => nil
)],
- Immigrant.infer_keys([], [Author, Book]).first
+ keys
)
end
end

0 comments on commit e2cd5b1

Please sign in to comment.