From 0aa6ea183ba6296ca532a5b6712e0ba2cdff306f Mon Sep 17 00:00:00 2001
From: Katrina Owen
Date: Fri, 1 Mar 2013 20:38:24 -0500
Subject: [PATCH] Extract chart value object
This simplifies the views (slightly).
---
app/controllers/stats_controller.rb | 41 ++++++++++++++-----
app/models/stats/chart.rb | 18 ++++++++
app/views/stats/_actions.html.erb | 29 ++++---------
app/views/stats/_chart.html.erb | 4 +-
app/views/stats/_contexts.html.erb | 8 ++--
.../stats/actions_done_last_years.html.erb | 2 +-
.../stats/show_selection_from_chart.html.erb | 4 +-
7 files changed, 65 insertions(+), 41 deletions(-)
create mode 100644 app/models/stats/chart.rb
diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb
index 5512abad..3b907a97 100644
--- a/app/controllers/stats_controller.rb
+++ b/app/controllers/stats_controller.rb
@@ -50,8 +50,7 @@ class StatsController < ApplicationController
def actions_done_last_years
@page_title = t('stats.index_title')
- @chart_width = 900
- @chart_height = 400
+ @chart = Stats::Chart.new('actions_done_lastyears_data', :height => 400, :width => 900)
end
def actions_done_lastyears_data
@@ -309,7 +308,7 @@ class StatsController < ApplicationController
week_from = params['index'].to_i
week_to = week_from+1
- @chart_name = "actions_visible_running_time_data"
+ @chart = Stats::Chart.new('actions_visible_running_time_data')
@page_title = t('stats.actions_selected_from_week')
@further = false
if params['id'] == 'avrt_end'
@@ -334,7 +333,7 @@ class StatsController < ApplicationController
week_from = params['index'].to_i
week_to = week_from+1
- @chart_name = "actions_running_time_data"
+ @chart = Stats::Chart.new('actions_running_time_data')
@page_title = "Actions selected from week "
@further = false
if params['id'] == 'art_end'
@@ -421,12 +420,6 @@ class StatsController < ApplicationController
def init
@me = self # for meta programming
- # default chart dimensions
- @chart_width=460
- @chart_height=250
- @pie_width=@chart_width
- @pie_height=325
-
# get the current date wih time set to 0:0
@today = Time.zone.now.utc.beginning_of_day
@@ -468,6 +461,27 @@ class StatsController < ApplicationController
# get count of actions done in the past 12 months.
@sum_actions_done_last12months = current_user.todos.completed.completed_after(@cut_off_year).count
@sum_actions_created_last12months = current_user.todos.created_after(@cut_off_year).count
+
+ @completion_charts = %w{
+ actions_done_last30days_data
+ actions_done_last12months_data
+ actions_completion_time_data
+ }.map do |action|
+ Stats::Chart.new(action)
+ end
+
+ @timing_charts = %w{
+ actions_visible_running_time_data
+ actions_running_time_data
+ actions_open_per_week_data
+ actions_day_of_week_all_data
+ actions_day_of_week_30days_data
+ actions_time_of_day_all_data
+ actions_time_of_day_30days_data
+ }.map do |action|
+ Stats::Chart.new(action)
+ end
+
end
def get_stats_contexts
@@ -496,6 +510,13 @@ class StatsController < ApplicationController
"GROUP BY c.id, c.name ORDER BY total DESC " +
"LIMIT 5"
)
+
+ @context_charts = %w{
+ context_total_actions_data
+ context_running_actions_data
+ }.map do |action|
+ Stats::Chart.new(action, :height => 325)
+ end
end
def get_stats_projects
diff --git a/app/models/stats/chart.rb b/app/models/stats/chart.rb
new file mode 100644
index 00000000..66fc59a8
--- /dev/null
+++ b/app/models/stats/chart.rb
@@ -0,0 +1,18 @@
+module Stats
+
+ class Chart
+
+ attr_reader :action, :height, :width
+ def initialize(action, dimensions = {})
+ @action = action
+ @height = dimensions.fetch(:height) { 250 }
+ @width = dimensions.fetch(:width) { 460 }
+ end
+
+ def dimensions
+ "#{width}x#{height}"
+ end
+
+ end
+
+end
diff --git a/app/views/stats/_actions.html.erb b/app/views/stats/_actions.html.erb
index f4f18191..fc50247a 100755
--- a/app/views/stats/_actions.html.erb
+++ b/app/views/stats/_actions.html.erb
@@ -7,28 +7,13 @@
<%= t('stats.actions_avg_created', :count => (@sum_actions_created_last12months*10.0/12.0).round/10.0 )%>
<%= t('stats.actions_avg_completed', :count => (@sum_actions_done_last12months*10.0/12.0).round/10.0 )%>
-<%
- %w{
- actions_done_last30days_data
- actions_done_last12months_data
- actions_completion_time_data
- }.each do |action|
- %><%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => action)} -%><%
- end
-%>
+<% @completion_charts.each do |chart| %><%=
+ render :partial => 'chart', :locals => {:chart => chart}
+-%><% end %>
-<%
-%w{
- actions_visible_running_time_data
- actions_running_time_data
- actions_open_per_week_data
- actions_day_of_week_all_data
- actions_day_of_week_30days_data
- actions_time_of_day_all_data
- actions_time_of_day_30days_data
- }.each do |action|
- %><%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => action)} -%><%
- end
-%>
+<% @timing_charts.each do |chart| %><%=
+ render :partial => 'chart', :locals => {:chart => chart}
+-%><% end %>
+
diff --git a/app/views/stats/_chart.html.erb b/app/views/stats/_chart.html.erb
index 48d1beef..d0d581c2 100755
--- a/app/views/stats/_chart.html.erb
+++ b/app/views/stats/_chart.html.erb
@@ -1,6 +1,6 @@
<% @swf_count ||= 0 -%>
<%= swf_tag asset_path("open-flash-chart.swf"),
- :flashvars => { 'width' => width, 'height' => height, 'data' => data},
+ :flashvars => { 'width' => chart.width, 'height' => chart.height, 'data' => url_for(:action => chart.action)},
:parameters => { 'allowScriptAccess' => 'sameDomain', 'wmode' => 'transparent'},
:div_id => "chart_#{@swf_count+=1}",
- :size => "#{width}x#{height}" %>
\ No newline at end of file
+ :size => chart.dimensions %>
diff --git a/app/views/stats/_contexts.html.erb b/app/views/stats/_contexts.html.erb
index b09989eb..eb236a97 100755
--- a/app/views/stats/_contexts.html.erb
+++ b/app/views/stats/_contexts.html.erb
@@ -1,6 +1,6 @@
-<%= render :partial => 'chart', :locals => {:width => @pie_width, :height => @pie_height, :data => url_for(:action => 'context_total_actions_data')} -%>
-
-<%= render :partial => 'chart', :locals => {:width => @pie_width, :height => @pie_height, :data => url_for(:action => 'context_running_actions_data')} -%>
+<% @context_charts.each do |chart| %><%=
+ render :partial => 'chart', :locals => {:chart => chart}
+-%><% end %>
@@ -34,4 +34,4 @@
<%
end
-%>
-
\ No newline at end of file
+
diff --git a/app/views/stats/actions_done_last_years.html.erb b/app/views/stats/actions_done_last_years.html.erb
index 105a7356..76917102 100644
--- a/app/views/stats/actions_done_last_years.html.erb
+++ b/app/views/stats/actions_done_last_years.html.erb
@@ -1,2 +1,2 @@
-<%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => :actions_done_lastyears_data)} -%>
+<%= render :partial => 'chart', :locals => {:chart => @chart} -%>
<%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %>
diff --git a/app/views/stats/show_selection_from_chart.html.erb b/app/views/stats/show_selection_from_chart.html.erb
index 1b5c884b..80b40306 100644
--- a/app/views/stats/show_selection_from_chart.html.erb
+++ b/app/views/stats/show_selection_from_chart.html.erb
@@ -1,4 +1,4 @@
-<%= render :partial => 'chart', :locals => {:width => @chart_width, :height => @chart_height, :data => url_for(:action => @chart_name)} -%>
+<%= render :partial => 'chart', :locals => {:chart => @chart} -%>
<%= t('stats.click_to_update_actions') %> <%= raw t('stats.click_to_return', :link => link_to(t('stats.click_to_return_link'), stats_path)) %>
@@ -22,4 +22,4 @@
<%= render :partial => "todos/todo", :collection => @selected_actions, :locals => { :parent_container_type => 'stats' } %>
-
\ No newline at end of file
+