Class | Tag |
In: |
app/models/tag.rb
|
Parent: | ActiveRecord::Base |
size | [RW] |
Returns an array of tags with a size attribute This takes the same arguments as find, plus the additional `:sizes` option, which contols the number of sizes the tag cloud will have. The default number of sizes is 5.
# File app/models/tag.rb, line 26 26: def self.cloud(options={}) 27: sizes = (options.delete(:sizes) || 5) - 1 28: sizes = 1 if sizes < 1 29: tags = counts(options) 30: return [] if tags.blank? 31: 32: min = nil 33: max = nil 34: tags.each do |t| 35: t.count = t.count.to_i 36: min = t.count if (min.nil? || t.count < min) 37: max = t.count if (max.nil? || t.count > min) 38: end 39: 40: divisor = ((max - min) / sizes) + 1 41: tags.each do |t| 42: t.size = ("%1.0f" % (t.count * 1.0 / divisor)).to_i 43: end 44: 45: tags 46: end
# File app/models/tag.rb, line 52 52: def self.columns_for_index 53: [ {:label => "Name", :method => :name, :order => "name" }, 54: {:label => "Usages", :method => :tagging_count }, 55: {:label => "Updated On", :method => :updated_on_string, :order => "updated_at"} ] 56: end
Returns an array of tags with a count attribute
# File app/models/tag.rb, line 12 12: def self.counts(options={}) 13: with_scope(:find => { 14: :select => "tags.id, tags.name, count(*) as count", 15: :joins => :taggings, 16: :group => "tags.id, tags.name", 17: :order => "count desc, tags.name" }) do 18: all(options) 19: end 20: end
# File app/models/tag.rb, line 58 58: def render 59: @taggings = @content_block.taggings.paginate(:page => params[:page]) 60: end