From 6ad754792f2a12d5cd66ba2f2ebffc42e6b16720 Mon Sep 17 00:00:00 2001 From: Panupan Sriautharawong Date: Tue, 9 Apr 2013 22:46:32 -0400 Subject: [PATCH 1/2] Add touch option to belongs_to matcher Also fixes validate failing when specified on model but not on matcher. --- .../active_record/association_matcher.rb | 25 +++++++-- .../active_record/association_matcher_spec.rb | 54 ++++++++++++++++++- 2 files changed, 73 insertions(+), 6 deletions(-) diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index 7ff0bcf5c..ae46dc80a 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -7,6 +7,8 @@ module ActiveRecord # :nodoc: # * :class_name - tests that the association resolves to class_name. # * :validate - tests that the association makes use of the validate # option. + # * :touch - tests that the association makes use of the touch + # option. # # Example: # it { should belong_to(:parent) } @@ -107,7 +109,12 @@ def with_foreign_key(foreign_key) end def validate(validate = true) - @validate = validate + @options[:validate] = validate + self + end + + def touch(touch = true) + @options[:touch] = touch self end @@ -122,7 +129,8 @@ def matches?(subject) order_correct? && conditions_correct? && join_table_exists? && - validate_correct? + validate_correct? && + touch_correct? end def failure_message_for_should @@ -258,10 +266,19 @@ def join_table_exists? end def validate_correct? - if !@validate && !reflection.options[:validate] || @validate == reflection.options[:validate] + if !@options.key?(:validate) || @options[:validate] == (reflection.options[:validate] == true ? true : false) + true + else + @missing = "#{@name} should have :validate => #{@options[:validate]}" + false + end + end + + def touch_correct? + if !@options.key?(:touch) || @options[:touch] == (reflection.options[:touch] == true ? true : false) true else - @missing = "#{@name} should have :validate => #{@validate}" + @missing = "#{@name} should have :touch => #{@options[:touch]}" false end end diff --git a/spec/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/shoulda/matchers/active_record/association_matcher_spec.rb index 2a84896e3..65855c0fe 100644 --- a/spec/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/shoulda/matchers/active_record/association_matcher_spec.rb @@ -81,9 +81,9 @@ belonging_to_parent.should_not belong_to(:parent).class_name('TreeChild') end - context 'validate' do + context 'an association with a :validate option' do [false, true].each do |validate_value| - context 'when the model has :validate => #{validate_value}' do + context "when the model has :validate => #{validate_value}" do it 'accepts a matching validate option' do belonging_to_parent(:validate => validate_value). should belong_to(:parent).validate(validate_value) @@ -103,6 +103,11 @@ should_not belong_to(:parent).validate end end + + it 'will not break matcher when validate option is unspecified' do + belonging_to_parent(:validate => validate_value). + should belong_to(:parent) + end end end end @@ -121,6 +126,51 @@ end end + context 'an association with a :touch option' do + [false, true].each do |touch_value| + context "when the model has :touch => #{touch_value}" do + it 'accepts a matching touch option' do + belonging_to_parent(:touch => touch_value).should + belong_to(:parent).touch(touch_value) + end + + it 'rejects a non-matching touch option' do + belonging_to_parent(:touch => touch_value).should_not + belong_to(:parent).touch(!touch_value) + end + + it 'defaults to touch(true)' do + if touch_value + belonging_to_parent(:touch => touch_value).should + belong_to(:parent).touch + else + belonging_to_parent(:touch => touch_value).should_not + belong_to(:parent).touch + end + end + + it 'will not break matcher when touch option is unspecified' do + belonging_to_parent(:touch => touch_value).should + belong_to(:parent) + end + end + end + end + + context 'an association without a :touch option' do + it 'accepts touch(false)' do + belonging_to_parent.should belong_to(:parent).touch(false) + end + + it 'rejects touch(true)' do + belonging_to_parent.should_not belong_to(:parent).touch(true) + end + + it 'rejects touch()' do + belonging_to_parent.should_not belong_to(:parent).touch + end + end + def belonging_to_parent(options = {}) define_model :parent define_model :child, :parent_id => :integer do From 064dd086971dd9b5a9ebbd5719c88b65495360e2 Mon Sep 17 00:00:00 2001 From: Panupan Sriautharawong Date: Fri, 12 Apr 2013 15:04:03 -0400 Subject: [PATCH 2/2] Couple fixes to association_matcher_spec.rb --- .../active_record/association_matcher.rb | 4 ++-- .../active_record/association_matcher_spec.rb | 22 +++++++++---------- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/shoulda/matchers/active_record/association_matcher.rb b/lib/shoulda/matchers/active_record/association_matcher.rb index ae46dc80a..ed4b951bd 100644 --- a/lib/shoulda/matchers/active_record/association_matcher.rb +++ b/lib/shoulda/matchers/active_record/association_matcher.rb @@ -266,7 +266,7 @@ def join_table_exists? end def validate_correct? - if !@options.key?(:validate) || @options[:validate] == (reflection.options[:validate] == true ? true : false) + if !@options.key?(:validate) || @options[:validate] == !!reflection.options[:validate] true else @missing = "#{@name} should have :validate => #{@options[:validate]}" @@ -275,7 +275,7 @@ def validate_correct? end def touch_correct? - if !@options.key?(:touch) || @options[:touch] == (reflection.options[:touch] == true ? true : false) + if !@options.key?(:touch) || @options[:touch] == !!reflection.options[:touch] true else @missing = "#{@name} should have :touch => #{@options[:touch]}" diff --git a/spec/shoulda/matchers/active_record/association_matcher_spec.rb b/spec/shoulda/matchers/active_record/association_matcher_spec.rb index 65855c0fe..8ea9f0560 100644 --- a/spec/shoulda/matchers/active_record/association_matcher_spec.rb +++ b/spec/shoulda/matchers/active_record/association_matcher_spec.rb @@ -105,8 +105,7 @@ end it 'will not break matcher when validate option is unspecified' do - belonging_to_parent(:validate => validate_value). - should belong_to(:parent) + belonging_to_parent(:validate => validate_value).should belong_to(:parent) end end end @@ -130,28 +129,27 @@ [false, true].each do |touch_value| context "when the model has :touch => #{touch_value}" do it 'accepts a matching touch option' do - belonging_to_parent(:touch => touch_value).should - belong_to(:parent).touch(touch_value) + belonging_to_parent(:touch => touch_value). + should belong_to(:parent).touch(touch_value) end it 'rejects a non-matching touch option' do - belonging_to_parent(:touch => touch_value).should_not - belong_to(:parent).touch(!touch_value) + belonging_to_parent(:touch => touch_value). + should_not belong_to(:parent).touch(!touch_value) end it 'defaults to touch(true)' do if touch_value - belonging_to_parent(:touch => touch_value).should - belong_to(:parent).touch + belonging_to_parent(:touch => touch_value). + should belong_to(:parent).touch else - belonging_to_parent(:touch => touch_value).should_not - belong_to(:parent).touch + belonging_to_parent(:touch => touch_value). + should_not belong_to(:parent).touch end end it 'will not break matcher when touch option is unspecified' do - belonging_to_parent(:touch => touch_value).should - belong_to(:parent) + belonging_to_parent(:touch => touch_value).should belong_to(:parent) end end end