Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActsAsTaggableOn::ActiveRecord::Backports
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



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

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:



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

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

.named(name) ⇒ Object

SCOPES:



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

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

.named_any(list) ⇒ Object



21
22
23
# File 'lib/acts_as_taggable_on/tag.rb', line 21

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

.named_like(name) ⇒ Object



25
26
27
# File 'lib/acts_as_taggable_on/tag.rb', line 25

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

.named_like_any(list) ⇒ Object



29
30
31
# File 'lib/acts_as_taggable_on/tag.rb', line 29

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:



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

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

#countObject



61
62
63
# File 'lib/acts_as_taggable_on/tag.rb', line 61

def count
  read_attribute(:count).to_i
end

#to_sObject



57
58
59
# File 'lib/acts_as_taggable_on/tag.rb', line 57

def to_s
  name
end