Class: Tag

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/tag.rb

Defined Under Namespace

Classes: HierarchyCycle

Constant Summary collapse

SYMBOL =
/[^#=\/]/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#markObject

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 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
:mark_condition - Set 'mark' attribute on tags 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(options = {})
  find(:all, options_for_counts(options))
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 options_for_counts(options = {})
  options.assert_valid_keys :start_at, :end_at, :conditions, :at_least, :at_most, :order, :limit, :joins, :mark_condition
  options = options.dup

  start_at = sanitize_sql(["#{Tagging.table_name}.created_at >= ?", options.delete(:start_at)]) if options[:start_at]
  end_at = sanitize_sql(["#{Tagging.table_name}.created_at <= ?", options.delete(:end_at)]) if options[:end_at]

  conditions = [
    options.delete(:conditions),
    start_at,
    end_at
  ].compact

  conditions = conditions.any? ? conditions.join(' AND ') : nil

  mark_condition = options.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 << options.delete(:joins) if options[:joins]

  at_least  = sanitize_sql(['COUNT(*) >= ?', options.delete(:at_least)]) if options[:at_least]
  at_most   = sanitize_sql(['COUNT(*) <= ?', options.delete(:at_most)]) if options[: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(options)
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

#countObject



80
81
82
# File 'lib/tag.rb', line 80

def count
  read_attribute(:count).to_i
end

#marked?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/tag.rb', line 84

def marked?
  read_attribute(:mark).to_i == 1
end

#to_paramObject



76
77
78
# File 'lib/tag.rb', line 76

def to_param
  name
end

#to_sObject



72
73
74
# File 'lib/tag.rb', line 72

def to_s
  name
end