Replace instance variables with readable attributes in AssociationMatcher

This commit is contained in:
Melissa Xie 2013-06-14 11:34:22 -04:00
parent a8e2c5145d
commit 718c8b6dd0
1 changed files with 35 additions and 33 deletions

View File

@ -81,19 +81,19 @@ module Shoulda # :nodoc:
end end
def through(through) def through(through)
through_matcher = AssociationMatchers::ThroughMatcher.new(through, @name) through_matcher = AssociationMatchers::ThroughMatcher.new(through, name)
add_submatcher(through_matcher) add_submatcher(through_matcher)
self self
end end
def dependent(dependent) def dependent(dependent)
dependent_matcher = AssociationMatchers::DependentMatcher.new(dependent, @name) dependent_matcher = AssociationMatchers::DependentMatcher.new(dependent, name)
add_submatcher(dependent_matcher) add_submatcher(dependent_matcher)
self self
end end
def order(order) def order(order)
order_matcher = AssociationMatchers::OrderMatcher.new(order, @name) order_matcher = AssociationMatchers::OrderMatcher.new(order, name)
add_submatcher(order_matcher) add_submatcher(order_matcher)
self self
end end
@ -124,13 +124,13 @@ module Shoulda # :nodoc:
end end
def description def description
description = "#{macro_description} #{@name}" description = "#{macro_description} #{name}"
description += " class_name => #{@options[:class_name]}" if @options.key?(:class_name) description += " class_name => #{options[:class_name]}" if options.key?(:class_name)
[description, @submatchers.map(&:description)].flatten.join(' ') [description, submatchers.map(&:description)].flatten.join(' ')
end end
def failure_message_for_should def failure_message_for_should
"Expected #{expectation} (#{missing})" "Expected #{expectation} (#{missing_options})"
end end
def failure_message_for_should_not def failure_message_for_should_not
@ -152,12 +152,14 @@ module Shoulda # :nodoc:
private private
attr_reader :submatchers, :missing, :subject, :macro, :name, :options
def add_submatcher(matcher) def add_submatcher(matcher)
@submatchers << matcher @submatchers << matcher
end end
def macro_description def macro_description
case @macro.to_s case macro.to_s
when 'belongs_to' when 'belongs_to'
'belong to' 'belong to'
when 'has_many' when 'has_many'
@ -170,22 +172,22 @@ module Shoulda # :nodoc:
end end
def expectation def expectation
"#{model_class.name} to have a #{@macro} association called #{@name}" "#{model_class.name} to have a #{macro} association called #{name}"
end end
def missing def missing_options
[@missing, failing_submatchers.map(&:missing_option)].flatten.join [missing, failing_submatchers.map(&:missing_option)].flatten.join
end end
def failing_submatchers def failing_submatchers
@failing_submatchers ||= @submatchers.select do |matcher| @failing_submatchers ||= submatchers.select do |matcher|
!matcher.matches?(@subject) !matcher.matches?(subject)
end end
end end
def association_exists? def association_exists?
if reflection.nil? if reflection.nil?
@missing = "no association called #{@name}" @missing = "no association called #{name}"
false false
else else
true true
@ -193,11 +195,11 @@ module Shoulda # :nodoc:
end end
def reflection def reflection
@reflection ||= model_class.reflect_on_association(@name) @reflection ||= model_class.reflect_on_association(name)
end end
def macro_correct? def macro_correct?
if reflection.macro == @macro if reflection.macro == macro
true true
else else
@missing = "actual association type was #{reflection.macro}" @missing = "actual association type was #{reflection.macro}"
@ -210,15 +212,15 @@ module Shoulda # :nodoc:
end end
def belongs_foreign_key_missing? def belongs_foreign_key_missing?
@macro == :belongs_to && !class_has_foreign_key?(model_class) macro == :belongs_to && !class_has_foreign_key?(model_class)
end end
def model_class def model_class
@subject.class subject.class
end end
def has_foreign_key_missing? def has_foreign_key_missing?
[:has_many, :has_one].include?(@macro) && [:has_many, :has_one].include?(macro) &&
!through? && !through? &&
!class_has_foreign_key?(associated_class) !class_has_foreign_key?(associated_class)
end end
@ -232,11 +234,11 @@ module Shoulda # :nodoc:
end end
def class_name_correct? def class_name_correct?
if @options.key?(:class_name) if options.key?(:class_name)
if @options[:class_name].to_s == reflection.klass.to_s if options[:class_name].to_s == reflection.klass.to_s
true true
else else
@missing = "#{@name} should resolve to #{@options[:class_name]} for class_name" @missing = "#{name} should resolve to #{options[:class_name]} for class_name"
false false
end end
else else
@ -245,11 +247,11 @@ module Shoulda # :nodoc:
end end
def conditions_correct? def conditions_correct?
if @options.key?(:conditions) if options.key?(:conditions)
if @options[:conditions].to_s == reflection.options[:conditions].to_s if options[:conditions].to_s == reflection.options[:conditions].to_s
true true
else else
@missing = "#{@name} should have the following conditions: #{@options[:conditions]}" @missing = "#{name} should have the following conditions: #{options[:conditions]}"
false false
end end
else else
@ -258,7 +260,7 @@ module Shoulda # :nodoc:
end end
def join_table_exists? def join_table_exists?
if @macro != :has_and_belongs_to_many || if macro != :has_and_belongs_to_many ||
model_class.connection.tables.include?(join_table) model_class.connection.tables.include?(join_table)
true true
else else
@ -271,7 +273,7 @@ module Shoulda # :nodoc:
if option_correct?(:validate) if option_correct?(:validate)
true true
else else
@missing = "#{@name} should have :validate => #{@options[:validate]}" @missing = "#{name} should have :validate => #{options[:validate]}"
false false
end end
end end
@ -280,22 +282,22 @@ module Shoulda # :nodoc:
if option_correct?(:touch) if option_correct?(:touch)
true true
else else
@missing = "#{@name} should have :touch => #{@options[:touch]}" @missing = "#{name} should have :touch => #{options[:touch]}"
false false
end end
end end
def option_correct?(key) def option_correct?(key)
!@options.key?(key) || reflection_set_properly_for?(key) !options.key?(key) || reflection_set_properly_for?(key)
end end
def reflection_set_properly_for?(key) def reflection_set_properly_for?(key)
@options[key] == !!reflection.options[key] options[key] == !!reflection.options[key]
end end
def class_has_foreign_key?(klass) def class_has_foreign_key?(klass)
if @options.key?(:foreign_key) if options.key?(:foreign_key)
reflection.options[:foreign_key] == @options[:foreign_key] reflection.options[:foreign_key] == options[:foreign_key]
else else
if klass.column_names.include?(foreign_key) if klass.column_names.include?(foreign_key)
true true
@ -325,7 +327,7 @@ module Shoulda # :nodoc:
end end
def foreign_key_reflection def foreign_key_reflection
if [:has_one, :has_many].include?(@macro) && reflection.options.include?(:inverse_of) if [:has_one, :has_many].include?(macro) && reflection.options.include?(:inverse_of)
associated_class.reflect_on_association(reflection.options[:inverse_of]) associated_class.reflect_on_association(reflection.options[:inverse_of])
else else
reflection reflection