remove code duplication

This commit is contained in:
Matt Rogers 2012-09-08 13:31:22 -05:00
parent f9bd923205
commit b89062913f

View file

@ -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 "+