diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 170e442e..a67ec9e7 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -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 diff --git a/app/models/stats/top_contexts_query.rb b/app/models/stats/top_contexts_query.rb index 714af8e1..69a5e9e9 100644 --- a/app/models/stats/top_contexts_query.rb +++ b/app/models/stats/top_contexts_query.rb @@ -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