Sha256: 905209b9c0f8a0c7c29b68e84cc7152c4c7100182c11f9f9726b800176f77e76

Contents?: true

Size: 1.62 KB

Versions: 22

Compression:

Stored size: 1.62 KB

Contents

class Tag < ActiveRecord::Base
  
  has_many :taggings
  
  validates_uniqueness_of :name
  
  attr_accessor :size
  
  named_scope :named, lambda{|tag| {:conditions => ["tags.name = ? ", tag]} }
  
  # Returns an array of tags with a count attribute
  def self.counts(options={})
    with_scope(:find => { 
        :select => "tags.id, tags.name, count(*) as count", 
        :joins => :taggings, 
        :group => "tags.id, tags.name", 
        :order => "count desc, tags.name" }) do
      all(options)
    end
  end
  
  # 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.
  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
  
  def tagging_count
    taggings.count
  end
  
  def self.columns_for_index
    [ {:label => "Name", :method => :name, :order => "name" },
      {:label => "Usages", :method => :tagging_count },
      {:label => "Updated On", :method => :updated_on_string, :order => "updated_at"}  ]
  end  
  
  def render
    @taggings = @content_block.taggings.paginate(:page => params[:page])
  end  
  
end

Version data entries

22 entries across 22 versions & 8 rubygems

Version Path
SFEley-browsercms-3.0.2 app/models/tag.rb
buzzware-browsercms-3.0.2 app/models/tag.rb
coredumplings-browsercms-3.0.0 app/models/tag.rb
nate-browsercms-3.0.210 app/models/tag.rb
nate-browsercms-3.0.211 app/models/tag.rb
we5-browsercms-3.0.1.1 app/models/tag.rb
webficient-browsercms-3.0.1 app/models/tag.rb
webficient-browsercms-3.0.2 app/models/tag.rb
webficient-browsercms-3.0.3 app/models/tag.rb
webficient-browsercms-3.0.4 app/models/tag.rb
browsercms-3.0.5 app/models/tag.rb
we5-browsercms-3.0.5.1 app/models/tag.rb
we5-browsercms-3.0.5 app/models/tag.rb
browsercms_s3-3.0.5 app/models/tag.rb
browsercms-3.0.4 app/models/tag.rb
browsercms-3.0.3 app/models/tag.rb
browsercms_s3-3.0.4 app/models/tag.rb
browsercms_s3-3.0.3 app/models/tag.rb
we5-browsercms-3.0.2 app/models/tag.rb
browsercms-3.0.2 app/models/tag.rb