Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to match belong_to with touch #222

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 21 additions & 4 deletions lib/shoulda/matchers/active_record/association_matcher.rb
Expand Up @@ -7,6 +7,8 @@ module ActiveRecord # :nodoc:
# * <tt>:class_name</tt> - tests that the association resolves to class_name.
# * <tt>:validate</tt> - tests that the association makes use of the validate
# option.
# * <tt>:touch</tt> - tests that the association makes use of the touch
# option.
#
# Example:
# it { should belong_to(:parent) }
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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
else
@missing = "#{@name} should have :validate => #{@options[:validate]}"
false
end
end

def touch_correct?
if !@options.key?(:touch) || @options[:touch] == !!reflection.options[:touch]
true
else
@missing = "#{@name} should have :validate => #{@validate}"
@missing = "#{@name} should have :touch => #{@options[:touch]}"
false
end
end
Expand Down
52 changes: 50 additions & 2 deletions spec/shoulda/matchers/active_record/association_matcher_spec.rb
Expand Up @@ -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)
Expand All @@ -103,6 +103,10 @@
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
Expand All @@ -121,6 +125,50 @@
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
Expand Down