Class: MetaTag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/meta_tag.rb

Constant Summary collapse

DELIMITER =
delim

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloud(options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/meta_tag.rb', line 38

def cloud(options = {})
  options = {:by => 'popularity', :order => 'asc', :limit => 5}.merge(options)
  validate_by options[:by]
  validate_order options[:order]
  all(
    :select => 'meta_tags.*, count(*) as popularity',
    :limit => options[:limit],
    :joins => "JOIN taggings ON taggings.meta_tag_id = meta_tags.id",
    :conditions => options[:conditions],
    :group => "meta_tags.id, meta_tags.name",
    :order => "#{options[:by]} #{options[:order].upcase}"
  )
end

.find_or_create_by_name!(name) ⇒ Object



34
35
36
# File 'app/models/meta_tag.rb', line 34

def find_or_create_by_name!(name)
  find_by_name(name) || create!(:name => name)
end

.validate_by(by) ⇒ Object



52
53
54
55
# File 'app/models/meta_tag.rb', line 52

def validate_by(by)
  valid_field_names = column_names + ['popularity']
  valid_field_names.include?(by) or raise %Q{"by" must be one of #{valid_field_names.to_sentence}, was #{by}}
end

.validate_order(order) ⇒ Object



57
58
59
# File 'app/models/meta_tag.rb', line 57

def validate_order(order)
  order =~ /^(asc|desc)$/i or raise %Q{"order" must be set to either "asc" or "desc"}
end

Instance Method Details

#<=>(other) ⇒ Object



62
63
64
65
# File 'app/models/meta_tag.rb', line 62

def <=>(other)
  # To be able to sort an array of tags
  name <=> other.name
end

#after_saveObject



28
29
30
31
# File 'app/models/meta_tag.rb', line 28

def after_save
  # if you allow editable tag names, you might want before_save instead 
  self.name = name.downcase.strip.squeeze(" ")
end