Method: Tag.cloud

Defined in:
app/models/tag.rb

.cloud(options = {}) ⇒ Object

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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/tag.rb', line 26

def self.cloud(options={})
  sizes = (options.delete(:sizes) || 5) - 1
  sizes = 1 if sizes < 1
  tags = counts(options)
  return [] if tags.blank?
  
  min = nil
  max = nil
  tags.each do |t|
    t.count = t.count.to_i
    min = t.count if (min.nil? || t.count < min)
    max = t.count if (max.nil? || t.count > min)
  end

  divisor = ((max - min) / sizes) + 1
  tags.each do |t|
    t.size = ("%1.0f" % (t.count * 1.0 / divisor)).to_i
  end
  
  tags
end