Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/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

  • :at_least - exclude tags with a frequency less than the given value

  • :at_most - exclude tags with a frequency greater than the given value

Deprecated:

  • :conditions

  • :limit

  • :order



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/models/tag.rb', line 42

def counts(options = {})
  options.assert_valid_keys :start_at, :end_at, :at_least, :at_most, :conditions, :limit, :order, :joins
  
  tags = joins(:taggings)
  tags = tags.having(['count >= ?', options[:at_least]]) if options[:at_least]
  tags = tags.having(['count <= ?', options[:at_most]])  if options[:at_most]
  tags = tags.where("#{Tagging.quoted_table_name}.created_at >= ?", options[:start_at]) if options[:start_at]
  tags = tags.where("#{Tagging.quoted_table_name}.created_at <= ?", options[:end_at])   if options[:end_at]
  
  # TODO: deprecation warning
  tags = tags.where(options[:conditions]) if options[:conditions]
  tags = tags.limit(options[:limit])      if options[:limit]
  tags = tags.order(options[:order])      if options[:order]
  
  if joins = options.delete(:joins)
    tags = tags.joins(joins)
  end
  
  tags.select("#{quoted_table_name}.id, #{quoted_table_name}.name, COUNT(#{quoted_table_name}.id) AS count").group('tags.id, tags.name')
end

.find_or_create_with_like_by_name(name) ⇒ Object



25
26
27
# File 'app/models/tag.rb', line 25

def find_or_create_with_like_by_name(name)
  where(arel_table[:name].matches(name)).first || create(:name => name)
end

Instance Method Details

#==(object) ⇒ Object



12
13
14
# File 'app/models/tag.rb', line 12

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

#countObject



20
21
22
# File 'app/models/tag.rb', line 20

def count
  read_attribute(:count).to_i
end

#to_sObject



16
17
18
# File 'app/models/tag.rb', line 16

def to_s
  name
end