Class: ActsAsTaggableOn::Tag

Inherits:
ActiveRecord::Base
  • 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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/acts_as_taggable_on/tag.rb', line 74

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|
    begin
      tries ||= 3
      comparable_tag_name = comparable_name(tag_name)
      existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
      existing_tag || create(name: tag_name)
    rescue ActiveRecord::RecordNotUnique
      if (tries -= 1).positive?
        ActiveRecord::Base.connection.execute 'ROLLBACK'
        existing_tags = named_any(list)
        retry
      end

      raise DuplicateTagError.new("'#{tag_name}' has already been taken")
    end
  end
end

.find_or_create_with_like_by_name(name) ⇒ Object

CLASS METHODS:



66
67
68
69
70
71
72
# File 'lib/acts_as_taggable_on/tag.rb', line 66

def self.find_or_create_with_like_by_name(name)
  if ActsAsTaggableOn.strict_case_match
    self.find_or_create_all_with_like_by_name([name]).first
  else
    named_like(name).first || create(name: name)
  end
end

.for_context(context) ⇒ Object



52
53
54
55
56
# File 'lib/acts_as_taggable_on/tag.rb', line 52

def self.for_context(context)
  joins(:taggings).
    where(["#{ActsAsTaggableOn.taggings_table}.context = ?", context]).
    select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end

.for_tenant(tenant) ⇒ Object



58
59
60
61
62
# File 'lib/acts_as_taggable_on/tag.rb', line 58

def self.for_tenant(tenant)
  joins(:taggings).
    where("#{ActsAsTaggableOn.taggings_table}.tenant = ?", tenant.to_s).
    select("DISTINCT #{ActsAsTaggableOn.tags_table}.*")
end

.named(name) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/acts_as_taggable_on/tag.rb', line 25

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



33
34
35
36
37
38
# File 'lib/acts_as_taggable_on/tag.rb', line 33

def self.named_any(list)
  clause = list.map { |tag|
    sanitize_sql_for_named_any(tag).force_encoding('BINARY')
  }.join(' OR ')
  where(clause)
end

.named_like(name) ⇒ Object



40
41
42
43
# File 'lib/acts_as_taggable_on/tag.rb', line 40

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



45
46
47
48
49
50
# File 'lib/acts_as_taggable_on/tag.rb', line 45

def self.named_like_any(list)
  clause = list.map { |tag|
    sanitize_sql(["name #{ActsAsTaggableOn::Utils.like_operator} ? ESCAPE '!'", "%#{ActsAsTaggableOn::Utils.escape_like(tag.to_s)}%"])
  }.join(' OR ')
  where(clause)
end

Instance Method Details

#==(object) ⇒ Object

INSTANCE METHODS:



100
101
102
# File 'lib/acts_as_taggable_on/tag.rb', line 100

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

#countObject



108
109
110
# File 'lib/acts_as_taggable_on/tag.rb', line 108

def count
  read_attribute(:count).to_i
end

#to_sObject



104
105
106
# File 'lib/acts_as_taggable_on/tag.rb', line 104

def to_s
  name
end

#validates_name_uniqueness?Boolean

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

Returns:

  • (Boolean)


17
18
19
# File 'lib/acts_as_taggable_on/tag.rb', line 17

def validates_name_uniqueness?
  true
end