Added class_name option matcher to association_matcher
This commit is contained in:
parent
ead1e1d42c
commit
17ac7d5a36
|
@ -18,6 +18,7 @@ module Shoulda # :nodoc:
|
|||
# * <tt>through</tt> - association name for <tt>has_many :through</tt>
|
||||
# * <tt>dependent</tt> - tests that the association makes use of the
|
||||
# dependent option.
|
||||
# * <tt>:class_name</tt> - tests that the association makes use of the class_name option.
|
||||
#
|
||||
# Example:
|
||||
# it { should have_many(:friends) }
|
||||
|
@ -35,6 +36,7 @@ module Shoulda # :nodoc:
|
|||
# Options:
|
||||
# * <tt>:dependent</tt> - tests that the association makes use of the
|
||||
# dependent option.
|
||||
# * <tt>:class_name</tt> - tests that the association makes use of the class_name option.
|
||||
#
|
||||
# Example:
|
||||
# it { should have_one(:god) } # unless hindu
|
||||
|
@ -78,6 +80,11 @@ module Shoulda # :nodoc:
|
|||
self
|
||||
end
|
||||
|
||||
def class_name(class_name)
|
||||
@class_name = class_name
|
||||
self
|
||||
end
|
||||
|
||||
def matches?(subject)
|
||||
@subject = subject
|
||||
association_exists? &&
|
||||
|
@ -85,6 +92,7 @@ module Shoulda # :nodoc:
|
|||
foreign_key_exists? &&
|
||||
through_association_valid? &&
|
||||
dependent_correct? &&
|
||||
class_name_correct? &&
|
||||
order_correct? &&
|
||||
conditions_correct? &&
|
||||
join_table_exists?
|
||||
|
@ -102,6 +110,7 @@ module Shoulda # :nodoc:
|
|||
description = "#{macro_description} #{@name}"
|
||||
description += " through #{@through}" if @through
|
||||
description += " dependent => #{@dependent}" if @dependent
|
||||
description += " class_name => #{@class_name}" if @class_name
|
||||
description += " order => #{@order}" if @order
|
||||
description
|
||||
end
|
||||
|
@ -172,6 +181,15 @@ module Shoulda # :nodoc:
|
|||
end
|
||||
end
|
||||
|
||||
def class_name_correct?
|
||||
if @class_name.nil? || @class_name.to_s == reflection.options[:class_name].to_s
|
||||
true
|
||||
else
|
||||
@missing = "#{@name} should have #{@class_name} as class_name"
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
def order_correct?
|
||||
if @order.nil? || @order.to_s == reflection.options[:order].to_s
|
||||
true
|
||||
|
|
|
@ -83,6 +83,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|||
end
|
||||
Child.new.should_not @matcher.conditions(:adopter => true)
|
||||
end
|
||||
|
||||
it "should accept an association with a valid :class_name option" do
|
||||
define_model :tree_parent, :adopter => :boolean
|
||||
define_model :child, :parent_id => :integer do
|
||||
belongs_to :parent, :class_name => 'TreeParent'
|
||||
end
|
||||
Child.new.should @matcher.class_name('TreeParent')
|
||||
end
|
||||
|
||||
it "should reject an association with a bad :class_name option" do
|
||||
define_model :parent, :adopter => :boolean
|
||||
define_model :child, :parent_id => :integer do
|
||||
belongs_to :parent
|
||||
end
|
||||
Child.new.should_not @matcher.class_name('TreeChild')
|
||||
end
|
||||
end
|
||||
|
||||
context "have_many" do
|
||||
|
@ -209,6 +225,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|||
Parent.new.should_not @matcher.conditions({ :adopted => true })
|
||||
end
|
||||
|
||||
it "should accept an association with a valid :class_name option" do
|
||||
define_model :node, :parent_id => :integer, :adopted => :boolean
|
||||
define_model :parent do
|
||||
has_many :children, :class_name => 'Node'
|
||||
end
|
||||
Parent.new.should @matcher.class_name('Node')
|
||||
end
|
||||
|
||||
it "should reject an association with a bad :class_name option" do
|
||||
define_model :child, :parent_id => :integer, :adopted => :boolean
|
||||
define_model :parent do
|
||||
has_many :children
|
||||
end
|
||||
Parent.new.should_not @matcher.class_name('Node')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "have_one" do
|
||||
|
@ -298,6 +330,22 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|||
Person.new.should_not @matcher.conditions(:disabled => true)
|
||||
end
|
||||
|
||||
it "should accept an association with a valid :class_name option" do
|
||||
define_model :person_detail, :person_id => :integer, :disabled => :boolean
|
||||
define_model :person do
|
||||
has_one :detail, :class_name => 'PersonDetail'
|
||||
end
|
||||
Person.new.should @matcher.class_name('PersonDetail')
|
||||
end
|
||||
|
||||
it "should reject an association with a bad :class_name option" do
|
||||
define_model :detail, :person_id => :integer, :disabled => :boolean
|
||||
define_model :person do
|
||||
has_one :detail
|
||||
end
|
||||
Person.new.should_not @matcher.class_name('PersonDetail')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "have_and_belong_to_many" do
|
||||
|
@ -359,6 +407,26 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
|||
Person.new.should_not @matcher.conditions(:adopted => true)
|
||||
end
|
||||
|
||||
it "should accept an association with a valid :class_name option" do
|
||||
define_model :person_relatives, :adopted => :boolean
|
||||
define_model :person do
|
||||
has_and_belongs_to_many :relatives, :class_name => 'PersonRelatives'
|
||||
end
|
||||
define_model :people_person_relative, :person_id => :integer,
|
||||
:person_relative_id => :integer
|
||||
Person.new.should @matcher.class_name('PersonRelatives')
|
||||
end
|
||||
|
||||
it "should reject an association with a bad :class_name 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.class_name('PersonRelatives')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue