Class: MetaTag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MetaTag
- Defined in:
- app/models/meta_tag.rb
Constant Summary collapse
- DELIMITER =
delim
Class Method Summary collapse
- .cloud(options = {}) ⇒ Object
- .find_or_create_by_name!(name) ⇒ Object
- .validate_by(by) ⇒ Object
- .validate_order(order) ⇒ Object
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( = {}) = {:by => 'popularity', :order => 'asc', :limit => 5}.merge() validate_by [:by] validate_order [:order] all( :select => 'meta_tags.*, count(*) as popularity', :limit => [:limit], :joins => "JOIN taggings ON taggings.meta_tag_id = meta_tags.id", :conditions => [:conditions], :group => "meta_tags.id, meta_tags.name", :order => "#{[:by]} #{[: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_save ⇒ Object
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 |