Class: Stats::TagCloudQuery

Inherits:
Object
  • Object
show all
Defined in:
app/models/stats/tag_cloud_query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, cutoff = nil) ⇒ TagCloudQuery

Returns a new instance of TagCloudQuery.



5
6
7
8
# File 'app/models/stats/tag_cloud_query.rb', line 5

def initialize(user, cutoff = nil)
  @user = user
  @cutoff = cutoff
end

Instance Attribute Details

#cutoffObject (readonly)

Returns the value of attribute cutoff.



3
4
5
# File 'app/models/stats/tag_cloud_query.rb', line 3

def cutoff
  @cutoff
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'app/models/stats/tag_cloud_query.rb', line 3

def user
  @user
end

Instance Method Details

#query_optionsObject



14
15
16
17
18
# File 'app/models/stats/tag_cloud_query.rb', line 14

def query_options
  options = [sql, user.id]
  options += [cutoff, cutoff] if cutoff
  options
end

#resultObject



10
11
12
# File 'app/models/stats/tag_cloud_query.rb', line 10

def result
  Tag.find_by_sql(query_options)
end

#sqlObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/stats/tag_cloud_query.rb', line 20

def sql
  # TODO: parameterize limit
  query = "SELECT tags.id, tags.name AS name, COUNT(*) AS count"
  query << " FROM taggings, tags, todos"
  query << " WHERE tags.id = tag_id"
  query << " AND todos.user_id = ? "
  query << " AND taggings.taggable_type = 'Todo' "
  query << " AND taggings.taggable_id = todos.id "
  if cutoff
    query << " AND (todos.created_at > ? OR "
    query << "      todos.completed_at > ?) "
  end
  query << " GROUP BY tags.id, tags.name"
  query << " ORDER BY count DESC, name "
  query << " LIMIT 100"
end