Class: Tag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Tag
- Defined in:
- lib/tag.rb
Defined Under Namespace
Classes: HierarchyCycle
Constant Summary collapse
- SYMBOL =
/[^#=\/]/.freeze
Instance Attribute Summary collapse
-
#mark ⇒ Object
Returns the value of attribute mark.
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.
- .find_or_create_with_like_by_name!(name) ⇒ Object
- .find_with_like_by_name(name) ⇒ Object
- .options_for_counts(options = {}) ⇒ Object
Instance Method Summary collapse
- #==(object) ⇒ Object
- #check_mark!(condition) ⇒ Object
- #count ⇒ Object
- #marked? ⇒ Boolean
- #to_param ⇒ Object
- #to_s ⇒ Object
Instance Attribute Details
#mark ⇒ Object
Returns the value of attribute mark.
6 7 8 |
# File 'lib/tag.rb', line 6 def mark @mark end |
Class Method Details
.counts(options = {}) ⇒ Object
Calculate the tag counts for all tags.
:start_at - Restrict the to those created after a certain time
:end_at - Restrict the to those created before a certain time
:conditions - A piece of SQL conditions to add to the query
:limit - The maximum number of to return
:order - A piece of SQL to order by. Eg 'count desc' or 'taggings.created_at desc'
:at_least - Exclude with a frequency less than the given value
:at_most - Exclude with a frequency greater than the given value
:mark_condition - Set 'mark' attribute on conforms this condition
primarliy used with per-user taggings like ['taggings.create_by_id = ?', user.id]
104 105 106 |
# File 'lib/tag.rb', line 104 def counts( = {}) find(:all, ()) end |
.find_or_create_with_like_by_name(name) ⇒ Object
LIKE is used for cross-database case-insensitivity
56 57 58 |
# File 'lib/tag.rb', line 56 def self.find_or_create_with_like_by_name(name) find_with_like_by_name(name) || create(:name => name) end |
.find_or_create_with_like_by_name!(name) ⇒ Object
60 61 62 |
# File 'lib/tag.rb', line 60 def self.find_or_create_with_like_by_name!(name) find_with_like_by_name(name) || create!(:name => name) end |
.find_with_like_by_name(name) ⇒ Object
64 65 66 |
# File 'lib/tag.rb', line 64 def self.find_with_like_by_name(name) where("name LIKE ?", name).first end |
.options_for_counts(options = {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/tag.rb', line 108 def ( = {}) .assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :joins, :mark_condition = .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 = [ .delete(:conditions), start_at, end_at ].compact conditions = conditions.any? ? conditions.join(' AND ') : nil mark_condition = .delete(:mark_condition) mark_condition = sanitize_sql(mark_condition) if mark_condition mark_select = "GROUP_CONCAT(DISTINCT IF((#{sanitize_sql(mark_condition)}), 1, NULL)) as mark" if mark_condition base_select = "#{Tag.table_name}.id, #{Tag.table_name}.name, COUNT(*) AS count" select = [ base_select, mark_select ].compact.join(', ') 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 => select, :joins => joins.join(" "), :conditions => conditions, :group => group_by }.update() end |
Instance Method Details
#==(object) ⇒ Object
68 69 70 |
# File 'lib/tag.rb', line 68 def ==(object) super || (object.is_a?(Tag) && name == object.name) end |
#check_mark!(condition) ⇒ Object
88 89 90 |
# File 'lib/tag.rb', line 88 def check_mark!(condition) self.mark = taggings.find(:first, :conditions => condition) ? 1 : 0 end |
#count ⇒ Object
80 81 82 |
# File 'lib/tag.rb', line 80 def count read_attribute(:count).to_i end |
#marked? ⇒ Boolean
84 85 86 |
# File 'lib/tag.rb', line 84 def marked? read_attribute(:mark).to_i == 1 end |
#to_param ⇒ Object
76 77 78 |
# File 'lib/tag.rb', line 76 def to_param name end |
#to_s ⇒ Object
72 73 74 |
# File 'lib/tag.rb', line 72 def to_s name end |