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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/acts_as_taggable_on/tag.rb', line 63

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:



55
56
57
58
59
60
61
# File 'lib/acts_as_taggable_on/tag.rb', line 55

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



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 { |tag|
    sanitize_sql_for_named_any(tag).force_encoding('BINARY')
  }.join(' OR ')
  where(clause)
end

.named_like(name) ⇒ Object



41
42
43
44
# 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



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

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:



86
87
88
# File 'lib/acts_as_taggable_on/tag.rb', line 86

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

#countObject



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

def count
  read_attribute(:count).to_i
end

#to_sObject



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

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