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



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/acts_as_taggable_on/tag.rb', line 68

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|
    comparable_tag_name = comparable_name(tag_name)
    existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
    begin
      existing_tag || create(name: tag_name)
    rescue ActiveRecord::RecordNotUnique
      # Postgres aborts the current transaction with
      # PG::InFailedSqlTransaction: ERROR:  current transaction is aborted, commands ignored until end of transaction block
      # so we have to rollback this transaction
      raise DuplicateTagError.new("'#{tag_name}' has already been taken")
    end
  end
end

.find_or_create_with_like_by_name(name) ⇒ Object

CLASS METHODS:



60
61
62
63
64
65
66
# File 'lib/acts_as_taggable_on/tag.rb', line 60

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

.named(name) ⇒ Object

SCOPES:



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

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/acts_as_taggable_on/tag.rb', line 32

def self.named_any(list)
  if ActsAsTaggableOn.strict_case_match
    clause = list.map { |tag|
      sanitize_sql(["name = #{binary}?", as_8bit_ascii(tag)])
    }.join(' OR ')
    where(clause)
  else
    clause = list.map { |tag|
      sanitize_sql(['LOWER(name) = LOWER(?)', as_8bit_ascii(unicode_downcase(tag))])
    }.join(' OR ')
    where(clause)
  end
end

.named_like(name) ⇒ Object



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

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



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

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:



91
92
93
# File 'lib/acts_as_taggable_on/tag.rb', line 91

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

#countObject



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

def count
  read_attribute(:count).to_i
end

#to_sObject



95
96
97
# File 'lib/acts_as_taggable_on/tag.rb', line 95

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