Added support for testing the :conditions option on associations
This commit is contained in:
parent
3d9439030b
commit
0820901e4b
|
@ -72,6 +72,11 @@ module Shoulda # :nodoc:
|
||||||
@order = order
|
@order = order
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def conditions(conditions)
|
||||||
|
@conditions = conditions
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
def matches?(subject)
|
def matches?(subject)
|
||||||
@subject = subject
|
@subject = subject
|
||||||
|
@ -81,6 +86,7 @@ module Shoulda # :nodoc:
|
||||||
through_association_valid? &&
|
through_association_valid? &&
|
||||||
dependent_correct? &&
|
dependent_correct? &&
|
||||||
order_correct? &&
|
order_correct? &&
|
||||||
|
conditions_correct? &&
|
||||||
join_table_exists?
|
join_table_exists?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -175,6 +181,15 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
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?
|
def join_table_exists?
|
||||||
if @macro != :has_and_belongs_to_many ||
|
if @macro != :has_and_belongs_to_many ||
|
||||||
::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
|
::ActiveRecord::Base.connection.tables.include?(join_table.to_s)
|
||||||
|
|
|
@ -67,6 +67,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
end
|
end
|
||||||
Child.new.should_not @matcher.dependent(:destroy)
|
Child.new.should_not @matcher.dependent(:destroy)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "have_many" do
|
context "have_many" do
|
||||||
|
@ -177,6 +193,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
Parent.new.should_not @matcher.order(:id)
|
Parent.new.should_not @matcher.order(:id)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "have_one" do
|
context "have_one" do
|
||||||
|
@ -250,6 +282,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
Person.new.should_not @matcher.order(:id)
|
Person.new.should_not @matcher.order(:id)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
context "have_and_belong_to_many" do
|
context "have_and_belong_to_many" do
|
||||||
|
@ -290,6 +338,27 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
end
|
end
|
||||||
Person.new.should_not @matcher
|
Person.new.should_not @matcher
|
||||||
end
|
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
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue