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
- .color_from_name(str) ⇒ Object
-
.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
.color_from_name(str) ⇒ Object
22 23 24 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 22 def self.color_from_name(str) "##{Digest::MD5.hexdigest(str.to_s)[0..5]}" end |
.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
59 60 61 62 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 59 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
17 18 19 20 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 17 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
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 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 64 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 << ", #{Tag.table_name}.color" if column_names.include?('color') 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
26 27 28 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 26 def ==(object) super || (object.is_a?(Tag) && name == object.name) end |
#color ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 38 def color if self.class.column_names.include?('color') db_color = read_attribute(:color) return ('#' + "%06x" % db_color) unless db_color.nil? end self.class.color_from_name(name) end |
#color=(color) ⇒ Object
46 47 48 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 46 def color=(color) write_attribute(:color, color.sub(/\A#/, '').hex) unless color.blank? end |
#count ⇒ Object
34 35 36 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 34 def count read_attribute(:count).to_i end |
#to_s ⇒ Object
30 31 32 |
# File 'lib/redmineup/acts_as_taggable/tag.rb', line 30 def to_s name end |