Permalink
Browse files

association_matcher foreign_key refinement

#66

has_many and has_one tests failed with the reverse association has a
nonstandard name, (eg "belongs_to :author, :class_name => :User)

corrected by having have_one and have_many association tests check for an
:inverse_of on their association, and use the inverse relationship's
foreign key info if it can be found
  • Loading branch information...
1 parent 963a293 commit c863f7e6f00103b95a5be8234a2db9eb9bfc0f4d @bloy bloy committed Mar 4, 2012
Showing with 9 additions and 1 deletion.
  1. +9 −1 lib/shoulda/matchers/active_record/association_matcher.rb
View
10 lib/shoulda/matchers/active_record/association_matcher.rb
@@ -240,7 +240,15 @@ def associated_class
end
def foreign_key
- reflection.respond_to?(:foreign_key) ? reflection.foreign_key : reflection.primary_key_name
+ fk_reflection = reflection
+ if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of)
+ fk_reflection = associated_class.reflect_on_association(
+ reflection.options[:inverse_of]
+ )
+ end
+ fk_reflection.respond_to?(:foreign_key) ?
+ fk_reflection.foreign_key :
+ fk_reflection.primary_key_name
end
def through?

0 comments on commit c863f7e

Please sign in to comment.