Class: Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tag
- Defined in:
- lib/tag.rb
Class Method Summary collapse
-
.counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
-
.find_or_create_with_like_by_name(name) ⇒ Object
LIKE is used for cross-database case-insensitivity.
- .options_for_counts(options = {}) ⇒ Object
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( = {}) find(:all, ()) 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 ( = {}) .assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :joins = .dup start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", .delete(:start_at)]) if [:start_at] end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", .delete(:end_at)]) if [:end_at] conditions = [ (sanitize_sql(.delete(:conditions)) if [: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 << .delete(:joins) if [:joins] at_least = sanitize_sql(['COUNT(*) >= ?', .delete(:at_least)]) if [:at_least] at_most = sanitize_sql(['COUNT(*) <= ?', .delete(:at_most)]) if [: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() 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 |
#count ⇒ Object
23 24 25 |
# File 'lib/tag.rb', line 23 def count read_attribute(:count).to_i end |
#to_s ⇒ Object
19 20 21 |
# File 'lib/tag.rb', line 19 def to_s name end |