Class: Stats::TopContextsQuery
- Inherits:
-
Object
- Object
- Stats::TopContextsQuery
- Defined in:
- app/models/stats/top_contexts_query.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#running ⇒ Object
readonly
Returns the value of attribute running.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(user, options = {}) ⇒ TopContextsQuery
constructor
A new instance of TopContextsQuery.
- #result ⇒ Object
- #sql ⇒ Object private
Constructor Details
#initialize(user, options = {}) ⇒ TopContextsQuery
Returns a new instance of TopContextsQuery.
8 9 10 11 12 |
# File 'app/models/stats/top_contexts_query.rb', line 8 def initialize(user, = {}) @user = user @running = .fetch(:running) { false } @limit = .fetch(:limit) { false } end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
6 7 8 |
# File 'app/models/stats/top_contexts_query.rb', line 6 def limit @limit end |
#running ⇒ Object (readonly)
Returns the value of attribute running.
6 7 8 |
# File 'app/models/stats/top_contexts_query.rb', line 6 def running @running end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
6 7 8 |
# File 'app/models/stats/top_contexts_query.rb', line 6 def user @user end |
Instance Method Details
#result ⇒ Object
14 15 16 |
# File 'app/models/stats/top_contexts_query.rb', line 14 def result user.contexts.find_by_sql([sql, user.id]) end |
#sql ⇒ Object (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 |