54
55
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
|
# File 'lib/acts_as_taggable_on_padrino/taggable/collection.rb', line 54
def all_tag_counts(options = {})
options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :on, :id
tagging_scope = acts_as_taggable_on_tagging_model.select("#{acts_as_taggable_on_tagging_model.table_name}.tag_id, COUNT(#{acts_as_taggable_on_tagging_model.table_name}.tag_id) AS tags_count").
joins("INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{acts_as_taggable_on_tagging_model.table_name}.taggable_id").
where(:taggable_type => base_class.name)
tagging_scope = tagging_scope.where(table_name => {inheritance_column => name}) unless descends_from_active_record? tagging_scope = tagging_scope.where(:taggable_id => options.delete(:id)) if options[:id]
tagging_scope = tagging_scope.where(:context => options.delete(:on).to_s) if options[:on]
tagging_scope = tagging_scope.where(["#{acts_as_taggable_on_tagging_model.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
tagging_scope = tagging_scope.where(["#{acts_as_taggable_on_tagging_model.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
tag_scope = acts_as_taggable_on_tag_model.select("#{acts_as_taggable_on_tag_model.table_name}.*, #{acts_as_taggable_on_tagging_model.table_name}.tags_count AS count").order(options[:order]).limit(options[:limit])
tag_scope.where(options[:conditions]) if options[:conditions]
at_least = sanitize_sql(['tags_count >= ?', options.delete(:at_least)]) if options[:at_least]
at_most = sanitize_sql(['tags_count <= ?', options.delete(:at_most)]) if options[:at_most]
having = ["COUNT(#{acts_as_taggable_on_tagging_model.table_name}.tag_id) > 0", at_least, at_most].compact.join(' AND ')
tagging_scope = tagging_scope.where(:taggable_id => select("#{table_name}.#{primary_key}")).
group("#{acts_as_taggable_on_tagging_model.table_name}.tag_id").
having(having)
tag_scope = tag_scope.joins("JOIN (#{tagging_scope.to_sql}) AS taggings ON taggings.tag_id = tags.id")
tag_scope
end
|