Skip to content

Instantly share code, notes, and snippets.

@rubysolo
Created June 2, 2009 02:32
Show Gist options
  • Save rubysolo/121945 to your computer and use it in GitHub Desktop.
Save rubysolo/121945 to your computer and use it in GitHub Desktop.
class BaseReport
# encapsulate common functionality for gathering report parameters
def initialize(attrs={})
date_time_attributes = {}
# this is a naive implementation of recognizing rails' multipart parameters (eg for datetime selects)
# we are assuming either date / datetime, and discarding any datatype component
attrs.each do |k,v|
if k.to_s.include?("(")
# assume this is a date/time parameter : extract parts to build entire value
attribute_name, position = k.split("(")
position = position.to_i
date_time_attributes[attribute_name] ||= []
date_time_attributes[attribute_name] << [position, v.to_i]
else
setter = "#{k}=".to_sym
self.send(setter, v) if self.respond_to?(setter)
end
end
# add date attributes
date_time_attributes.each do |k, v|
setter = "#{k}=".to_sym
if self.respond_to?(setter)
# extract the actual multipart value in the right order
args = v.sort.map{|pair| pair[1] }
case args.length
when 3
# assume it's a date
self.send(setter, Date.new(*args))
when 6
# assume it's a datetime
self.send(setter, Time.new(*args))
else
end
end
end
end
end
class Admin::Reports::CustomersController < Admin::Reports::BaseController
class Report < BaseReport
attr_accessor :start_date
attr_accessor :end_date
end
def index
@report = Report.new(
:start_date => 1.month.ago.at_beginning_of_month,
:end_date => 1.month.ago.at_end_of_month
)
end
def execute
@report = Report.new(params[:report])
@customers = Customer.find(:all, :conditions => ["created_at BETWEEN ? AND ?", @report.start_date, @report.end_date], :order => "source, created_at")
respond_to do |format|
format.html
format.xls {
headers['Content-Type']='application/vnd.ms-excel; charset=utf-8'
headers['Content-Disposition'] = %{attachment; filename="Customers-#{@report.start_date.strftime('%Y%m%d')}-#{@report.end_date.strftime('%Y%m%d')}.xls"}
headers['Cache-Control'] = ''
render :layout => false
}
end
end
end
<h1>New Customers</h1>
<table>
<tr>
<th>Organization Name</th>
<th>Signup Date</th>
<th>Revenue</th>
</tr>
<%- @customers.each do |customer| -%>
<tr class="<%= cycle('even_row', 'odd_row') %>">
<td><%= customer.organization_name %></td>
<td><%= date_mdy customer.created_at %></td>
<td align="right"><%= number_to_currency customer.revenue %></td>
</tr>
<%- end -%>
</table>
xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.Workbook({
'xmlns' => "urn:schemas-microsoft-com:office:spreadsheet",
'xmlns:o' => "urn:schemas-microsoft-com:office:office",
'xmlns:x' => "urn:schemas-microsoft-com:office:excel",
'xmlns:html' => "http://www.w3.org/TR/REC-html40",
'xmlns:ss' => "urn:schemas-microsoft-com:office:spreadsheet"
}) do
xml.Styles do
xml.Style 'ss:ID' => 'Default', 'ss:Name' => 'Normal' do
xml.Alignment 'ss:Vertical' => 'Bottom'
xml.Borders
xml.Font 'ss:FontName' => 'Verdana'
xml.Interior
xml.NumberFormat
xml.Protection
end
xml.Style 'ss:ID' => 'currency', 'ss:Name' => 'Currency' do
xml.NumberFormat 'ss:Format' => '_(&quot;$&quot;* #,##0.00_);_(&quot;$&quot;* \(#,##0.00\);_(&quot;$&quot;* &quot;-&quot;??_);_(@_)'
end
end
xml.Worksheet 'ss:Name' => "Customers" do
xml.Table do
# Header
xml.Row do
xml.Cell do
xml.Data 'Customer Name', 'ss:Type' => 'String'
end
xml.Cell do
xml.Data 'Signup Date', 'ss:Type' => 'String'
end
xml.Cell do
xml.Data 'Revenue', 'ss:Type' => 'String'
end
# OTHER HEADERS AS NEEDED
end
@customers.each do |customer|
xml.Row do
xml.Cell do
xml.Data customer.name, 'ss:Type' => 'String'
end
xml.Cell do
xml.Data customer.created_at.to_s, 'ss:Type' => 'String'
end
xml.Cell do
xml.Data @orders[customer.id], 'ss:Type' => 'Number'
end
# OTHER CELLS AS NEEDED
end
end
end # xml.Table
end # xml.Worksheet
end # xml.Workbook
<h1>New Customers</h1>
<%= start_form_tag(:action => "execute") %>
<p>
<label for="report_start_date">Start Date:</label><br/>
<%= date_select :report, :start_date %>
</p>
<p>
<label for="report_end_date">End Date:</label><br/>
<%= date_select :report, :end_date %>
</p>
<%= submit_tag "Execute" %>
<%= end_form_tag %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment