Class: ActsAsTaggableOn::Tag

Inherits:
Object
  • Object
show all
Defined in:
lib/acts-as-taggable-on/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_all_with_like_by_name(*list) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

CLASS METHODS:



69
70
71
72
73
74
75
# 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) ⇒ Object



55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
# 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) ⇒ Object



26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
45
# 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) ⇒ Object



47
48
49
50
51
52
53
# 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

Instance Method Details

#==(other) ⇒ Object

INSTANCE METHODS:



102
103
104
# File 'lib/acts-as-taggable-on/tag.rb', line 102

def ==(other)
  super || (other.is_a?(Tag) && name == other.name)
end

#countObject



110
111
112
# File 'lib/acts-as-taggable-on/tag.rb', line 110

def count
  read_attribute(:count).to_i
end

#to_sObject



106
107
108
# File 'lib/acts-as-taggable-on/tag.rb', line 106

def to_s
  name
end

#validates_name_uniqueness?Boolean

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

Returns:

  • (Boolean)


18
19
20
# File 'lib/acts-as-taggable-on/tag.rb', line 18

def validates_name_uniqueness?
  true
end