Class: Stats::TopContextsQuery

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, options = {}) ⇒ TopContextsQuery


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

def initialize(user, options = {})
  @user = user
  @running = options.fetch(:running) { false }
  @limit = options.fetch(:limit) { false }
end

Instance Attribute Details

#limitObject (readonly)

Returns the value of attribute limit


7
8
9
# File 'app/models/stats/top_contexts_query.rb', line 7

def limit
  @limit
end

#runningObject (readonly)

Returns the value of attribute running


7
8
9
# File 'app/models/stats/top_contexts_query.rb', line 7

def running
  @running
end

#userObject (readonly)

Returns the value of attribute user


7
8
9
# File 'app/models/stats/top_contexts_query.rb', line 7

def user
  @user
end

Instance Method Details

#resultObject


14
15
16
# File 'app/models/stats/top_contexts_query.rb', line 14

def result
  user.contexts.find_by_sql([sql, user.id])
end

#sqlObject (private)


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

def sql
  query = "SELECT c.id AS id, c.name AS name, count(c.id) AS total "
  query << "FROM contexts c, todos t "
  query << "WHERE t.context_id=c.id "
  query << "AND t.user_id = ? "
  if running
    query << "AND t.completed_at IS NULL "
    query << "AND NOT c.state='hidden' "
  end
  query << "GROUP BY c.id, c.name "
  query << "ORDER BY total DESC "
  if limit
    query << "LIMIT #{limit}"
  end
  query
end