Class: ActsAsTaggableOn::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ActsAsTaggableOn::Tag
- Defined in:
- app/models/acts_as_taggable_on/tag.rb
Overview
require_dependency ActsAsTaggableOn::Engine.config.root.join(‘app’, ‘models’, ‘acts_as_taggable_on’, ‘tag.rb’).to_s
Class Method Summary collapse
-
.counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
- .default_per_page ⇒ Object
- .popular(limit = 20, type = nil) ⇒ Object
Instance Method Summary collapse
Class Method Details
.counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
-
:start_at
- restrict the tags to those created after a certain time -
:end_at
- restrict the tags to those created before a certain time -
:at_least
- exclude tags with a frequency less than the given value -
:at_most
- exclude tags with a frequency greater than the given value
Deprecated:
-
:conditions
-
:limit
-
:order
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'app/models/acts_as_taggable_on/tag.rb', line 29 def counts( = {}) .assert_valid_keys :start_at, :end_at, :at_least, :at_most, :conditions, :limit, :order, :joins = select("#{quoted_table_name}.id", "#{quoted_table_name}.name", "COUNT(#{quoted_table_name}.id) AS count") = .joins(:taggings) = .having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) >= ?", [:at_least]]) if [:at_least] = .having(["COUNT(#{ActsAsTaggableOn::Tagging.quoted_table_name}.id) <= ?", [:at_most]]) if [:at_most] = .where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at >= ?", [:start_at]) if [:start_at] = .where("#{ActsAsTaggableOn::Tagging.quoted_table_name}.created_at <= ?", [:end_at]) if [:end_at] = .where([:conditions]) if [:conditions] = .limit([:limit]) if [:limit] = .order([:order]) if [:order] if joins = .delete(:joins) = .joins(joins) end .group("#{quoted_table_name}.id, #{quoted_table_name}.name") end |
.default_per_page ⇒ Object
12 13 14 |
# File 'app/models/acts_as_taggable_on/tag.rb', line 12 def default_per_page 25 end |
.popular(limit = 20, type = nil) ⇒ Object
6 7 8 9 10 |
# File 'app/models/acts_as_taggable_on/tag.rb', line 6 def popular(limit = 20, type = nil) = ActsAsTaggableOn::Tag.counts(:at_least => 0).limit(limit).order('count DESC') = .where("taggings.taggable_type = ?", type.capitalize) if type end |
Instance Method Details
#related_tags(limit = 10) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/acts_as_taggable_on/tag.rb', line 56 def (limit = 10) taggables = self.taggings.limit(10).to_a.collect{|t| t.taggable }.compact tagging_ids = taggables.map{|t| t.taggings.limit(10).map(&:id) }.flatten.uniq return [] if tagging_ids.blank? ActsAsTaggableOn::Tag.where("tags.id != '#{self.id}'") .select("tags.id, tags.name, COUNT(tags.id) as count") .joins(:taggings) .where({:taggings => {:id => tagging_ids }}) .group("tags.id, tags.name") .order("count DESC") .limit(limit) end |
#to_param ⇒ Object
52 53 54 |
# File 'app/models/acts_as_taggable_on/tag.rb', line 52 def to_param URI.escape(URI.escape(self.name), /[\/.?#]/) end |