Class: Stats::TopProjectsQuery

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, cutoff = nil) ⇒ TopProjectsQuery

Returns a new instance of TopProjectsQuery.



8
9
10
11
# File 'app/models/stats/top_projects_query.rb', line 8

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

Instance Attribute Details

#cutoffObject (readonly)

Returns the value of attribute cutoff.



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

def cutoff
  @cutoff
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Instance Method Details

#query_optionsObject (private)



19
20
21
22
23
# File 'app/models/stats/top_projects_query.rb', line 19

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

#resultObject



13
14
15
# File 'app/models/stats/top_projects_query.rb', line 13

def result
  user.projects.find_by_sql(query_options)
end

#sqlObject (private)



25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/models/stats/top_projects_query.rb', line 25

def sql
  query = "SELECT p.id, p.name, count(p.id) AS count "
  query << "FROM todos t, projects p "
  query << "WHERE t.project_id = p.id "
  query << "AND t.user_id= ? "
  if cutoff
    query << "AND (t.created_at > ? OR t.completed_at > ?) "
  end
  query << "GROUP BY p.id, p.name "
  query << "ORDER BY count DESC "
  query << "LIMIT 10"
end