Class: ActsAsTaggableOn::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Utils
Defined in:
lib/acts_as_taggable_on/tag.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utils

included

Class Method Details

.find_or_create_all_with_like_by_name(*list, owner, context) ⇒ Object



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

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

  return [] if list.empty?

  existing_tags = Tag.named_any(list, owner, context).all
  new_tag_names = list.reject do |name|
                    name = comparable_name(name)
                    existing_tags.any? { |tag| comparable_name(tag.name) == name }
                  end
  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(["lower(name) = ?", name.downcase])
end

.named_any(list, owner, context) ⇒ Object



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

def self.named_any(list, owner, context)
  where(list.map { |tag| sanitize_sql(["lower(name) = ? and tagger_id = ? and context = ?", tag.to_s.downcase, owner, context]) }.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_operator} ? ESCAPE '!'", "%#{escape_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_operator} ? ESCAPE '!'", "%#{escape_like(tag.to_s)}%"]) }.join(" OR "))
end

Instance Method Details

#==(object) ⇒ Object

INSTANCE METHODS:



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

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

#countObject



66
67
68
# File 'lib/acts_as_taggable_on/tag.rb', line 66

def count
  read_attribute(:count).to_i
end

#to_sObject



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

def to_s
  name
end