Skip to content

Instantly share code, notes, and snippets.

@cpjolicoeur
Created January 7, 2009 22:06
Show Gist options
  • Save cpjolicoeur/44470 to your computer and use it in GitHub Desktop.
Save cpjolicoeur/44470 to your computer and use it in GitHub Desktop.
problems with mock/stubbing the query data
require 'spreadsheet'
class ExcelExport
# http://spreadsheet.rubyforge.org/files/GUIDE_txt.html
# http://spreadsheet.rubyforge.org/
# http://github.com/jacobat/ruby-spreadsheet/tree/master
class << self
#
# This method will take all export all queries from a report.
# * Each query will be a seperate workbook sheet
#
def full_report_export(site, report, query_data)
workbook = Spreadsheet::Workbook.new
# create a new worksheet for each query
query_data.each_key do |key|
sheet = workbook.create_worksheet( :name => key.to_s )
# set the header row
sheet.row(0).concat query_data[key].first.instance_variable_get(:@attributes).keys
# populate the data rows
query_data[key].each_with_index do | arr, idx |
sheet.row(idx+1).concat arr.instance_variable_get(:@attributes).values
end
end
prefix = Rails.env.production? ? '' : "#{Rails.env}-"
file_path = "#{RAILS_ROOT}/public/system/export/#{prefix}#{site.slug}-#{report.slug}-report-export.xls"
workbook.write file_path
# return the file path to the controller
file_path
end
end
end
before(:all) do
@report = mock_model(Report, :slug => 'occupancy')
@report.stub!(:query_data).and_return( YAML.load_file( "#{RAILS_ROOT}/spec/data_dumps/occupancy.yml") )
@site = mock_model(Site, :slug => 'chatswood', :dw_site_id => 1)
@query_data = @report.query_data( :site_id => @site.dw_site_id, :date1 => 1.week.ago.to_s(:dw) , :date2 => 1.day.ago.to_s(:dw) )
end
---
average-occupancy-by-zone:
- !ruby/object:DataWarehouse
attributes:
AVG_OCCUPANCY_PCT: "20.24"
ZONE_NAME: L1
attributes_cache: {}
- !ruby/object:DataWarehouse
attributes:
AVG_OCCUPANCY_PCT: "26.73"
ZONE_NAME: L2
attributes_cache: {}
- !ruby/object:DataWarehouse
attributes:
AVG_OCCUPANCY_PCT: "29.45"
ZONE_NAME: L3
attributes_cache: {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment