Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
test and bugfix for failing case
  • Loading branch information
bloy committed Mar 23, 2012
1 parent c863f7e commit 5c173bf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -246,9 +246,13 @@ def foreign_key
reflection.options[:inverse_of]
)
end
fk_reflection.respond_to?(:foreign_key) ?
fk_reflection.foreign_key :
fk_reflection.primary_key_name
if fk_reflection
fk_reflection.respond_to?(:foreign_key) ?
fk_reflection.foreign_key :
fk_reflection.primary_key_name
else
nil
end
end

def through?
Expand Down
19 changes: 19 additions & 0 deletions spec/shoulda/active_record/association_matcher_spec.rb
Expand Up @@ -240,6 +240,25 @@
Parent.new.should_not @matcher.class_name('Node')
end

it "should accept an association with a nonstandard reverse foreign key, using :inverse_of" do
define_model :child, :ancestor_id => :integer, :adopted => :boolean do
belongs_to :ancestor, :inverse_of => :children, :class_name => :Parent
end
define_model :parent do
has_many :children, :inverse_of => :ancestor
end
Parent.new.should @matcher
end

it "should reject an association with a nonstandard reverse foreign key, if :inverse_of is not correct" do
define_model :child, :mother_id => :integer, :adopted => :boolean do
belongs_to :mother, :inverse_of => :children, :class_name => :Parent
end
define_model :parent do
has_many :children, :inverse_of => :ancestor
end
Parent.new.should_not @matcher
end
end

context "have_one" do
Expand Down

0 comments on commit 5c173bf

Please sign in to comment.