-
-
Notifications
You must be signed in to change notification settings - Fork 909
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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) | ||
else option_verifier.correct_for_string?(:dependent, dependent) | ||
end | ||
end | ||
|
||
def missing_option_for(name, dependent) | ||
"#{name} should have #{dependent == true ? "a" : dependent} dependency" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [83/80] |
||
end | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extra empty line detected at body end. |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Line is too long. [83/80] |
||
end | ||
end | ||
end |
There was a problem hiding this comment.
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]