Skip to content

Commit

Permalink
Explicitly set the case_sensitive option for uniqueness validators
Browse files Browse the repository at this point in the history
On Rails 6 we see the following deprecation:

    DEPRECATION WARNING: Uniqueness validator will no longer enforce
    case sensitive comparison in Rails 6.1. To continue case sensitive
    comparison on the :name attribute in ActsAsTaggableOn::Tag model,
    pass `case_sensitive: true` option explicitly to the uniqueness
    validator.

Also update the validation syntax to the "new" and recommended way
introduced in Rails 4.
  • Loading branch information
Hector Zhao committed Oct 28, 2020
1 parent 47da503 commit 6935055
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ As such, _Breaking Changes_ are major. _Features_ would map to either major or m
### [Master / Unreleased](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.5.0...master)

* Fixes
* [@nbulaj Add support for Ruby 2.7 and it's kwargs](https://github.com/mbleigh/acts-as-taggable-on/pull/910)
* [@nbulaj Add support for Ruby 2.7 and it's kwargs](https://github.com/mbleigh/acts-as-taggable-on/pull/910)
* [@FunnyHector Fix the uniqueness validator deprecation warning on Rails 6](https://github.com/mbleigh/acts-as-taggable-on/pull/1009)

### [6.5.0 / 2019-11-07](https://github.com/mbleigh/acts-as-taggable-on/compare/v6.0.0...v6.5.0)

Expand Down
7 changes: 4 additions & 3 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ class Tag < ::ActiveRecord::Base

### VALIDATIONS:

validates_presence_of :name
validates_uniqueness_of :name, if: :validates_name_uniqueness?
validates_length_of :name, maximum: 255
validates :name,
presence: true,
uniqueness: { case_sensitive: ActsAsTaggableOn.strict_case_match, if: :validates_name_uniqueness? },
length: { maximum: 255 }

# monkey patch this method if don't need name uniqueness validation
def validates_name_uniqueness?
Expand Down

0 comments on commit 6935055

Please sign in to comment.