From 8b1f0a34a08fcfab724357e68d22a455ec51ef99 Mon Sep 17 00:00:00 2001 From: Katrina Owen Date: Sat, 2 Mar 2013 17:11:19 -0700 Subject: [PATCH] Group project-related stats into a class. Reduce number of instance variables available to the views. Replace raw SQL with AR-type query. --- app/controllers/stats_controller.rb | 18 +----------------- app/models/stats/projects.rb | 22 ++++++++++++++++++++++ app/views/stats/_projects.html.erb | 6 +++--- app/views/stats/index.html.erb | 2 +- 4 files changed, 27 insertions(+), 21 deletions(-) create mode 100644 app/models/stats/projects.rb diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index b5fbf87a..b44182a9 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -14,9 +14,9 @@ class StatsController < ApplicationController @unique_tags_count = tag_ids.uniq.size @hidden_contexts = current_user.contexts.hidden @actions = Stats::Actions.new(current_user) + @projects = Stats::Projects.new(current_user) get_stats_contexts - get_stats_projects get_stats_tags end @@ -399,22 +399,6 @@ class StatsController < ApplicationController end end - def get_stats_projects - @projects_and_actions = Stats::TopProjectsQuery.new(current_user).result - @projects_and_actions_last30days = Stats::TopProjectsQuery.new(current_user, @cut_off_month).result - - # get the first 10 projects and their running time (creation date versus - # now()) - @projects_and_runtime = current_user.projects.find_by_sql( - "SELECT id, name, created_at "+ - "FROM projects "+ - "WHERE state='active' "+ - "AND user_id=#{current_user.id} "+ - "ORDER BY created_at ASC "+ - "LIMIT 10" - ) - end - def get_stats_tags tags = Stats::TagCloudQuery.new(current_user).result @tag_cloud = Stats::TagCloud.new(tags) diff --git a/app/models/stats/projects.rb b/app/models/stats/projects.rb new file mode 100644 index 00000000..45b437f4 --- /dev/null +++ b/app/models/stats/projects.rb @@ -0,0 +1,22 @@ +module Stats + class Projects + + attr_reader :user + def initialize(user) + @user = user + end + + def runtime + @runtime ||= user.projects.active.order('created_at ASC').limit(10) + end + + def actions + @actions ||= Stats::TopProjectsQuery.new(user).result + end + + def actions_last30days + @actions_last30days ||= Stats::TopProjectsQuery.new(user, 1.month.ago.beginning_of_day).result + end + + end +end diff --git a/app/views/stats/_projects.html.erb b/app/views/stats/_projects.html.erb index 5b2d6de3..918fc128 100755 --- a/app/views/stats/_projects.html.erb +++ b/app/views/stats/_projects.html.erb @@ -1,6 +1,6 @@ -<%= render :partial => 'projects_list', :locals => {:projects => @projects_and_actions, :key => 'projects', :n => :count} -%> +<%= render :partial => 'projects_list', :locals => {:projects => projects.actions, :key => 'projects', :n => :count} -%> -<%= render :partial => 'projects_list', :locals => {:projects => @projects_and_actions_last30days, :key => 'projects_30days', :n => :count} -%> +<%= render :partial => 'projects_list', :locals => {:projects => projects.actions_last30days, :key => 'projects_30days', :n => :count} -%> -<%= render :partial => 'projects_list', :locals => {:projects => @projects_and_runtime, :key => 'longrunning', :n => :age_in_days} -%> +<%= render :partial => 'projects_list', :locals => {:projects => projects.runtime, :key => 'longrunning', :n => :age_in_days} -%> diff --git a/app/views/stats/index.html.erb b/app/views/stats/index.html.erb index cd3704cc..f40f1dd3 100755 --- a/app/views/stats/index.html.erb +++ b/app/views/stats/index.html.erb @@ -12,7 +12,7 @@ <%= render :partial => 'contexts' -%>

<%= t('stats.projects') %>

- <%= render :partial => 'projects' -%> + <%= render :partial => 'projects', :locals => {:projects => @projects} -%>

<%= t('stats.tags') %>

<%= render :partial => 'tags', :locals => {:tag_cloud => @tag_cloud, :key => ''} -%>