Module: ActsAsTaggable::ClassMethods

Defined in:
lib/acts_as_taggable/acts_as_taggable.rb

Instance Method Summary collapse

Instance Method Details

#applied_tag_namesObject



56
57
58
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 56

def applied_tag_names
  applied_tags.pluck(:name)
end

#applied_tagsObject

Make it possible to ask for tags on a scoped Taggable relation. e.g. Users.online.applied_tags



52
53
54
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 52

def applied_tags
  Tag.select("#{Tag.table_name}.*, COUNT(*) AS count").joins(:taggings).where(:taggable_type => self.name, Tagging.table_name => {:taggable_id => all}).group("#{Tag.table_name}.id")
end

#create_tag(tag_name) ⇒ Object



68
69
70
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 68

def create_tag(tag_name)
  find_tags(tag_name).first || tags.create!(:name => tag_name)
end

#find_tags(*input) ⇒ Object

Given an unsanitized string or list of tags, Returns a list of tags



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 73

def find_tags(*input)
  input = input.flatten.compact
  input = input.first if input.one?
  case input
  when Tag
    HelperMethods.filter_tags_by_current_tag_scope([input])
  when String
    tags.where(:name => input.split(acts_as_taggable_options[:delimiter]).collect {|tag_name| tag_name.strip}).to_a
    tags.where(:name => input.split(acts_as_taggable_options[:delimiter]).collect {|tag_name| sanitize_tag_name(tag_name) }).to_a
  when Array
    input.flat_map {|tag| find_tags(tag)}.select(&:present?).uniq
  when ActiveRecord::Relation
    input.distinct.to_a
  else
    []
  end
end

#sanitize_tag_name(tag_name) ⇒ Object



91
92
93
94
95
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 91

def sanitize_tag_name(tag_name)
  sanitized_tag_name = tag_name.to_s.squish
  sanitized_tag_name.downcase! if acts_as_taggable_options[:downcase]
  sanitized_tag_name
end

#tag_namesObject



64
65
66
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 64

def tag_names
  tags.pluck(:name)
end

#tagged_with_all(*tags) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 41

def tagged_with_all(*tags)
  tags = find_tags(tags)
  return none if tags.empty?

  tags.inject(all.distinct) do |scope, tag|
    scope = scope.joins "LEFT OUTER JOIN #{Tagging.table_name} AS alias_#{tag.id} ON alias_#{tag.id}.taggable_id = #{table_name}.id"
    scope = scope.where "alias_#{tag.id}.tag_id" => tag
  end
end

#tagged_with_any(*tags) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 29

def tagged_with_any(*tags)
  tags = find_tags(tags)
  return none if tags.empty?

  table_alias = "alias_#{tags.hash.abs}"
  scope = all.distinct.select "#{table_name}.*"
  scope = scope.joins "JOIN #{Tagging.table_name} AS #{table_alias} ON #{table_alias}.taggable_id = #{table_name}.id"
  scope = scope.where "#{table_alias}.tag_id" => tags

  return scope
end

#tagsObject



60
61
62
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 60

def tags
  Tag.where(:taggable_type => name)
end