Permalink
Browse files

Report classes

I didn't like how much was going on in the view
(specifically the lambdas). This commit pulls
the reporting logic (simple thought it may be)
into separate classes.
  • Loading branch information...
1 parent 5255424 commit 3f8320baacf41d656b05445400bf0d776456142d @dce dce committed Nov 29, 2012
@@ -4,6 +4,14 @@ module StatBoard
class StatsController < ApplicationController
before_filter :basic_authenticate, :if => lambda { StatBoard.username && StatBoard.password }
+ def index
+ @reports = [
+ Reports::OverallReport.new,
+ Reports::MonthlyReport.new,
+ Reports::WeeklyReport.new
+ ]
+ end
+
private
def basic_authenticate
@@ -1,16 +1,16 @@
-<div id="<%= title.downcase.gsub(" ", "-") %>" class="span4">
+<div id="<%= report.slug %>" class="span4">
<table class="table table-striped">
<thead>
<tr>
- <th colspan="2"><%= title %></th>
+ <th colspan="2"><%= report.name %></th>
</tr>
</thead>
<tbody>
<% StatBoard.models.each do |model| %>
<tr>
<td class="span2"><%= model.to_s.pluralize %></td>
- <td class="span2"><%= scope[model].count %></td>
+ <td class="span2"><%= report.count(model) %></td>
</tr>
<% end %>
</tbody>
@@ -1,16 +1,6 @@
<% if StatBoard.models %>
<div class="row">
- <%= render "chart",
- :title => "Overall",
- :scope => lambda { |klass| klass } %>
-
- <%= render "chart",
- :title => "This Month",
- :scope => lambda { |klass| klass.where(["created_at > ?", 1.month.ago]) } %>
-
- <%= render "chart",
- :title => "This Week",
- :scope => lambda { |klass| klass.where(["created_at > ?", 1.week.ago]) } %>
+ <%= render :partial => "chart", :collection => @reports, :as => :report %>
</div>
<% else %>
Please specify some models by setting <code>StatBoard.models</code>
View
@@ -1,4 +1,8 @@
require "stat_board/engine"
+require "stat_board/reports/report"
+require "stat_board/reports/overall_report"
+require "stat_board/reports/monthly_report"
+require "stat_board/reports/weekly_report"
module StatBoard
mattr_accessor :models, :title, :username, :password
@@ -0,0 +1,13 @@
+module StatBoard
+ module Reports
+ class MonthlyReport < Report
+ def name
+ "This Month"
+ end
+
+ def scope(model)
+ model.where(["created_at > ?", 1.month.ago])
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module StatBoard
+ module Reports
+ class OverallReport < Report
+ def name
+ "Overall"
+ end
+
+ def scope(model)
+ model
+ end
+ end
+ end
+end
@@ -0,0 +1,21 @@
+module StatBoard
+ module Reports
+ class Report
+ def name
+ # define in subclass
+ end
+
+ def scope
+ # define in subclass
+ end
+
+ def slug
+ name.downcase.gsub(" ", "-")
+ end
+
+ def count(model)
+ scope(model).count
+ end
+ end
+ end
+end
@@ -0,0 +1,13 @@
+module StatBoard
+ module Reports
+ class WeeklyReport < Report
+ def name
+ "This Week"
+ end
+
+ def scope(model)
+ model.where(["created_at > ?", 1.week.ago])
+ end
+ end
+ end
+end
@@ -98,7 +98,6 @@
it "sees the stat board if correct details provided" do
page.driver.browser.authorize("user", "pass")
visit "/stats"
- puts page.body
page.should have_content("StatBoard")
end

0 comments on commit 3f8320b

Please sign in to comment.