dossier 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,20 +1,31 @@
1
1
  # Dossier
2
2
 
3
- Dossier is a Rails engine that turns SQL into reports. Reports can be easily rendered in various formats, like HTML, CSV, and JSON.
3
+ Dossier is a Rails engine that turns SQL into reports. Reports can be easily rendered in various formats, like HTML, CSV, XLS, and JSON.
4
4
 
5
5
  - If you **hate** SQL, you can use whatever tool you like to generate it; for example, ActiveRecord's `to_sql`.
6
- - If you **love** SQL, you can use every feature feature your database supports.
6
+ - If you **love** SQL, you can use every feature your database supports.
7
7
 
8
8
  [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/adamhunter/dossier)
9
9
 
10
10
  ## Setup
11
11
 
12
- Install the Dossier gem and create `config/dossier.yml`. This has the same format as Rails' `database.yml`, and can actually just be a symlink: `ln -s config/{database,dossier}.yml`.
12
+ Install the Dossier gem and create `config/dossier.yml`. This has the same format as Rails' `database.yml`, and can actually just be a symlink (from your `Rails.root`: `ln -s database.yml config/dossier.yml`).
13
13
 
14
14
  ## Routing
15
15
 
16
16
  Dossier will add a route to your app so that `reports/fancy_ketchup` will instantiate and run a `FancyKetchupReport`. It will respond with whatever format was requested; for example `reports/fancy_ketchup.csv` will render the results as CSV.
17
17
 
18
+ ## Formats
19
+
20
+ Dossier currently supports outputting to the following formats:
21
+
22
+ - HTML
23
+ - CSV
24
+ - XLS
25
+ - JSON
26
+
27
+ Any of these formats can be requested by using the appropriate format extension on the end of the report's URL.
28
+
18
29
  ## Basic Reports
19
30
 
20
31
  In your app, create report classes under `app/reports`, with `Report` as the end of the class name. Define a `sql` method that returns the sql string to be sent to the database.
@@ -91,7 +102,7 @@ class LeastProfitableAccountsReport < Dossier::Report
91
102
  end
92
103
  ```
93
104
 
94
- The built-in `ReportsController` uses this formatting when rendering the HTML and JSON representations, but not when rendering the CSV.
105
+ The built-in `ReportsController` uses this formatting when rendering the HTML and JSON representations, but not when rendering the CSV or XLS.
95
106
 
96
107
  If your formatting method takes a second argment, it will be given a hash of the values in the row.
97
108
 
@@ -209,3 +220,8 @@ Note: when you run the tests, Dossier will **make and/or truncate** some tables
209
220
  - To other formats
210
221
  - Extending the formatter
211
222
  - Show how to do "crosstab" reports (preliminary query to determine columns, then build SQL case statements?)
223
+
224
+ ## Roadmap
225
+
226
+ - Moar Dokumentationz pleaze
227
+ - Use the [`roo`](https://github.com/hmcgowan/roo) gem to generate a variety of output formats
@@ -21,6 +21,11 @@ module Dossier
21
21
  headers["Content-Disposition"] = %[attachment;filename=#{params[:report]}-report_#{Time.now.strftime('%m-%d-%Y-%H%M%S')}.csv]
22
22
  self.response_body = StreamCSV.new(report.raw_results.arrays)
23
23
  end
24
+
25
+ format.xls do
26
+ headers["Content-Disposition"] = %[attachment;filename=#{params[:report]}-report_#{Time.now.strftime('%m-%d-%Y-%H%M%S')}.xls]
27
+ self.response_body = Xls.new(report.raw_results.arrays)
28
+ end
24
29
  end
25
30
  end
26
31
 
@@ -0,0 +1 @@
1
+ Mime::Type.register "application/xls", :xls
@@ -1,3 +1,3 @@
1
1
  module Dossier
2
- VERSION = "2.1.1"
2
+ VERSION = "2.2.0"
3
3
  end
@@ -0,0 +1,34 @@
1
+ module Dossier
2
+ class Xls
3
+
4
+ HEADER = %Q{<?xml version="1.0" encoding="UTF-8"?>\n<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:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">\n<Worksheet ss:Name="Sheet1">\n<Table>\n}
5
+ FOOTER = %Q{</Table>\n</Worksheet>\n</Workbook>\n}
6
+
7
+ def initialize(collection, headers = nil)
8
+ @headers = headers || collection.shift
9
+ @collection = collection
10
+ end
11
+
12
+ def each
13
+ yield HEADER
14
+ yield headers_as_row
15
+ @collection.each { |record| yield as_row(record) }
16
+ yield FOOTER
17
+ end
18
+
19
+ private
20
+
21
+ def as_cel(el)
22
+ %{<Cell><Data ss:Type="String">#{el}</Data></Cell>}
23
+ end
24
+
25
+ def as_row(array)
26
+ my_array = array.map{|a| as_cel(a)}.join("\n")
27
+ "<Row>\n" + my_array + "\n</Row>\n"
28
+ end
29
+
30
+ def headers_as_row
31
+ as_row(@headers.map { |header| Dossier::Formatter.titleize(header) })
32
+ end
33
+ end
34
+ end
data/lib/dossier.rb CHANGED
@@ -29,3 +29,4 @@ require "dossier/query"
29
29
  require "dossier/report"
30
30
  require "dossier/result"
31
31
  require "dossier/stream_csv"
32
+ require "dossier/xls"
@@ -3,7 +3,7 @@ defaults: &defaults
3
3
  database: dossier_test
4
4
  host: localhost
5
5
  username: root
6
- password: yePassworde
6
+ password: rubyr3d!
7
7
 
8
8
  development:
9
9
  <<: *defaults
Binary file
@@ -1,33 +1 @@
1
- Connecting to database specified by database.yml
2
-  (45.0ms) CREATE TABLE `employees` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255), `hired_on` date, `suspended` tinyint(1)) ENGINE=InnoDB
3
-  (217.3ms) CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB
4
-  (141.9ms) CREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)
5
-  (0.2ms) SELECT version FROM `schema_migrations`
6
-  (0.3ms) INSERT INTO `schema_migrations` (version) VALUES ('20130110141950')
7
- Connecting to database specified by database.yml
8
- Connecting to database specified by database.yml
9
- Connecting to database specified by database.yml
10
-  (1.5ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` 
11
- Migrating to CreateEmployees (20130110141950)
12
- Migrating to AddDivisionToEmployees (20130110221932)
13
-  (109.6ms) ALTER TABLE `employees` ADD `division` varchar(255)
14
-  (0.5ms) INSERT INTO `schema_migrations` (`version`) VALUES ('20130110221932')
15
-  (0.2ms) SELECT `schema_migrations`.`version` FROM `schema_migrations`
16
- Connecting to database specified by database.yml
17
-  (1.7ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` 
18
-  (0.3ms) SELECT `schema_migrations`.`version` FROM `schema_migrations`
19
- Migrating to AddDivisionToEmployees (20130110221932)
20
-  (85.8ms) ALTER TABLE `employees` DROP `division`
21
-  (0.6ms) DELETE FROM `schema_migrations` WHERE `schema_migrations`.`version` = '20130110221932'
22
-  (0.1ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` 
23
- Connecting to database specified by database.yml
24
-  (1.6ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` 
25
- Migrating to CreateEmployees (20130110141950)
26
- Migrating to AddDivisionToEmployees (20130110221932)
27
-  (50.1ms) ALTER TABLE `employees` ADD `division` varchar(255)
28
-  (238.4ms) ALTER TABLE `employees` ADD `salary` int(11)
29
-  (0.4ms) INSERT INTO `schema_migrations` (`version`) VALUES ('20130110221932')
30
-  (0.3ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` 
31
- Connecting to database specified by database.yml
32
- Connecting to database specified by database.yml
33
1
  Connecting to database specified by database.yml