Skip to content

Commit

Permalink
Use I18n error message for uniqueness validation on Tag#name
Browse files Browse the repository at this point in the history
This is the first custom error message so a locale file is added. For
any non-en locale, this error message will raise a translation missing
error. Apps that uses this gem should add their own i18n translations.
  • Loading branch information
Hector Zhao committed Feb 2, 2022
1 parent ed70273 commit c9aeb0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
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)
4 changes: 1 addition & 3 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ 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)
errors.add(:name, :not_unique, type_of_comparison: type_of_comparison)
end

class << self
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

0 comments on commit c9aeb0d

Please sign in to comment.