Skip to content

Commit

Permalink
validate_after_scope_change? use compact before retrieving max
Browse files Browse the repository at this point in the history
Otherwise produces error "ArgumentError: comparison of NilClass with
String failed" if the resultant contains a combinations of strings and
nils.
  • Loading branch information
Florence Foo authored and mcmire committed Mar 15, 2015
1 parent d811bec commit b311311
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def validate_after_scope_change?
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?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,17 @@
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
Expand Down

0 comments on commit b311311

Please sign in to comment.