Class: Cms::Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
DefaultAccessible
Defined in:
app/models/cms/tag.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sizeObject

Returns the value of attribute size



9
10
11
# File 'app/models/cms/tag.rb', line 9

def size
  @size
end

Class Method Details

.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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/cms/tag.rb', line 30

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

.columns_for_indexObject



56
57
58
59
60
# File 'app/models/cms/tag.rb', line 56

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

.counts(options = {}) ⇒ Array

Returns an array of tags with a count attribute

Returns:

  • (Array)

    Each element of the area contains [Id (Integer), Name (String), count (Integer)] (with Sqlite3 anyway)



16
17
18
19
20
21
22
23
24
# File 'app/models/cms/tag.rb', line 16

def self.counts(options={})
  with_scope(:find => {
      :select => "#{Tag.table_name}.id, #{Tag.table_name}.name, count(*) as count",
      :joins => :taggings,
      :group => "#{Tag.table_name}.id, #{Tag.table_name}.name",
      :order => "count desc, #{Tag.table_name}.name"}) do
    all(options)
  end
end

Instance Method Details

#renderObject



62
63
64
# File 'app/models/cms/tag.rb', line 62

def render
  @taggings = @content_block.taggings.paginate(:page => params[:page])
end

#tagging_countObject



52
53
54
# File 'app/models/cms/tag.rb', line 52

def tagging_count
  taggings.count
end