Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/tagger/tag.rb

Constant Summary collapse

SEPARATORS =
{
  :comma => ",",
  :space => " "
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#weightObject (readonly)

Returns the value of attribute weight.



8
9
10
# File 'lib/tagger/tag.rb', line 8

def weight
  @weight
end

Class Method Details

.cloud(type, options = {}) ⇒ Object

Tag.cloud(:post) Tag.cloud(:post, :scope => @category) Tag.cloud(:post, :limit => 50)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tagger/tag.rb', line 13

def self.cloud(type, options = {})
  options[:limit] ||= 100

  scope_condition = "
    AND scopable_type = #{options[:scope].class.to_s.inspect}
    AND scopable_id = #{options[:scope].id}" if options[:scope]

  tags = Tag.find_by_sql [%(
    SELECT tags.id, tags.name, COUNT(*) AS total
    FROM tags, taggings
    WHERE
      tags.id == taggings.tag_id AND
      taggings.taggable_type = ?
      #{scope_condition}
    GROUP BY taggings.tag_id
    ORDER BY total DESC, name ASC
    LIMIT ?
  ), type.to_s.classify, options[:limit]]
end

.parse(tag_list, separator) ⇒ Object

Tag.parse(tags_list, :comma) Tag.parse(tags_list, :space)



35
36
37
38
39
40
41
42
# File 'lib/tagger/tag.rb', line 35

def self.parse(tag_list, separator)
  separator = SEPARATORS[separator] || SEPARATORS[:comma]
  tags = []
  tag_list = tag_list.dup
  tag_list.gsub!(/(["'])(.*?)(\1)/m) {|m| tags << $2; "" }
  tags += tag_list.split(separator)
  tags.collect(&:squish).reject(&:blank?).sort_by {|tag| tag.downcase}
end

Instance Method Details

#to_sObject



48
49
50
# File 'lib/tagger/tag.rb', line 48

def to_s
  name
end

#totalObject



44
45
46
# File 'lib/tagger/tag.rb', line 44

def total
  read_attribute(:total).to_i
end