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