class ActsAsTaggableOn::Tag

Public Class Methods

find_or_create_all_with_like_by_name(*list) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 77
def self.find_or_create_all_with_like_by_name(*list)
  list = Array(list).flatten

  return [] if list.empty?

  existing_tags = named_any(list)
  list.map do |tag_name|
    tries ||= 3
    comparable_tag_name = comparable_name(tag_name)
    existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
    next existing_tag if existing_tag

    transaction(requires_new: true) { create(name: tag_name) }
  rescue ActiveRecord::RecordNotUnique
    if (tries -= 1).positive?
      existing_tags = named_any(list)
      retry
    end

    raise DuplicateTagError, "'#{tag_name}' has already been taken"
  end
end
find_or_create_with_like_by_name(name) click to toggle source

CLASS METHODS:

# File lib/acts-as-taggable-on/tag.rb, line 69
def self.find_or_create_with_like_by_name(name)
  if ActsAsTaggableOn.strict_case_match
    find_or_create_all_with_like_by_name([name]).first
  else
    named_like(name).first || create(name: name)
  end
end
for_context(context) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 55
def self.for_context(context)
  joins(:taggings)
    .where(["#{ActsAsTaggableOn.taggings_table}.context = ?", context])
    .select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end
for_tenant(tenant) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 61
def self.for_tenant(tenant)
  joins(:taggings)
    .where("#{ActsAsTaggableOn.taggings_table}.tenant = ?", tenant.to_s)
    .select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end
named(name) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 26
def self.named(name)
  if ActsAsTaggableOn.strict_case_match
    where(["name = #{binary}?", as_8bit_ascii(name)])
  else
    where(['LOWER(name) = LOWER(?)', as_8bit_ascii(unicode_downcase(name))])
  end
end
named_any(list) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 34
def self.named_any(list)
  clause = list.map do |tag|
    sanitize_sql_for_named_any(tag)
  end.join(' OR ')
  where(clause)
end
named_like(name) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 41
def self.named_like(name)
  clause = ["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'",
            "%#{ActsAsTaggableOn::Utils.escape_like(name)}%"]
  where(clause)
end
named_like_any(list) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 47
def self.named_like_any(list)
  clause = list.map do |tag|
    sanitize_sql(["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'",
                  "%#{ActsAsTaggableOn::Utils.escape_like(tag.to_s)}%"])
  end.join(' OR ')
  where(clause)
end

Private Class Methods

as_8bit_ascii(string) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 129
def as_8bit_ascii(string)
  string.to_s.mb_chars
end
binary() click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 125
def binary
  ActsAsTaggableOn::Utils.using_mysql? ? 'BINARY ' : nil
end
comparable_name(str) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 117
def comparable_name(str)
  if ActsAsTaggableOn.strict_case_match
    str
  else
    unicode_downcase(str.to_s)
  end
end
sanitize_sql_for_named_any(tag) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 137
def sanitize_sql_for_named_any(tag)
  if ActsAsTaggableOn.strict_case_match
    sanitize_sql(["name = #{binary}?", as_8bit_ascii(tag)])
  else
    sanitize_sql(['LOWER(name) = LOWER(?)', as_8bit_ascii(unicode_downcase(tag))])
  end
end
unicode_downcase(string) click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 133
def unicode_downcase(string)
  as_8bit_ascii(string).downcase
end

Public Instance Methods

==(other) click to toggle source

INSTANCE METHODS:

Calls superclass method
# File lib/acts-as-taggable-on/tag.rb, line 102
def ==(other)
  super || (other.is_a?(Tag) && name == other.name)
end
count() click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 110
def count
  read_attribute(:count).to_i
end
to_s() click to toggle source
# File lib/acts-as-taggable-on/tag.rb, line 106
def to_s
  name
end
validates_name_uniqueness?() click to toggle source

monkey patch this method if don’t need name uniqueness validation

# File lib/acts-as-taggable-on/tag.rb, line 18
def validates_name_uniqueness?
  true
end