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:
Florence Foo 2015-03-02 09:39:44 +11:00 committed by Elliot Winkler
parent d811bec14a
commit b3113117bd
3 changed files with 16 additions and 1 deletions

View File

@ -55,6 +55,9 @@
* Fix `permit` so that it does not break the functionality of
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
* 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
[#648]: https://github.com/thoughtbot/shoulda-matchers/pull/648
[#675]: https://github.com/thoughtbot/shoulda-matchers/pull/675
[#677]: https://github.com/thoughtbot/shoulda-matchers/pull/677
# 2.8.0

View File

@ -386,7 +386,7 @@ module Shoulda
else
all_records = @subject.class.all
@options[:scopes].all? do |scope|
previous_value = all_records.map(&scope).max
previous_value = all_records.map(&scope).compact.max
next_value =
if previous_value.blank?

View File

@ -390,6 +390,17 @@ describe Shoulda::Matchers::ActiveRecord::ValidateUniquenessOfMatcher, type: :mo
array: true
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
context 'when the model has a case-sensitive validation' do