public
Description: create reports via sql or ruby code for your rails app in minutes
Homepage: http://upstream-berlin.com/blog/open-source/#dead_simple_reports
Clone URL: git://github.com/langalex/dead_simple_reports.git
Search Repo:
added excel format reports via the spreadsheet-excel gem, added specs, 
changed install to copy fewer files into the app so that updates are 
easier
Alexander Lang (author)
Sat Apr 12 12:51:43 -0700 2008
commit  61fea284d033406dab26044912dec36e652a0327
tree    90393b0b4a65eab7886ba2db6a7569a3ad619e64
parent  1ce6c51ed41b92df86e66bf14ce8aa3db5831161
...
 
1
...
1
2
0
@@ -1 +1,2 @@
0
+Mime::Type.register "application/vnd.ms-excel", :xls
0
 
...
1
2
3
4
 
5
6
7
8
9
...
1
2
3
 
4
5
6
 
7
8
0
@@ -1,8 +1,7 @@
0
 require 'fileutils'
0
 
0
 dir = File.dirname(__FILE__)
0
-FileUtils.cp Dir[File.join(dir, 'lib', 'controllers', '*.rb')], File.join(dir, '../../../app/controllers')
0
+FileUtils.cp File.join(dir, 'lib', 'controllers', 'reports_controller.rb.template'), File.join(dir, '../../../app/controllers/reports_controller.rb')
0
 FileUtils.cp_r Dir[File.join(dir, 'views', '*')], File.join(dir, '../../../app/views')
0
 
0
-
0
 puts IO.read(File.join(File.dirname(__FILE__), 'INSTALL'))
0
\ No newline at end of file
...
 
 
 
1
 
2
3
4
...
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
...
1
2
3
4
5
6
7
8
...
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
0
@@ -1,4 +1,8 @@
0
+require "spreadsheet/excel"
0
+require 'iconv'
0
+
0
 class ReportsController < ApplicationController
0
+ include Spreadsheet
0
 
0
   def index
0
     @reports = Report.find :all
0
@@ -6,27 +10,52 @@ class ReportsController < ApplicationController
0
   
0
   def show
0
     @report = Report.find(params[:id])
0
- @results = @report.run
0
+ data = @report.run
0
     respond_to do |format|
0
       format.html do
0
- if @results.is_a? Array
0
- @results = @results.inject('<table>') do |table, row|
0
- table << '<tr>'
0
- table << row.inject('') {|_row, cell| _row << "<td>#{cell.to_s}</td>"}
0
- table << '</tr>'
0
- end << '</table>'
0
- end
0
+ @table = create_html_table data
0
       end
0
       format.csv do
0
- if @results.is_a? Array
0
- @results = @results.inject('') do |table, row|
0
- table << row.map {|cell| cell.to_s.gsub(/\s+/, ' ')}.join(';')
0
- table << "\n"
0
- end
0
- end
0
- render :text => @results
0
+ render :text => create_csv_table(data)
0
+ headers['Content-Disposition'] = "attachment; filename=\"#{@report.name.downcase.gsub(/\s+/, '_')}_#{Date.today.to_s}.csv\""
0
+ end
0
+ format.xls do
0
+ render :text => create_excel_table(data)
0
+ headers['Content-Disposition'] = "attachment; filename=\"#{@report.name.downcase.gsub(/\s+/, '_')}_#{Date.today.to_s}.xls\""
0
+ headers['Cache-Control'] = ''
0
+ end
0
+ end
0
+ end
0
+
0
+ def create_html_table(_table)
0
+ _table.inject('<table>') do |table, row|
0
+ table << '<tr>'
0
+ table << row.inject('') {|_row, cell| _row << "<td>#{cell.to_s}</td>"}
0
+ table << '</tr>'
0
+ end << '</table>'
0
+ end
0
+
0
+ def create_csv_table(_table)
0
+ _table.inject('') do |table, row|
0
+ table << row.map {|cell| cell.to_s.gsub(/\s+/, ' ')}.join(';')
0
+ table << "\n"
0
+ end
0
+ end
0
+
0
+ def create_excel_table(_table)
0
+ out = StringIO.new
0
+ workbook = Excel.new(out)
0
+ format = Format.new
0
+
0
+ worksheet = workbook.add_worksheet
0
+ _table.each_with_index do |row, i|
0
+ row.each_with_index do |col, j|
0
+ worksheet.write i, j, Iconv.conv('ISO-8859-1', 'UTF-8', col.to_s), format # excel doesn't seem to like utf-8
0
       end
0
     end
0
+
0
+ workbook.close
0
+ out.string
0
   end
0
   
0
 end
...
10
11
12
13
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
...
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
0
@@ -10,4 +10,49 @@ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => File
0
 # migrate
0
 
0
 require File.dirname(__FILE__) + '/../generators/dead_simple_reports/templates/migration'
0
-CreateDeadSimpleReports.up
0
\ No newline at end of file
0
+CreateDeadSimpleReports.up
0
+
0
+# rails controller stuff
0
+
0
+class ApplicationController
0
+ class MimeTypeFormat
0
+
0
+ [:html, :csv, :xls].each do |mime_type|
0
+
0
+ eval <<-CODE
0
+ def #{mime_type}(&block)
0
+ @#{mime_type}_block = block
0
+ end
0
+
0
+ def #{mime_type}_view
0
+ @#{mime_type}_block.call
0
+ end
0
+
0
+ CODE
0
+
0
+ end
0
+
0
+ end
0
+
0
+ def respond_to(&formatter)
0
+ @format = MimeTypeFormat.new
0
+ formatter.call @format
0
+ @format
0
+ end
0
+
0
+ def params
0
+ {}
0
+ end
0
+
0
+ def headers
0
+ {}
0
+ end
0
+
0
+ def render(options = {})
0
+ @view = options[:text]
0
+ end
0
+
0
+ def view
0
+ @view
0
+ end
0
+end
0
\ No newline at end of file
...
2
3
4
5
 
 
 
 
 
 
 
6
7
8
...
2
3
4
 
5
6
7
8
9
10
11
12
13
14
0
@@ -2,6 +2,12 @@
0
 
0
 <ol>
0
   <%- @reports.each do |report| -%>
0
- <li><%= link_to h(report.name), :action => 'show', :id => report.id %> (<%= link_to 'CSV', :action => 'show', :id => report.id, :format => 'csv' %>)</li>
0
+ <li>
0
+ <%= link_to h(report.name), :action => 'show', :id => report.id %>
0
+ (
0
+ <%= link_to 'CSV', :action => 'show', :id => report.id, :format => 'csv' %>
0
+ <%= link_to 'Excel', :action => 'show', :id => report.id, :format => 'xls' %>
0
+ )
0
+ </li>
0
   <%- end -%>
0
 </ol>
0
\ No newline at end of file
...
1
2
3
 
4
5
6
7
8
9
10
11
12
13
14
15
16
 
17
...
1
2
 
3
4
 
 
 
 
 
 
 
 
 
 
 
 
5
6
0
@@ -1,17 +1,6 @@
0
 <h1><%= h @report.name %></h1>
0
 
0
-<p>Format: <%= link_to 'CSV', :format => 'csv' %></p>
0
+<p>Format: <%= link_to 'CSV', :format => 'csv' %>, <%= link_to 'Excel', :format => 'xls' %></p>
0
 
0
-<style>
0
- table {
0
- border-collapse: collapse;
0
- }
0
-
0
- td {
0
- border: 1px solid black;
0
- padding: 3px;
0
- }
0
-</style>
0
-
0
-<%= @results %>
0
+<%= @table %>
0
 

Comments

    No one has commented yet.