Module: ActiveRecord::Acts::Taggable::SingletonMethods
- Defined in:
- lib/acts_as_taggable.rb
Instance Method Summary collapse
- #caching_tag_list? ⇒ Boolean
- #find_options_for_find_tagged_with(tags, options = {}) ⇒ Object
- #find_options_for_tag_counts(options = {}) ⇒ Object
-
#find_related_tags(tags, options = {}) ⇒ Object
Returns an array of related tags.
-
#find_tagged_with(*args) ⇒ Object
Pass either a tag, string, or an array of strings or tags.
- #joins_for_match_all_tags(tags) ⇒ Object
-
#tag_counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
Instance Method Details
#caching_tag_list? ⇒ Boolean
159 160 161 |
# File 'lib/acts_as_taggable.rb', line 159 def caching_tag_list? column_names.include?(cached_tag_list_column_name) end |
#find_options_for_find_tagged_with(tags, options = {}) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/acts_as_taggable.rb', line 71 def (, = {}) = .is_a?(Array) ? TagList.new(.map(&:to_s)) : TagList.from() = .dup return {} if .empty? conditions = [] conditions << sanitize_sql(.delete(:conditions)) if [:conditions] taggings_alias, = "#{table_name}_taggings", "#{table_name}_tags" joins = [ "INNER JOIN #{Tagging.table_name} #{taggings_alias} ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)}", "INNER JOIN #{Tag.table_name} #{} ON #{}.id = #{taggings_alias}.tag_id" ] if .delete(:exclude) conditions << <<-END #{table_name}.id NOT IN (SELECT #{Tagging.table_name}.taggable_id FROM #{Tagging.table_name} INNER JOIN #{Tag.table_name} ON #{Tagging.table_name}.tag_id = #{Tag.table_name}.id WHERE #{()} AND #{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)}) END else if .delete(:match_all) joins << () else conditions << (, ) end end { :select => "DISTINCT #{table_name}.*", :joins => joins.join(" "), :conditions => conditions.join(" AND ") }.reverse_merge!() end |
#find_options_for_tag_counts(options = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/acts_as_taggable.rb', line 137 def ( = {}) = .dup scope = scope(:find) conditions = [] conditions << send(:sanitize_conditions, .delete(:conditions)) if [:conditions] conditions << send(:sanitize_conditions, scope[:conditions]) if scope && scope[:conditions] conditions << "#{Tagging.table_name}.taggable_type = #{quote_value(base_class.name)}" conditions << type_condition unless descends_from_active_record? conditions.compact! conditions = conditions.join(" AND ") joins = ["INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{Tagging.table_name}.taggable_id"] joins << .delete(:joins) if [:joins] joins << scope[:joins] if scope && scope[:joins] joins = joins.join(" ") = { :conditions => conditions, :joins => joins }.update() Tag.() end |
#find_related_tags(tags, options = {}) ⇒ Object
Returns an array of related tags. Related tags are all the other tags that are found on the models tagged with the provided tags.
Pass either a tag, string, or an array of strings or tags.
Options:
:order - SQL Order how to order the tags. Defaults to "count DESC, tags.name".
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/acts_as_taggable.rb', line 41 def (, = {}) = .is_a?(Array) ? TagList.new(.map(&:to_s)) : TagList.from() = find_tagged_with() return [] if .blank? = .to_s(:db) Tag.find(:all, .merge({ :select => "#{Tag.table_name}.*, COUNT(#{Tag.table_name}.id) AS count", :joins => "JOIN #{Tagging.table_name} ON #{Tagging.table_name}.taggable_type = '#{base_class.name}' AND #{Tagging.table_name}.taggable_id IN (#{}) AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id", :order => [:order] || "count DESC, #{Tag.table_name}.name", :group => "#{Tag.table_name}.id, #{Tag.table_name}.name HAVING #{Tag.table_name}.name NOT IN (#{.map { |n| quote_value(n) }.join(",")})" })) end |
#find_tagged_with(*args) ⇒ Object
Pass either a tag, string, or an array of strings or tags.
Options:
:exclude - Find models that are not tagged with the given tags
:match_all - Find models that match all of the given tags, not just one
:conditions - A piece of SQL conditions to add to the query
66 67 68 69 |
# File 'lib/acts_as_taggable.rb', line 66 def find_tagged_with(*args) = (*args) .blank? ? [] : find(:all, ) end |
#joins_for_match_all_tags(tags) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/acts_as_taggable.rb', line 108 def () joins = [] .each_with_index do |tag, index| taggings_alias, = "taggings_#{index}", "tags_#{index}" join = <<-END INNER JOIN #{Tagging.table_name} #{taggings_alias} ON #{taggings_alias}.taggable_id = #{table_name}.#{primary_key} AND #{taggings_alias}.taggable_type = #{quote_value(base_class.name)} INNER JOIN #{Tag.table_name} #{} ON #{taggings_alias}.tag_id = #{}.id AND #{}.name = ? END joins << sanitize_sql([join, tag]) end joins.join(" ") end |
#tag_counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
See Tag.counts for available options.
133 134 135 |
# File 'lib/acts_as_taggable.rb', line 133 def tag_counts( = {}) Tag.find(:all, ()) end |