diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 4fea7d64..15ccd7f6 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -72,6 +72,11 @@ module Shoulda # :nodoc: @order = order self end + + def conditions(conditions) + @conditions = conditions + self + end def matches?(subject) @subject = subject @@ -81,6 +86,7 @@ module Shoulda # :nodoc: through_association_valid? && dependent_correct? && order_correct? && + conditions_correct? && join_table_exists? end @@ -175,6 +181,15 @@ module Shoulda # :nodoc: end end + def conditions_correct? + if @conditions.nil? || @conditions.to_s == reflection.options[:conditions].to_s + true + else + @missing = "#{@name} should have the following conditions: #{@conditions}" + false + end + end + def join_table_exists? if @macro != :has_and_belongs_to_many || ::ActiveRecord::Base.connection.tables.include?(join_table.to_s) diff --git a/spec/shoulda/active_record/association_matcher_spec.rb b/spec/shoulda/active_record/association_matcher_spec.rb index d91ae5a1..ca3ed6ff 100644 --- a/spec/shoulda/active_record/association_matcher_spec.rb +++ b/spec/shoulda/active_record/association_matcher_spec.rb @@ -67,6 +67,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do end Child.new.should_not @matcher.dependent(:destroy) end + + it "should accept an association with a valid :conditions option" do + define_model :parent, :adopter => :boolean + define_model :child, :parent_id => :integer do + belongs_to :parent, :conditions => { :adopter => true } + end + Child.new.should @matcher.conditions(:adopter => true) + end + + it "should reject an association with a bad :conditions option" do + define_model :parent, :adopter => :boolean + define_model :child, :parent_id => :integer do + belongs_to :parent + end + Child.new.should_not @matcher.conditions(:adopter => true) + end end context "have_many" do @@ -177,6 +193,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Parent.new.should_not @matcher.order(:id) end + it "should accept an association with a valid :conditions option" do + define_model :child, :parent_id => :integer, :adopted => :boolean + define_model :parent do + has_many :children, :conditions => { :adopted => true } + end + Parent.new.should @matcher.conditions({ :adopted => true }) + end + + it "should reject an association with a bad :conditions option" do + define_model :child, :parent_id => :integer, :adopted => :boolean + define_model :parent do + has_many :children + end + Parent.new.should_not @matcher.conditions({ :adopted => true }) + end + end context "have_one" do @@ -250,6 +282,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do Person.new.should_not @matcher.order(:id) end + it "should accept an association with a valid :conditions option" do + define_model :detail, :person_id => :integer, :disabled => :boolean + define_model :person do + has_one :detail, :conditions => { :disabled => true} + end + Person.new.should @matcher.conditions(:disabled => true) + end + + it "should reject an association with a bad :conditions option" do + define_model :detail, :person_id => :integer, :disabled => :boolean + define_model :person do + has_one :detail + end + Person.new.should_not @matcher.conditions(:disabled => true) + end + end context "have_and_belong_to_many" do @@ -290,6 +338,27 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do end Person.new.should_not @matcher end + + it "should accept an association with a valid :conditions option" do + define_model :relatives, :adopted => :boolean + define_model :person do + has_and_belongs_to_many :relatives, :conditions => { :adopted => true } + end + define_model :people_relative, :person_id => :integer, + :relative_id => :integer + Person.new.should @matcher.conditions(:adopted => true) + end + + it "should reject an association with a bad :conditions option" do + define_model :relatives, :adopted => :boolean + define_model :person do + has_and_belongs_to_many :relatives + end + define_model :people_relative, :person_id => :integer, + :relative_id => :integer + Person.new.should_not @matcher.conditions(:adopted => true) + end + end end