Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActsAsTaggableOn::ActiveRecord::Backports, TagCustomisations, TagValidations
Defined in:
lib/acts_as_taggable_on/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActsAsTaggableOn::ActiveRecord::Backports

included

Class Method Details

.find_or_create_all_with_like_by_name(*list) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/acts_as_taggable_on/tag.rb', line 41

def self.find_or_create_all_with_like_by_name(*list)
  list = [list].flatten

  return [] if list.empty?

  existing_tags = Tag.named_any(list).all
  new_tag_names = list.reject { |name| existing_tags.any? { |tag| tag.name.mb_chars.downcase == name.mb_chars.downcase } }
  created_tags  = new_tag_names.map { |name| Tag.create(:name => name) }

  existing_tags + created_tags
end

.find_or_create_with_like_by_name(name) ⇒ Object

CLASS METHODS:



37
38
39
# File 'lib/acts_as_taggable_on/tag.rb', line 37

def self.find_or_create_with_like_by_name(name)
  named_like(name).first || create(:name => name)
end

.named(name) ⇒ Object

SCOPES:



19
20
21
# File 'lib/acts_as_taggable_on/tag.rb', line 19

def self.named(name)
  where(["name LIKE ?", name])
end

.named_any(list) ⇒ Object



23
24
25
# File 'lib/acts_as_taggable_on/tag.rb', line 23

def self.named_any(list)
  where(list.map { |tag| sanitize_sql(["name LIKE ?", tag.to_s]) }.join(" OR "))
end

.named_like(name) ⇒ Object



27
28
29
# File 'lib/acts_as_taggable_on/tag.rb', line 27

def self.named_like(name)
  where(["name LIKE ?", "%#{name}%"])
end

.named_like_any(list) ⇒ Object



31
32
33
# File 'lib/acts_as_taggable_on/tag.rb', line 31

def self.named_like_any(list)
  where(list.map { |tag| sanitize_sql(["name LIKE ?", "%#{tag.to_s}%"]) }.join(" OR "))
end

Instance Method Details

#==(object) ⇒ Object

INSTANCE METHODS:



55
56
57
# File 'lib/acts_as_taggable_on/tag.rb', line 55

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

#countObject



63
64
65
# File 'lib/acts_as_taggable_on/tag.rb', line 63

def count
  read_attribute(:count).to_i
end

#to_sObject



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

def to_s
  name
end