Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for testing the :conditions option on associations

  • Loading branch information...
commit 0820901e4ba02592777aa1beb73fb338178ba915 1 parent 3d94390
@Hakon Hakon authored
View
15 lib/shoulda/matchers/active_record/association_matcher.rb
@@ -72,6 +72,11 @@ def order(order)
@order = order
self
end
+
+ def conditions(conditions)
+ @conditions = conditions
+ self
+ end
def matches?(subject)
@subject = subject
@@ -81,6 +86,7 @@ def matches?(subject)
through_association_valid? &&
dependent_correct? &&
order_correct? &&
+ conditions_correct? &&
join_table_exists?
end
@@ -175,6 +181,15 @@ def order_correct?
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)
View
69 spec/shoulda/active_record/association_matcher_spec.rb
@@ -67,6 +67,22 @@
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 @@
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 @@
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 @@
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
Please sign in to comment.
Something went wrong with that request. Please try again.