Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a custom validation method to validate the uniqueness of name #1009

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
en:
activerecord:
errors:
models:
acts_as_taggable_on/tag:
attributes:
name:
not_unique: has already been taken (using %{type_of_comparison} comparison)
6 changes: 6 additions & 0 deletions db/migrate/5_change_collation_for_tag_names.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ def up
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
12 changes: 11 additions & 1 deletion lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class Tag < ActsAsTaggableOn.base_class
### VALIDATIONS:

validates_presence_of :name
validates_uniqueness_of :name, if: :validates_name_uniqueness?, case_sensitive: true
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 @@ -110,6 +111,15 @@ 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'
errors.add(:name, :not_unique, type_of_comparison: type_of_comparison)
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', tenant_id: 100)
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 @@ -91,12 +105,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 @@ -116,8 +149,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 @@ -304,30 +356,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
3 changes: 3 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
require 'logger'

require File.expand_path('../../lib/acts-as-taggable-on', __FILE__)

I18n.load_path << File.expand_path('../../config/locales/en.yml', __FILE__)
I18n.enforce_available_locales = true

require 'rails'
require 'rspec/its'
require 'barrier'
Expand Down