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 true option for dependent matcher and test all options #631

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/shoulda/matchers/active_record/association_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ module ActiveRecord
# should belong_to(:world).dependent(:destroy)
# end
#
# To test that any :dependent option was specified, pass true.
# # RSpec
# describe Person do
# it { should belong_to(:world).dependent(true) }
# end
#
# ##### counter_cache
#
# Use `counter_cache` to assert that the `:counter_cache` option was
Expand Down Expand Up @@ -993,7 +999,7 @@ def macro_correct?
end

def macro_supports_primary_key?
macro == :belongs_to ||
macro == :belongs_to ||
([:has_many, :has_one].include?(macro) && !through?)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ def description

def matches?(subject)
self.subject = ModelReflector.new(subject, name)

if option_verifier.correct_for_string?(:dependent, dependent)
if correct_for?(dependent)
true
else
self.missing_option = "#{name} should have #{dependent} dependency"
self.missing_option = missing_option_for(name, dependent)
false
end
end
Expand All @@ -34,6 +33,18 @@ def matches?(subject)
def option_verifier
@option_verifier ||= OptionVerifier.new(subject)
end

def correct_for?(dependent)
case dependent
when true, false then option_verifier.correct_for_boolean?(:dependent, dependent)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [93/80]

else option_verifier.correct_for_string?(:dependent, dependent)
end
end

def missing_option_for(name, dependent)
"#{name} should have #{dependent == true ? "a" : dependent} dependency"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra empty line detected at body end.

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,28 @@ def having_many_non_existent_class(model_name, assoc_name, options = {})
end

it 'accepts an association with a valid :dependent option' do
expect(having_one_detail(dependent: :destroy)).
to have_one(:detail).dependent(:destroy)
dependent_options.each do |option|
expect(having_one_detail(dependent: option)).
to have_one(:detail).dependent(option)
end
end

it 'accepts any dependent option if true' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

dependent_options.each do |option|
expect(having_one_detail(dependent: option)).
to have_one(:detail).dependent(true)
end
end

it 'rejects any dependent options if false' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

dependent_options.each do |option|
expect(having_one_detail(dependent: option)).
to_not have_one(:detail).dependent(false)
end
end

it 'accepts a nil dependent option if false' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

expect(having_one_detail).to have_one(:detail).dependent(false)
end

it 'rejects an association with a bad :dependent option' do
Expand Down Expand Up @@ -1117,4 +1137,13 @@ def define_association_with_order(model, macro, name, order, other_options={})
args << options
model.__send__(macro, name, *args)
end

def dependent_options
case Rails.version
when /\A3/
[:destroy, :delete, :nullify, :restrict]
when /\A4/
[:destroy, :delete, :nullify, :restrict_with_exception, :restrict_with_error]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [83/80]

end
end
end