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) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/acts_as_taggable_on/tag.rb', line 53

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:



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

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

SCOPES:



19
20
21
22
23
24
25
# File 'lib/acts_as_taggable_on/tag.rb', line 19

def self.named(name)
  if ActsAsTaggableOn.strict_case_match
    where(["name = #{binary}?", name])
  else
    where(["lower(name) = ?", name.downcase])
  end
end

.named_any(list) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/acts_as_taggable_on/tag.rb', line 27

def self.named_any(list)
  if ActsAsTaggableOn.strict_case_match
    where(list.map { |tag| sanitize_sql(["name = #{binary}?", tag.to_s.mb_chars]) }.join(" OR "))
  else
    where(list.map { |tag| sanitize_sql(["lower(name) = ?", tag.to_s.mb_chars.downcase]) }.join(" OR "))
  end
end

.named_like(name) ⇒ Object



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

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

.named_like_any(list) ⇒ Object



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

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:



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

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

#countObject



78
79
80
# File 'lib/acts_as_taggable_on/tag.rb', line 78

def count
  read_attribute(:count).to_i
end

#to_sObject



74
75
76
# File 'lib/acts_as_taggable_on/tag.rb', line 74

def to_s
  name
end