From b89062913f5bca23f9e222943efb7a79f407bac3 Mon Sep 17 00:00:00 2001 From: Matt Rogers Date: Sat, 8 Sep 2012 13:31:22 -0500 Subject: [PATCH] remove code duplication --- app/controllers/stats_controller.rb | 64 ++++++++++++----------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 10700fc9..e5f2f6e2 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -198,26 +198,8 @@ class StatsController < ApplicationController "GROUP BY c.name, c.id "+ "ORDER BY total DESC" ) - @sum = all_actions_per_context.inject(0){|sum, apc| sum += apc['total'].to_i } - - pie_cutoff=10 - size = [all_actions_per_context.size, pie_cutoff].min - - # explicitely copy contents of hash to avoid ending up with two arrays pointing to same hashes - @actions_per_context = Array.new(size){|i| { - 'name' => all_actions_per_context[i][:name], - 'total' => all_actions_per_context[i][:total].to_i, - 'id' => all_actions_per_context[i][:id] - } } - if size==pie_cutoff - @actions_per_context[size-1]['name']=t('stats.other_actions_label') - @actions_per_context[size-1]['total']=@actions_per_context[size-1]['total'] - @actions_per_context[size-1]['id']=-1 - size.upto(all_actions_per_context.size-1){ |i| @actions_per_context[size-1]['total']+=(all_actions_per_context[i]['total'].to_i) } - end - - @truncate_chars = 15 + prep_context_data_for_view(all_actions_per_context) render :layout => false end @@ -235,26 +217,8 @@ class StatsController < ApplicationController "GROUP BY c.name, c.id "+ "ORDER BY total DESC" ) - @sum = all_actions_per_context.inject(0){|sum, apc| sum += apc['total'].to_i } - pie_cutoff=10 - size = [all_actions_per_context.size, pie_cutoff].min - - # explicitely copy contents of hash to avoid ending up with two arrays pointing to same hashes - @actions_per_context = Array.new(size){|i| { - 'name' => all_actions_per_context[i][:name], - 'total' => all_actions_per_context[i][:total].to_i, - 'id' => all_actions_per_context[i][:id] - } } - - if size==pie_cutoff - @actions_per_context[size-1]['name']=t('stats.other_actions_label') - @actions_per_context[size-1]['total']=@actions_per_context[size-1]['total'] - @actions_per_context[size-1]['id']=-1 - (size).upto(all_actions_per_context.size()-1){|i| @actions_per_context[size-1]['total']+=all_actions_per_context[i]['total'].to_i } - end - - @truncate_chars = 15 + prep_context_data_for_view(all_actions_per_context) render :layout => false end @@ -408,6 +372,30 @@ class StatsController < ApplicationController private + def prep_context_data_for_view(all_actions_per_context) + + @sum = all_actions_per_context.inject(0){|sum, apc| sum += apc['total'].to_i } + + pie_cutoff=10 + size = [all_actions_per_context.size, pie_cutoff].min + + # explicitely copy contents of hash to avoid ending up with two arrays pointing to same hashes + @actions_per_context = Array.new(size){|i| { + 'name' => all_actions_per_context[i][:name], + 'total' => all_actions_per_context[i][:total].to_i, + 'id' => all_actions_per_context[i][:id] + } } + + if size==pie_cutoff + @actions_per_context[size-1]['name']=t('stats.other_actions_label') + @actions_per_context[size-1]['total']=@actions_per_context[size-1]['total'] + @actions_per_context[size-1]['id']=-1 + size.upto(all_actions_per_context.size-1){ |i| @actions_per_context[size-1]['total']+=(all_actions_per_context[i]['total'].to_i) } + end + + @truncate_chars = 15 + end + def get_unique_tags_of_user tag_ids = current_user.todos.find_by_sql([ "SELECT DISTINCT tags.id as id "+