Module: ActsAsTaggableOnPadrino::Taggable::Collection::ClassMethods

Defined in:
lib/acts_as_taggable_on_padrino/taggable/collection.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_taggable_on(*args) ⇒ Object



33
34
35
36
# File 'lib/acts_as_taggable_on_padrino/taggable/collection.rb', line 33

def acts_as_taggable_on(*args)
  super
  initialize_acts_as_taggable_on_collection
end

#all_tag_counts(options = {}) ⇒ Object

Calculate the tag counts for all tags.

Parameters:

  • options (Hash) (defaults to: {})

    Options:

    • :start_at - Restrict the tags to those created after a certain time

    • :end_at - Restrict the tags to those created before a certain time

    • :conditions - A piece of SQL conditions to add to the query

    • :limit - The maximum number of tags to return

    • :order - A piece of SQL to order by. Eg ‘tags.count desc’ or ‘taggings.created_at desc’

    • :at_least - Exclude tags with a frequency less than the given value

    • :at_most - Exclude tags with a frequency greater than the given value

    • :on - Scope the find to only include a certain context



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

  ## Generate scope:
  tagging_scope = ActsAsTaggableOnPadrino::Tagging.select("#{tagging_table_name}.tag_id, COUNT(#{tagging_table_name}.tag_id) AS tags_count").
      joins("INNER JOIN #{table_name} ON #{table_name}.#{primary_key} = #{tagging_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? # Current model is STI descendant, so add type checking to the join condition
  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(["#{tagging_table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
  tagging_scope = tagging_scope.where(["#{tagging_table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]

  tag_scope = ActsAsTaggableOnPadrino::Tag.select("#{tag_table_name}.*, #{tagging_table_name}.tags_count AS count").order(options[:order]).limit(options[:limit])
  tag_scope.where(options[:conditions]) if options[:conditions]

  # GROUP BY and HAVING clauses:
  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(#{tagging_table_name}.tag_id) > 0", at_least, at_most].compact.join(' AND ')

  # Append the current scope to the scope, because we can't use scope(:find) in RoR 3.0 anymore:
  tagging_scope = tagging_scope.where(:taggable_id => select("#{table_name}.#{primary_key}")).
                                group("#{tagging_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

#initialize_acts_as_taggable_on_collectionObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/acts_as_taggable_on_padrino/taggable/collection.rb', line 10

def initialize_acts_as_taggable_on_collection
  tag_types.each do |tag_type|
    singular_tag_type = tag_type.to_s.singularize
    class_eval %(
      def self.#{singular_tag_type}_counts(options={})
        tag_counts_on('#{tag_type}', options)
      end

      def #{singular_tag_type}_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
    )
  end
end

#tag_counts_on(context, options = {}) ⇒ Object



38
39
40
# File 'lib/acts_as_taggable_on_padrino/taggable/collection.rb', line 38

def tag_counts_on(context, options = {})
  all_tag_counts(options.merge({:on => context.to_s}))
end