Skip to content

Commit

Permalink
Require the classes match to consider an association a reciprocal (Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Jun 3, 2009
1 parent 2bb56bd commit c5f2dc1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
7 changes: 4 additions & 3 deletions lib/sequel/model/associations.rb
Expand Up @@ -103,7 +103,7 @@ def reciprocal
r_type = reciprocal_type
key = self[:key]
associated_class.all_association_reflections.each do |assoc_reflect|
if assoc_reflect[:type] == r_type && assoc_reflect[:key] == key
if assoc_reflect[:type] == r_type && assoc_reflect[:key] == key && assoc_reflect.associated_class == self[:model]
return self[:reciprocal] = assoc_reflect[:name]
end
end
Expand Down Expand Up @@ -290,8 +290,9 @@ def reciprocal
right_key = self[:right_key]
join_table = self[:join_table]
associated_class.all_association_reflections.each do |assoc_reflect|
if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_key] == right_key \
&& assoc_reflect[:right_key] == left_key && assoc_reflect[:join_table] == join_table
if assoc_reflect[:type] == :many_to_many && assoc_reflect[:left_key] == right_key &&
assoc_reflect[:right_key] == left_key && assoc_reflect[:join_table] == join_table &&
assoc_reflect.associated_class == self[:model]
return self[:reciprocal] = assoc_reflect[:name]
end
end
Expand Down
32 changes: 29 additions & 3 deletions spec/model/association_reflection_spec.rb
Expand Up @@ -37,6 +37,17 @@ class ::ParParent < Sequel::Model; end
end

describe Sequel::Model::Associations::AssociationReflection, "#reciprocal" do
before do
class ::ParParent < Sequel::Model; end
class ::ParParentTwo < Sequel::Model; end
class ::ParParentThree < Sequel::Model; end
end
after do
Object.send(:remove_const, :ParParent)
Object.send(:remove_const, :ParParentTwo)
Object.send(:remove_const, :ParParentThree)
end

it "should use the :reciprocal value if present" do
@c = Class.new(Sequel::Model)
@d = Class.new(Sequel::Model)
Expand All @@ -45,10 +56,25 @@ class ::ParParent < Sequel::Model; end
@c.association_reflection(:c).reciprocal.should == :xx
end

it "should require the associated class is the current class to be a reciprocal" do
ParParent.many_to_one :par_parent_two, :key=>:blah
ParParent.many_to_one :par_parent_three, :key=>:blah
ParParentTwo.one_to_many :par_parents, :key=>:blah
ParParentThree.one_to_many :par_parents, :key=>:blah

ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_two
ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_three

ParParent.many_to_many :par_parent_twos, :left_key=>:l, :right_key=>:r, :join_table=>:jt
ParParent.many_to_many :par_parent_threes, :left_key=>:l, :right_key=>:r, :join_table=>:jt
ParParentTwo.many_to_many :par_parents, :right_key=>:l, :left_key=>:r, :join_table=>:jt
ParParentThree.many_to_many :par_parents, :right_key=>:l, :left_key=>:r, :join_table=>:jt

ParParentTwo.association_reflection(:par_parents).reciprocal.should == :par_parent_twos
ParParentThree.association_reflection(:par_parents).reciprocal.should == :par_parent_threes
end

it "should figure out the reciprocal if the :reciprocal value is not present" do
class ::ParParent < Sequel::Model; end
class ::ParParentTwo < Sequel::Model; end
class ::ParParentThree < Sequel::Model; end
ParParent.many_to_one :par_parent_two
ParParentTwo.one_to_many :par_parents
ParParent.many_to_many :par_parent_threes
Expand Down

0 comments on commit c5f2dc1

Please sign in to comment.