Add touch option to belongs_to matcher.
* Also fixes validate failing when specified on model but not on matcher.
This commit is contained in:
parent
2401bda8cc
commit
5ad04a89e2
1
NEWS.md
1
NEWS.md
|
@ -1,5 +1,6 @@
|
||||||
# HEAD
|
# HEAD
|
||||||
|
|
||||||
|
* Add `:touch` option to the association matcher.
|
||||||
* Ruby 2.0.0 is now officially supported.
|
* Ruby 2.0.0 is now officially supported.
|
||||||
* Fixes the issue where using %{attribute} or %{model} in I18n translations
|
* Fixes the issue where using %{attribute} or %{model} in I18n translations
|
||||||
raised exceptions
|
raised exceptions
|
||||||
|
|
|
@ -7,6 +7,8 @@ module Shoulda # :nodoc:
|
||||||
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
|
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
|
||||||
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
# * <tt>:validate</tt> - tests that the association makes use of the validate
|
||||||
# option.
|
# option.
|
||||||
|
# * <tt>:touch</tt> - tests that the association makes use of the touch
|
||||||
|
# option.
|
||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# it { should belong_to(:parent) }
|
# it { should belong_to(:parent) }
|
||||||
|
@ -107,7 +109,12 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate(validate = true)
|
def validate(validate = true)
|
||||||
@validate = validate
|
@options[:validate] = validate
|
||||||
|
self
|
||||||
|
end
|
||||||
|
|
||||||
|
def touch(touch = true)
|
||||||
|
@options[:touch] = touch
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -122,7 +129,8 @@ module Shoulda # :nodoc:
|
||||||
order_correct? &&
|
order_correct? &&
|
||||||
conditions_correct? &&
|
conditions_correct? &&
|
||||||
join_table_exists? &&
|
join_table_exists? &&
|
||||||
validate_correct?
|
validate_correct? &&
|
||||||
|
touch_correct?
|
||||||
end
|
end
|
||||||
|
|
||||||
def failure_message_for_should
|
def failure_message_for_should
|
||||||
|
@ -258,14 +266,31 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_correct?
|
def validate_correct?
|
||||||
if !@validate && !reflection.options[:validate] || @validate == reflection.options[:validate]
|
if option_correct?(:validate)
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
@missing = "#{@name} should have :validate => #{@validate}"
|
@missing = "#{@name} should have :validate => #{@options[:validate]}"
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def touch_correct?
|
||||||
|
if option_correct?(:touch)
|
||||||
|
true
|
||||||
|
else
|
||||||
|
@missing = "#{@name} should have :touch => #{@options[:touch]}"
|
||||||
|
false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def option_correct?(key)
|
||||||
|
!@options.key?(key) || reflection_set_properly_for?(key)
|
||||||
|
end
|
||||||
|
|
||||||
|
def reflection_set_properly_for?(key)
|
||||||
|
@options[key] == !!reflection.options[key]
|
||||||
|
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]
|
||||||
|
|
|
@ -81,9 +81,9 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
belonging_to_parent.should_not belong_to(:parent).class_name('TreeChild')
|
belonging_to_parent.should_not belong_to(:parent).class_name('TreeChild')
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'validate' do
|
context 'an association with a :validate option' do
|
||||||
[false, true].each do |validate_value|
|
[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
|
it 'accepts a matching validate option' do
|
||||||
belonging_to_parent(:validate => validate_value).
|
belonging_to_parent(:validate => validate_value).
|
||||||
should belong_to(:parent).validate(validate_value)
|
should belong_to(:parent).validate(validate_value)
|
||||||
|
@ -103,6 +103,10 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
should_not belong_to(:parent).validate
|
should_not belong_to(:parent).validate
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
end
|
end
|
||||||
|
@ -121,6 +125,50 @@ describe Shoulda::Matchers::ActiveRecord::AssociationMatcher do
|
||||||
end
|
end
|
||||||
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 = {})
|
def belonging_to_parent(options = {})
|
||||||
define_model :parent
|
define_model :parent
|
||||||
define_model :child, :parent_id => :integer do
|
define_model :child, :parent_id => :integer do
|
||||||
|
|
Loading…
Reference in New Issue