summarizer
Summary
In the model:
class Foo < ActiveRecord::Base
include Summarizer::Base # add Summarizer to your class
summarize :net, :gross # which columns/methods do you want to sum up?
end
In the controller:
# find_with_sums extends ActiveRecord.find
@foos = Foo.find_with_sums(:all, :order => 'created_at',
:split_sections_on => Proc.new {|a,b| a.at.month != b.at.month},
:section_label => Proc.new{|obj| obj.at.strftime("%B %Y")})
In the view:
<table>
<% @foos.each_with_sums do |foo| %>
<%= render :partial => 'index_section_header', :locals => { :header => foo.section_label } if foo.new_section? %>
<tr>
<td><%=h foo.created_at %></td>
<td><%=h foo.name %></td>
<td><%=h foo.gross %></td>
<td><%=h foo.net %></td>
</tr>
<%= render :partial => 'index_sum', :locals => { :sum => foo.section_sum } if foo.end_of_section? %>
<% end %>
<%= render :partial => 'index_sum', :locals => { :sum => @foos.total } %>
</table>
The partials _index_section_header.html.erb
<tr>
<td colspan="4"><%= header %></td>
</tr>
and _index_sum.html.erb
<tr>
<td colspan="2">Total</td>
<td><%= sum.gross %></td>
<td><%= sum.net %></td>
</tr>
In the Browser:
November 2009
2009-11-29 03:32:27 Blah 160.46 91.25
Total 160.46 91.25
December 2009
2009-12-04 01:32:45 Blub 130.42 122.24
2009-12-05 18:02:33 Bar 77.53 135.57
2009-12-15 21:19:25 Blah 175.32 150.42
2009-12-31 17:21:49 Baz 192.05 135.04
Total 575.32 543.27
January 2010
2010-01-08 18:26:48 Blub 116.14 151.22
Total 116.14 151.22
----------------------------------------------
Total 851.92 785.74
Usage
Soon to come…
Copyright
Copyright © 2010 Christoph Petschnig. See LICENSE for details.