validate_after_scope_change? use compact before retrieving max
Otherwise produces error "ArgumentError: comparison of NilClass with String failed" if the resultant contains a combinations of strings and nils.
This commit is contained in:
parent
d811bec14a
commit
b3113117bd
4
NEWS.md
4
NEWS.md
|
@ -55,6 +55,9 @@
|
||||||
* Fix `permit` so that it does not break the functionality of
|
* Fix `permit` so that it does not break the functionality of
|
||||||
ActionController::Parameters#require. ([#648], [#675])
|
ActionController::Parameters#require. ([#648], [#675])
|
||||||
|
|
||||||
|
* Fix `validate_uniqueness_of` + `scoped_to` so that it does not raise an error
|
||||||
|
if a record exists where the scoped attribute is nil. ([#677])
|
||||||
|
|
||||||
### Features
|
### Features
|
||||||
|
|
||||||
* Add `on` qualifier to `permit`. This allows you to make an assertion that
|
* Add `on` qualifier to `permit`. This allows you to make an assertion that
|
||||||
|
@ -71,6 +74,7 @@
|
||||||
[#607]: https://github.com/thoughtbot/shoulda-matchers/pull/607
|
[#607]: https://github.com/thoughtbot/shoulda-matchers/pull/607
|
||||||
[#648]: https://github.com/thoughtbot/shoulda-matchers/pull/648
|
[#648]: https://github.com/thoughtbot/shoulda-matchers/pull/648
|
||||||
[#675]: https://github.com/thoughtbot/shoulda-matchers/pull/675
|
[#675]: https://github.com/thoughtbot/shoulda-matchers/pull/675
|
||||||
|
[#677]: https://github.com/thoughtbot/shoulda-matchers/pull/677
|
||||||
|
|
||||||
# 2.8.0
|
# 2.8.0
|
||||||
|
|
||||||
|
|
|
@ -386,7 +386,7 @@ module Shoulda
|
||||||
else
|
else
|
||||||
all_records = @subject.class.all
|
all_records = @subject.class.all
|
||||||
@options[:scopes].all? do |scope|
|
@options[:scopes].all? do |scope|
|
||||||
previous_value = all_records.map(&scope).max
|
previous_value = all_records.map(&scope).compact.max
|
||||||
|
|
||||||
next_value =
|
next_value =
|
||||||
if previous_value.blank?
|
if previous_value.blank?
|
||||||
|
|
|
@ -390,6 +390,17 @@ describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher, type: :mo
|
||||||
array: true
|
array: true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "when an existing record that is not the first has a nil value for the scoped attribute" do
|
||||||
|
it 'still works' do
|
||||||
|
model = define_model_validating_uniqueness(scopes: [:scope])
|
||||||
|
create_record_from(model, scope: 'some value')
|
||||||
|
create_record_from(model, scope: nil)
|
||||||
|
record = build_record_from(model, scope: 'a different value')
|
||||||
|
|
||||||
|
expect(record).to validate_uniqueness.scoped_to(:scope)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when the model has a case-sensitive validation' do
|
context 'when the model has a case-sensitive validation' do
|
||||||
|
|
Loading…
Reference in New Issue