Skip to content

Commit

Permalink
Use a custom validation method to validate the uniqueness of name
Browse files Browse the repository at this point in the history
The intention of this change is to address the the following
deprecation seen on Rails 6:

    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.

Since there is the complexity of the config
`ActsAsTaggableOn.strict_case_match`, we can't just explicitly set
case_sensitive option as suggested in the deprecation warning.

The tests are quite blargh to deal with since the test database has the
collation set to case-insensitive and has an uniqueness index, which is
not realistic. To deal with the tests, I used a similar approach as in
cb8d6e6. In cases where case
sensitivity matters, we alter the collation on `name` column, run the
test, then alter the collation back.
  • Loading branch information
Hector Zhao committed Oct 30, 2020
1 parent 47da503 commit 370a55c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 28 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: 7 additions & 0 deletions db/migrate/5_change_collation_for_tag_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ class ChangeCollationForTagNames < ActiveRecord::Migration[4.2]; end
else
class ChangeCollationForTagNames < ActiveRecord::Migration; end
end

ChangeCollationForTagNames.class_eval do
def up
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_bin;")
end
end

def down
if ActsAsTaggableOn::Utils.using_mysql?
execute("ALTER TABLE #{ActsAsTaggableOn.tags_table} MODIFY name varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci;")
end
end
end
14 changes: 13 additions & 1 deletion lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class Tag < ::ActiveRecord::Base
### VALIDATIONS:

validates_presence_of :name
validates_uniqueness_of :name, if: :validates_name_uniqueness?
validates_length_of :name, maximum: 255

validate :name_is_unique

# monkey patch this method if don't need name uniqueness validation
def validates_name_uniqueness?
true
Expand Down Expand Up @@ -103,6 +104,17 @@ def count
read_attribute(:count).to_i
end

private

def name_is_unique
return unless self.class.named(name).exists?

type_of_comparison = ActsAsTaggableOn.strict_case_match ? 'case-sensitive' : 'case-insensitive'
error_message = "has already been taken (using #{type_of_comparison} comparison)"

errors.add(:name, error_message)
end

class << self

private
Expand Down
111 changes: 85 additions & 26 deletions spec/acts_as_taggable_on/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'
require 'db/migrate/2_add_missing_unique_indices.rb'

require 'db/migrate/5_change_collation_for_tag_names'

shared_examples_for 'without unique index' do
prepend_before(:all) { AddMissingUniqueIndices.down }
prepend_before(:all) { AddMissingUniqueIndices.new.down }
append_after(:all) do
ActsAsTaggableOn::Tag.delete_all
AddMissingUniqueIndices.up
AddMissingUniqueIndices.new.up
end
end

shared_context 'with case sensitive collation' do
around do |example|
ChangeCollationForTagNames.new.up

example.call

ActsAsTaggableOn::Tag.delete_all
ChangeCollationForTagNames.new.down
end
end

describe ActsAsTaggableOn::Tag do
before(:each) do
@tag = ActsAsTaggableOn::Tag.new
@user = TaggableModel.create(name: 'Pablo')
end

ActsAsTaggableOn.strict_case_match = false # default to false
end

describe 'named like any' do
context 'case insensitive collation and unique index on tag name', if: using_case_insensitive_collation? do
Expand All @@ -36,12 +48,14 @@
end

before(:each) do
ActsAsTaggableOn.strict_case_match = true

ActsAsTaggableOn::Tag.create(name: 'Awesome')
ActsAsTaggableOn::Tag.create(name: 'awesome')
ActsAsTaggableOn::Tag.create(name: 'epic')
end

it 'should find both tags' do
it 'should find all three tags' do
expect(ActsAsTaggableOn::Tag.named_like_any(%w(awesome epic)).count).to eq(3)
end
end
Expand Down Expand Up @@ -76,12 +90,31 @@
@tag.save
end

it 'should find by name' do
it "should find the tag with the name 'awesome'" do
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('awesome')).to eq(@tag)
end

it 'should find by name case insensitive' do
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('AWESOME')).to eq(@tag)
context 'when `ActsAsTaggableOn.strict_case_match` is set to false' do
it "should find the tag with the name 'awesome' when 'AWESOME' is requested" do
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('AWESOME')).to eq(@tag)
end
end

context 'when `ActsAsTaggableOn.strict_case_match` is set to true' do
include_context 'with case sensitive collation'

before do
ActsAsTaggableOn.strict_case_match = true
end

it "should create a new tag with the name 'AWESOME'" do
expect do
result = ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('AWESOME')

expect(result).not_to eq(@tag)
expect(result.name).to eq 'AWESOME'
end.to change(ActsAsTaggableOn::Tag, :count).by(1)
end
end

it 'should create by name' do
Expand All @@ -101,8 +134,27 @@
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('привет')).to eq(@tag)
end

it 'should find by name case insensitive' do
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('ПРИВЕТ')).to eq(@tag)
context 'when `ActsAsTaggableOn.strict_case_match` is set to false' do
it 'should find by name case insensitive' do
expect(ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('ПРИВЕТ')).to eq(@tag)
end
end

context 'when `ActsAsTaggableOn.strict_case_match` is set to true' do
include_context 'with case sensitive collation'

before do
ActsAsTaggableOn.strict_case_match = true
end

it 'should create a new record' do
expect do
result = ActsAsTaggableOn::Tag.find_or_create_with_like_by_name('ПРИВЕТ')

expect(result).not_to eq(@tag)
expect(result.name).to eq 'ПРИВЕТ'
end.to change(ActsAsTaggableOn::Tag, :count).by(1)
end
end

it 'should find by name accent insensitive', if: using_case_insensitive_collation? do
Expand Down Expand Up @@ -289,30 +341,37 @@
end
end

describe 'name uniqeness validation' do
let(:duplicate_tag) { ActsAsTaggableOn::Tag.new(name: 'ror') }
describe 'name uniqueness validation' do
let(:duplicate_tag_downcase) { ActsAsTaggableOn::Tag.new(name: 'ror') }
let(:duplicate_tag_upcase) { ActsAsTaggableOn::Tag.new(name: 'ROR') }

before { ActsAsTaggableOn::Tag.create!(name: 'ror') }

before { ActsAsTaggableOn::Tag.create(name: 'ror') }
context 'when `ActsAsTaggableOn.strict_case_match` is set to false' do
it 'should run uniqueness validation case-insensitively' do
expect(duplicate_tag_downcase).to_not be_valid
expect(duplicate_tag_downcase.errors.size).to eq(1)
expect(duplicate_tag_downcase.errors.messages[:name].first).to include('has already been taken')

context "when don't need unique names" do
include_context 'without unique index'
it 'should not run uniqueness validation' do
allow(duplicate_tag).to receive(:validates_name_uniqueness?) { false }
duplicate_tag.save
expect(duplicate_tag).to be_persisted
expect(duplicate_tag_upcase).to_not be_valid
expect(duplicate_tag_upcase.errors.size).to eq(1)
expect(duplicate_tag_upcase.errors.messages[:name].first).to include('has already been taken')
end
end

context 'when do need unique names' do
it 'should run uniqueness validation' do
expect(duplicate_tag).to_not be_valid
context 'when `ActsAsTaggableOn.strict_case_match` is set to true' do
include_context 'with case sensitive collation'

before do
ActsAsTaggableOn.strict_case_match = true
end

it 'add error to name' do
duplicate_tag.save
it 'should run uniqueness validation case-sensitively' do
expect(duplicate_tag_downcase).to_not be_valid
expect(duplicate_tag_downcase.errors.size).to eq(1)
expect(duplicate_tag_downcase.errors.messages[:name].first).to include('has already been taken')

expect(duplicate_tag.errors.size).to eq(1)
expect(duplicate_tag.errors.messages[:name]).to include('has already been taken')
expect(duplicate_tag_upcase).to be_valid
end
end
end
Expand Down

0 comments on commit 370a55c

Please sign in to comment.