Merge pull request #165 from kytrinyx/more-context-sql

Fold two more SQL queries into the query object
This commit is contained in:
Matt Rogers 2013-03-02 05:39:06 -08:00
commit d518fe6eb0
2 changed files with 12 additions and 31 deletions

View file

@ -187,36 +187,14 @@ class StatsController < ApplicationController
end
def context_total_actions_data
# get total action count per context Went from GROUP BY c.id to c.name for
# compatibility with postgresql. Since the name is forced to be unique, this
# should work.
all_actions_per_context = current_user.contexts.find_by_sql(
"SELECT c.name AS name, c.id as id, count(*) AS total "+
"FROM contexts c, todos t "+
"WHERE t.context_id=c.id "+
"AND c.user_id = #{current_user.id} " +
"GROUP BY c.name, c.id "+
"ORDER BY total DESC"
)
all_actions_per_context = Stats::TopContextsQuery.new(current_user).result
prep_context_data_for_view(all_actions_per_context)
render :layout => false
end
def context_running_actions_data
# get incomplete action count per visible context
#
# Went from GROUP BY c.id to c.name for compatibility with postgresql. Since
# the name is forced to be unique, this should work.
all_actions_per_context = current_user.contexts.find_by_sql(
"SELECT c.name AS name, c.id as id, count(*) AS total "+
"FROM contexts c, todos t "+
"WHERE t.context_id=c.id AND t.completed_at IS NULL AND NOT c.state='hidden' "+
"AND c.user_id = #{current_user.id} " +
"GROUP BY c.name, c.id "+
"ORDER BY total DESC"
)
all_actions_per_context = Stats::TopContextsQuery.new(current_user, :running => true).result
prep_context_data_for_view(all_actions_per_context)
@ -464,8 +442,8 @@ class StatsController < ApplicationController
end
def get_stats_contexts
@actions_per_context = Stats::TopContextsQuery.new(current_user).result
@running_actions_per_context = Stats::TopContextsQuery.new(current_user, :running).result
@actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5).result
@running_actions_per_context = Stats::TopContextsQuery.new(current_user, :limit => 5, :running => true).result
@context_charts = %w{
context_total_actions_data

View file

@ -4,10 +4,11 @@
module Stats
class TopContextsQuery
attr_reader :user, :running
def initialize(user, running = nil)
attr_reader :user, :running, :limit
def initialize(user, options = {})
@user = user
@running = running == :running
@running = options.fetch(:running) { false }
@limit = options.fetch(:limit) { false }
end
def result
@ -27,8 +28,10 @@ module Stats
end
query << "GROUP BY c.id, c.name "
query << "ORDER BY total DESC "
query << "LIMIT 5"
if limit
query << "LIMIT #{limit}"
end
query
end
end
end