Class: ActsAsTaggableOn::Tag

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecord::Backports

included

Class Method Details

.find_or_create_all_with_like_by_name(*list) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/acts_as_taggable_on/tag.rb', line 44

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 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:



40
41
42
# File 'lib/acts_as_taggable_on/tag.rb', line 40

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

.named(name) ⇒ Object



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

def self.named(name)
  where(["name #{like_operator} ?", name])
end

.named_any(list) ⇒ Object



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

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

.named_like(name) ⇒ Object



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

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

.named_like_any(list) ⇒ Object



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

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

.using_postgresql?Boolean

SCOPES:

Returns:

  • (Boolean)


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

def self.using_postgresql?
  connection.adapter_name == 'PostgreSQL'
end

Instance Method Details

#==(object) ⇒ Object

INSTANCE METHODS:



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

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

#countObject



69
70
71
# File 'lib/acts_as_taggable_on/tag.rb', line 69

def count
  read_attribute(:count).to_i
end

#to_sObject



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

def to_s
  name
end