Class: Tag

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.counts(options = {}) ⇒ Object

Calculate the tag counts for all tags.

:start_at - Restrict the tags to those created after a certain time
:end_at - Restrict the tags to those created before a certain time
:conditions - A piece of SQL conditions to add to the query
:limit - The maximum number of tags to return
:order - A piece of SQL to order by. Eg 'count desc' or 'taggings.created_at desc'
:at_least - Exclude tags with a frequency less than the given value
:at_most - Exclude tags with a frequency greater than the given value


36
37
38
# File 'lib/tag.rb', line 36

def counts(options = {})
  find(:all, options_for_counts(options))
end

.find_or_create_with_like_by_name(name) ⇒ Object

LIKE is used for cross-database case-insensitivity



11
12
13
# File 'lib/tag.rb', line 11

def self.find_or_create_with_like_by_name(name)
  find(:first, :conditions => ["name LIKE ?", name]) || create(:name => name)
end

.options_for_counts(options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tag.rb', line 40

def options_for_counts(options = {})
  options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :joins
  options = options.dup
  
  start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
  end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]
  
  conditions = [
    (sanitize_sql(options.delete(:conditions)) if options[:conditions]),
    start_at,
    end_at
  ].compact
  
  conditions = conditions.join(' AND ') if conditions.any?
  
  joins = ["INNER JOIN #{Tagging.table_name} ON #{Tag.table_name}.id = #{Tagging.table_name}.tag_id"]
  joins << options.delete(:joins) if options[:joins]
  
  at_least  = sanitize_sql(['COUNT(*) >= ?', options.delete(:at_least)]) if options[:at_least]
  at_most   = sanitize_sql(['COUNT(*) <= ?', options.delete(:at_most)]) if options[:at_most]
  having    = [at_least, at_most].compact.join(' AND ')
  group_by  = "#{Tag.table_name}.id, #{Tag.table_name}.name HAVING COUNT(*) > 0"
  group_by << " AND #{having}" unless having.blank?
  
  { :select     => "#{Tag.table_name}.id, #{Tag.table_name}.name, COUNT(*) AS count", 
    :joins      => joins.join(" "),
    :conditions => conditions,
    :group      => group_by
  }.update(options)
end

Instance Method Details

#==(object) ⇒ Object



15
16
17
# File 'lib/tag.rb', line 15

def ==(object)
  super || (object.is_a?(Tag) && name == object.name)
end

#countObject



23
24
25
# File 'lib/tag.rb', line 23

def count
  read_attribute(:count).to_i
end

#to_sObject



19
20
21
# File 'lib/tag.rb', line 19

def to_s
  name
end