dossier 2.1.1 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +20 -4
- data/app/controllers/dossier/reports_controller.rb +5 -0
- data/config/initializers/mime_types.rb +1 -0
- data/lib/dossier/version.rb +1 -1
- data/lib/dossier/xls.rb +34 -0
- data/lib/dossier.rb +1 -0
- data/spec/dummy/config/database.yml +1 -1
- data/spec/dummy/db/test.sqlite3 +0 -0
- data/spec/dummy/log/development.log +0 -32
- data/spec/dummy/log/test.log +231 -26330
- data/spec/fixtures/db/mysql2.yml +2 -2
- data/spec/fixtures/reports/employee.xls +39 -0
- data/spec/requests/employee_spec.rb +9 -0
- metadata +8 -4
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
|
6
|
+
- If you **love** SQL, you can use every feature your database supports.
|
7
7
|
|
8
8
|
[](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
|
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
|
data/lib/dossier/version.rb
CHANGED
data/lib/dossier/xls.rb
ADDED
@@ -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
data/spec/dummy/db/test.sqlite3
CHANGED
Binary file
|
@@ -1,33 +1 @@
|
|
1
|
-
Connecting to database specified by database.yml
|
2
|
-
[1m[36m (45.0ms)[0m [1mCREATE TABLE `employees` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `name` varchar(255), `hired_on` date, `suspended` tinyint(1)) ENGINE=InnoDB[0m
|
3
|
-
[1m[35m (217.3ms)[0m CREATE TABLE `schema_migrations` (`version` varchar(255) NOT NULL) ENGINE=InnoDB
|
4
|
-
[1m[36m (141.9ms)[0m [1mCREATE UNIQUE INDEX `unique_schema_migrations` ON `schema_migrations` (`version`)[0m
|
5
|
-
[1m[35m (0.2ms)[0m SELECT version FROM `schema_migrations`
|
6
|
-
[1m[36m (0.3ms)[0m [1mINSERT INTO `schema_migrations` (version) VALUES ('20130110141950')[0m
|
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
|
-
[1m[36m (1.5ms)[0m [1mSELECT `schema_migrations`.`version` FROM `schema_migrations` [0m
|
11
|
-
Migrating to CreateEmployees (20130110141950)
|
12
|
-
Migrating to AddDivisionToEmployees (20130110221932)
|
13
|
-
[1m[35m (109.6ms)[0m ALTER TABLE `employees` ADD `division` varchar(255)
|
14
|
-
[1m[36m (0.5ms)[0m [1mINSERT INTO `schema_migrations` (`version`) VALUES ('20130110221932')[0m
|
15
|
-
[1m[35m (0.2ms)[0m SELECT `schema_migrations`.`version` FROM `schema_migrations`
|
16
|
-
Connecting to database specified by database.yml
|
17
|
-
[1m[36m (1.7ms)[0m [1mSELECT `schema_migrations`.`version` FROM `schema_migrations` [0m
|
18
|
-
[1m[35m (0.3ms)[0m SELECT `schema_migrations`.`version` FROM `schema_migrations`
|
19
|
-
Migrating to AddDivisionToEmployees (20130110221932)
|
20
|
-
[1m[36m (85.8ms)[0m [1mALTER TABLE `employees` DROP `division`[0m
|
21
|
-
[1m[35m (0.6ms)[0m DELETE FROM `schema_migrations` WHERE `schema_migrations`.`version` = '20130110221932'
|
22
|
-
[1m[36m (0.1ms)[0m [1mSELECT `schema_migrations`.`version` FROM `schema_migrations` [0m
|
23
|
-
Connecting to database specified by database.yml
|
24
|
-
[1m[36m (1.6ms)[0m [1mSELECT `schema_migrations`.`version` FROM `schema_migrations` [0m
|
25
|
-
Migrating to CreateEmployees (20130110141950)
|
26
|
-
Migrating to AddDivisionToEmployees (20130110221932)
|
27
|
-
[1m[35m (50.1ms)[0m ALTER TABLE `employees` ADD `division` varchar(255)
|
28
|
-
[1m[36m (238.4ms)[0m [1mALTER TABLE `employees` ADD `salary` int(11)[0m
|
29
|
-
[1m[35m (0.4ms)[0m INSERT INTO `schema_migrations` (`version`) VALUES ('20130110221932')
|
30
|
-
[1m[36m (0.3ms)[0m [1mSELECT `schema_migrations`.`version` FROM `schema_migrations` [0m
|
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
|