Group project-related stats into a class.

Reduce number of instance variables available to the views.
Replace raw SQL with AR-type query.
This commit is contained in:
Katrina Owen 2013-03-02 17:11:19 -07:00
parent 1e3782ce67
commit 8b1f0a34a0
4 changed files with 27 additions and 21 deletions

View file

@ -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)

View file

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

View file

@ -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} -%>

View file

@ -12,7 +12,7 @@
<%= render :partial => 'contexts' -%>
<h2><%= t('stats.projects') %></h2>
<%= render :partial => 'projects' -%>
<%= render :partial => 'projects', :locals => {:projects => @projects} -%>
<h2><%= t('stats.tags') %></h2>
<%= render :partial => 'tags', :locals => {:tag_cloud => @tag_cloud, :key => ''} -%>