cpjolicoeur (owner)

Revisions

gist: 44470 Download_button fork
public
Description:
problems with mock/stubbing the query data
Public Clone URL: git://gist.github.com/44470.git
Embed All Files: show embed
excel_export.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
 
excel_export_spec.rb #
1
2
3
4
5
6
7
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
 
occupancy_data_dump.yml #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
---
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: {}