Skip to content
This repository has been archived by the owner on Apr 2, 2021. It is now read-only.

Commit

Permalink
Added class_name option matcher to association_matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
SweeD committed Oct 16, 2011
1 parent ead1e1d commit 17ac7d5
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
20 changes: 19 additions & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -18,6 +18,7 @@ def belong_to(name)
# * <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) }
Expand All @@ -35,6 +36,7 @@ def have_many(name)
# 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
Expand Down Expand Up @@ -72,19 +74,25 @@ def order(order)
@order = order
self
end

def conditions(conditions)
@conditions = conditions
self
end

def class_name(class_name)
@class_name = class_name
self
end

def matches?(subject)
@subject = subject
association_exists? &&
macro_correct? &&
foreign_key_exists? &&
through_association_valid? &&
dependent_correct? &&
class_name_correct? &&
order_correct? &&
conditions_correct? &&
join_table_exists?
Expand All @@ -102,6 +110,7 @@ def description
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
Expand Down Expand Up @@ -172,6 +181,15 @@ def dependent_correct?
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
Expand Down
70 changes: 69 additions & 1 deletion spec/shoulda/active_record/association_matcher_spec.rb
Expand Up @@ -83,6 +83,22 @@
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
Expand Down Expand Up @@ -208,6 +224,22 @@
end
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

Expand Down Expand Up @@ -297,7 +329,23 @@
end
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
Expand Down Expand Up @@ -359,6 +407,26 @@
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

0 comments on commit 17ac7d5

Please sign in to comment.