Module: ActsAsTaggable::ClassMethods
- Defined in:
- lib/acts_as_taggable/acts_as_taggable.rb
Instance Method Summary collapse
- #applied_tag_names ⇒ Object
-
#applied_tags ⇒ Object
Make it possible to ask for tags on a scoped Taggable relation.
- #create_tag(tag_name) ⇒ Object
-
#find_tags(*input) ⇒ Object
Given an unsanitized string or list of tags, Returns a list of tags.
- #sanitize_tag_name(tag_name) ⇒ Object
- #tag_names ⇒ Object
- #tagged_with_all(*tags) ⇒ Object
- #tagged_with_any(*tags) ⇒ Object
- #tags ⇒ Object
Instance Method Details
#applied_tag_names ⇒ Object
56 57 58 |
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 56 def applied_tag_names .pluck(:name) end |
#applied_tags ⇒ Object
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 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) (tag_name).first || .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 (*input) input = input.flatten.compact input = input.first if input.one? case input when Tag HelperMethods.([input]) when String .where(:name => input.split([:delimiter]).collect {|tag_name| tag_name.strip}).to_a .where(:name => input.split([:delimiter]).collect {|tag_name| sanitize_tag_name(tag_name) }).to_a when Array input.flat_map {|tag| (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 [:downcase] sanitized_tag_name end |
#tag_names ⇒ Object
64 65 66 |
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 64 def tag_names .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(*) = () return none if .empty? .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(*) = () return none if .empty? table_alias = "alias_#{.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" => return scope end |
#tags ⇒ Object
60 61 62 |
# File 'lib/acts_as_taggable/acts_as_taggable.rb', line 60 def Tag.where(:taggable_type => name) end |