Class: Redmineup::ActsAsTaggable::Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Redmineup::ActsAsTaggable::Tag
- Defined in:
- lib/redmineup/acts_as_taggable/tag.rb
Overview
:nodoc:
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
49 50 51 52 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 49 def counts( = {}) opt = () select(opt[:select]).where(opt[:conditions]).joins(opt[:joins]).group(opt[:group]) end |
.find_or_create_with_like_by_name(name) ⇒ Object
LIKE is used for cross-database case-insensitivity
15 16 17 18 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 15 def self.find_or_create_with_like_by_name(name) # find(:first, :conditions => ["name LIKE ?", name]) || create(:name => name) where("LOWER(name) LIKE LOWER(?)", name).first || create(:name => name) end |
.options_for_counts(options = {}) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 54 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 = 'COUNT(*) > 0' having = [having, at_least, at_most].compact.join(' AND ') group_by = "#{Tag.table_name}.id, #{Tag.table_name}.name" # group_by << " AND #{having}" unless having.blank? select_condition = if self.column_names.include?('color') "#{Tag.table_name}.id, #{Tag.table_name}.name, #{Tag.table_name}.color, COUNT(*) AS count" else "#{Tag.table_name}.id, #{Tag.table_name}.name, COUNT(*) AS count" end { :select => select_condition, :joins => joins.join(" "), :conditions => conditions, :group => group_by, :having => having }.update() end |
Instance Method Details
#==(object) ⇒ Object
20 21 22 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 20 def ==(object) super || (object.is_a?(Tag) && name == object.name) end |
#color ⇒ Object
32 33 34 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 32 def color return ('#' + "%06x" % super) unless super.nil? end |
#color=(color) ⇒ Object
36 37 38 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 36 def color=(color) write_attribute(:color, color.from(1).hex) unless color.blank? end |
#count ⇒ Object
28 29 30 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 28 def count read_attribute(:count).to_i end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 24 def to_s name end |