Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-burns committed Oct 14, 2011
2 parents ea477b8 + 0820901 commit 293b8aa
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -72,6 +72,11 @@ def order(order)
@order = order
self
end

def conditions(conditions)
@conditions = conditions
self
end

def matches?(subject)
@subject = subject
Expand All @@ -81,6 +86,7 @@ def matches?(subject)
through_association_valid? &&
dependent_correct? &&
order_correct? &&
conditions_correct? &&
join_table_exists?
end

Expand Down Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions spec/shoulda/active_record/association_matcher_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 293b8aa

Please sign in to comment.