-
-
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
No more throws a error when use validate_uniqueness_of with scope of boolean column #694
Closed
maurogeorge
wants to merge
1
commit into
thoughtbot:master
from
maurogeorge:validate_uniqueness_of-boolean
+58
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,43 @@ | |
column_type: :string | ||
end | ||
|
||
context 'when one of the scoped attributes is a boolean column' do | ||
include_context 'it supports scoped attributes of a certain type', | ||
column_type: :boolean | ||
|
||
context 'when there is more than one scoped attribute and all are boolean columns' 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. Line is too long. [91/80] |
||
it 'accepts when all the scoped attribute are true' do | ||
record = build_record_validating_uniqueness( | ||
scopes: [ | ||
build_attribute(name: :scope1), | ||
build_attribute(name: :scope2), | ||
] | ||
) | ||
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2) | ||
end | ||
|
||
it 'accepts when all the scoped attribute are false' do | ||
record = build_record_validating_uniqueness( | ||
scopes: [ | ||
build_attribute(name: :scope1, value: false), | ||
build_attribute(name: :scope2, value: false), | ||
] | ||
) | ||
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2) | ||
end | ||
|
||
it 'accepts when one scoped attribute is true and other is false' do | ||
record = build_record_validating_uniqueness( | ||
scopes: [ | ||
build_attribute(name: :scope1), | ||
build_attribute(name: :scope2, value: false), | ||
] | ||
) | ||
expect(record).to validate_uniqueness.scoped_to(:scope1, :scope2) | ||
end | ||
end | ||
end | ||
|
||
context 'when one of the scoped attributes is an integer column' do | ||
include_context 'it supports scoped attributes of a certain type', | ||
column_type: :integer | ||
|
@@ -752,6 +789,8 @@ def dummy_scalar_value_for(attribute_type) | |
Time.now | ||
when :uuid | ||
SecureRandom.uuid | ||
when :boolean | ||
true | ||
else | ||
raise ArgumentError, "Unknown type '#{attribute_type}'" | ||
end | ||
|
@@ -764,6 +803,8 @@ def next_version_of(value, value_type) | |
SecureRandom.uuid | ||
elsif value.is_a?(Time) | ||
value + 1 | ||
elsif value.in?([true, false]) | ||
!value | ||
elsif value.respond_to?(:next) | ||
value.next | ||
end | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think the tests for these are going to be a little different, because there's a limited number of values for a boolean column.
If you have one existing record where the scope attribute is either true or false, then you can simply flip that scope attribute. If you have two existing records where one of the record's scope attribute is true and the other is false... then you can't flip the scope attribute.
Say you have a validation on an attribute against a boolean scope:
Here's what a test would look like if there's only one existing record:
But now consider if there are two existing records. Here's what those tests might look like:
So now the question is... how do we modify the matcher to run these tests. I don't have a clear solution for this, but it seems like as soon as one of the scopes is a boolean attribute, it kind of messes with the existing logic...