Module: ActsAsTaggableOn::Taggable::Collection::ClassMethods
- Defined in:
- lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb
Instance Method Summary collapse
- #acts_as_taggable_on(*args) ⇒ Object
-
#all_tag_counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
-
#all_tags(options = {}) ⇒ Object
Calculate the tag names.
- #initialize_acts_as_taggable_on_collection ⇒ Object
- #safe_to_sql(relation) ⇒ Object
- #tag_counts_on(context, options = {}) ⇒ Object
- #tags_on(context, options = {}) ⇒ Object
Instance Method Details
#acts_as_taggable_on(*args) ⇒ Object
32 33 34 35 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 32 def acts_as_taggable_on(*args) super(*args) initialize_acts_as_taggable_on_collection end |
#all_tag_counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 108 def all_tag_counts( = {}) .assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on, :id scope = {} ## Generate conditions: [:conditions] = sanitize_sql([:conditions]) if [:conditions] start_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at >= ?", .delete(:start_at)]) if [:start_at] end_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", .delete(:end_at)]) if [:end_at] taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name]) taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.taggable_id = ?", [:id]]) if [:id] taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", .delete(:on).to_s]) if [:on] tagging_conditions = [ taggable_conditions, scope[:conditions], start_at_conditions, end_at_conditions ].compact.reverse tag_conditions = [ [:conditions] ].compact.reverse ## Generate joins: taggable_join = "INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{ActsAsTaggableOn::Tagging.table_name}.taggable_id" taggable_join << " AND #{table_name}.#{inheritance_column} = '#{name}'" unless descends_from_active_record? # Current model is STI descendant, so add type checking to the join condition tagging_joins = [ taggable_join, scope[:joins] ].compact tag_joins = [ ].compact ## Generate scope: tagging_scope = ActsAsTaggableOn::Tagging.select("#{ActsAsTaggableOn::Tagging.table_name}.tag_id, COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) AS tags_count") tag_scope = ActsAsTaggableOn::Tag.select("#{ActsAsTaggableOn::Tag.table_name}.*, #{ActsAsTaggableOn::Tagging.table_name}.tags_count AS count").order([:order]).limit([:limit]) # Joins and conditions tagging_joins.each { |join| tagging_scope = tagging_scope.joins(join) } tagging_conditions.each { |condition| tagging_scope = tagging_scope.where(condition) } tag_joins.each { |join| tag_scope = tag_scope.joins(join) } tag_conditions.each { |condition| tag_scope = tag_scope.where(condition) } # GROUP BY and HAVING clauses: at_least = sanitize_sql(["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) >= ?", .delete(:at_least)]) if [:at_least] at_most = sanitize_sql(["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) <= ?", .delete(:at_most)]) if [:at_most] having = ["COUNT(#{ActsAsTaggableOn::Tagging.table_name}.tag_id) > 0", at_least, at_most].compact.join(' AND ') group_columns = "#{ActsAsTaggableOn::Tagging.table_name}.tag_id" unless [:id] # Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore: scoped_select = "#{table_name}.#{primary_key}" tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{safe_to_sql(select(scoped_select))})") end tagging_scope = tagging_scope.group(group_columns).having(having) tag_scope = tag_scope.joins("JOIN (#{safe_to_sql(tagging_scope)}) AS #{ActsAsTaggableOn::Tagging.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id") tag_scope.extending(CalculationMethods) end |
#all_tags(options = {}) ⇒ Object
Calculate the tag names. To be used when you don’t need tag counts and want to avoid the taggable joins.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 56 def ( = {}) .assert_valid_keys :start_at, :end_at, :conditions, :order, :limit, :on ## Generate conditions: [:conditions] = sanitize_sql([:conditions]) if [:conditions] start_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at >= ?", .delete(:start_at)]) if [:start_at] end_at_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.created_at <= ?", .delete(:end_at)]) if [:end_at] taggable_conditions = sanitize_sql(["#{ActsAsTaggableOn::Tagging.table_name}.taggable_type = ?", base_class.name]) taggable_conditions << sanitize_sql([" AND #{ActsAsTaggableOn::Tagging.table_name}.context = ?", .delete(:on).to_s]) if [:on] tagging_conditions = [ taggable_conditions, start_at_conditions, end_at_conditions ].compact.reverse tag_conditions = [ [:conditions] ].compact.reverse ## Generate scope: tagging_scope = ActsAsTaggableOn::Tagging.select("#{ActsAsTaggableOn::Tagging.table_name}.tag_id") tag_scope = ActsAsTaggableOn::Tag.select("#{ActsAsTaggableOn::Tag.table_name}.*").order([:order]).limit([:limit]) # Joins and conditions tagging_conditions.each { |condition| tagging_scope = tagging_scope.where(condition) } tag_conditions.each { |condition| tag_scope = tag_scope.where(condition) } group_columns = "#{ActsAsTaggableOn::Tagging.table_name}.tag_id" # Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore: scoped_select = "#{table_name}.#{primary_key}" tagging_scope = tagging_scope.where("#{ActsAsTaggableOn::Tagging.table_name}.taggable_id IN(#{safe_to_sql(select(scoped_select))})").group(group_columns) tag_scope = tag_scope.joins("JOIN (#{safe_to_sql(tagging_scope)}) AS #{ActsAsTaggableOn::Tagging.table_name} ON #{ActsAsTaggableOn::Tagging.table_name}.tag_id = #{ActsAsTaggableOn::Tag.table_name}.id") tag_scope.extending(CalculationMethods) end |
#initialize_acts_as_taggable_on_collection ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 10 def initialize_acts_as_taggable_on_collection tag_types.map(&:to_s).each do |tag_type| class_eval <<-RUBY, __FILE__, __LINE__ + 1 def self.#{tag_type.singularize}_counts(options={}) tag_counts_on('#{tag_type}', options) end def #{tag_type.singularize}_counts(options = {}) tag_counts_on('#{tag_type}', options) end def top_#{tag_type}(limit = 10) tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i) end def self.top_#{tag_type}(limit = 10) tag_counts_on('#{tag_type}', :order => 'count desc', :limit => limit.to_i) end RUBY end end |
#safe_to_sql(relation) ⇒ Object
176 177 178 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 176 def safe_to_sql(relation) connection.respond_to?(:unprepared_statement) ? connection.unprepared_statement{relation.to_sql} : relation.to_sql end |
#tag_counts_on(context, options = {}) ⇒ Object
37 38 39 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 37 def tag_counts_on(context, = {}) all_tag_counts(.merge({:on => context.to_s})) end |
#tags_on(context, options = {}) ⇒ Object
41 42 43 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/collection.rb', line 41 def (context, = {}) (.merge({:on => context.to_s})) end |