to_spreadsheet 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 776455eae95d6fc809d03558bc270aead0bb0c57
4
- data.tar.gz: f38ea16841baabc6bcccfe42eb3d01d8d1d4d624
3
+ metadata.gz: b8caf02160bc192777511420e81ac8df803f8f6d
4
+ data.tar.gz: 393535d43b6e6563ed1e7831532bd607ad485fda
5
5
  SHA512:
6
- metadata.gz: acf617a13f77b92278751e2ffc70516d00a7e026c892c13d6deb36c8fe63f6754428ad032a696ba2387183fcf245775266364ded8614ce915fef2375180652e6
7
- data.tar.gz: 0dd83cfc2f159be301559c61861f3eae5eb1f0e60fb297226d368ba84df6cd003aefdfd6d893a28d8541032de50c5c2955c19f9e86b3bab11c636f74492de6f8
6
+ metadata.gz: c0c0ef72af612b307b511e3235c8f91656d56a976855672a8883072b9a0932276192ca68ee4a038fd8640c18414cbb0301bf53f16032fb18bfa9fd16c19bdc11
7
+ data.tar.gz: 430257b549ee502de4860ea92404e8b97e7182af9fa955a9e19a693311422d10003e432463aa8ad501eb55c197a7877342842a247ff4b33cd6d2da49f5a111a5
data/README.md ADDED
@@ -0,0 +1,149 @@
1
+ # to_spreadsheet [![Build Status](https://secure.travis-ci.org/glebm/to_spreadsheet.png?branch=master)](http://travis-ci.org/glebm/to_spreadsheet)
2
+
3
+ to_spreadsheet lets your Rails 3+ app render Excel files using the existing slim/haml/erb/etc views.
4
+
5
+ Installation
6
+ ------------
7
+
8
+ Add it to your Gemfile:
9
+ ```ruby
10
+ gem 'to_spreadsheet'
11
+ ```
12
+
13
+ Usage
14
+ -----
15
+
16
+ In the controller:
17
+ ```ruby
18
+ # my_thingies_controller.rb
19
+ class MyThingiesController < ApplicationController
20
+ respond_to :xls, :html
21
+ def index
22
+ @my_items = MyItem.all
23
+ respond_to do |format|
24
+ format.html
25
+ format.xlsx { render xlsx: :index, filename: "my_items_doc" }
26
+ end
27
+ end
28
+ end
29
+ ```
30
+
31
+ In the view partial:
32
+ ```haml
33
+ # _my_items.haml
34
+ %table
35
+ %caption My items
36
+ %thead
37
+ %tr
38
+ %td ID
39
+ %td Name
40
+ %tbody
41
+ - my_items.each do |my_item|
42
+ %tr
43
+ %td.number= my_item.id
44
+ %td= my_item.name
45
+ %tfoot
46
+ %tr
47
+ %td(colspan="2") #{my_items.length}
48
+ ```
49
+
50
+ In the XLSX view:
51
+ ```haml
52
+ # index.xls.haml
53
+ = render 'my_items', my_items: @my_items
54
+ ```
55
+
56
+ In the HTML view:
57
+ ```haml
58
+ # index.html.haml
59
+ = link_to 'Download spreadsheet', my_items_url(format: :xlsx)
60
+ = render 'my_items', my_items: @my_items
61
+ ```
62
+
63
+ ### Worksheets
64
+
65
+ Every table in the view will be converted to a separate sheet.
66
+ The sheet title will be assigned to the value of the table’s caption element if it exists.
67
+
68
+ ### Formatting
69
+
70
+ You can define formats in your view file (local to the view) or in the initializer
71
+
72
+ ```ruby
73
+ format_xls 'table.my-table' do
74
+ workbook use_autowidth: true
75
+ sheet orientation: landscape
76
+ format 'th', b: true # bold
77
+ format 'tbody tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
78
+ format 'A3:B10', i: true # italic
79
+ format column: 0, width: 35
80
+ format 'td.custom', lambda { |cell| modify cell somehow.}
81
+ # default value (fallback value when value is blank or 0 for integer / float)
82
+ default 'td.price', 10
83
+ end
84
+ ```
85
+
86
+ For the full list of supported properties head here: http://rubydoc.info/github/randym/axlsx/Axlsx/Styles#add_style-instance_method
87
+ In addition, for column formats, Axlsx columnInfo properties are also supported
88
+
89
+ ### Advanced formatting
90
+
91
+ to_spreadsheet [associates](https://github.com/glebm/to_spreadsheet/blob/master/lib/to_spreadsheet/renderer.rb#L33) HTML nodes with Axlsx objects as follows:
92
+
93
+ | HTML tag | Axlsx object |
94
+ |----------|--------------|
95
+ | table | worksheet |
96
+ | tr | row |
97
+ | td, th | cell |
98
+
99
+ For example, to directly manipulate a worksheet:
100
+ ```ruby
101
+ format_xls do
102
+ format 'table' do |worksheet|
103
+ worksheet.add_chart ...
104
+ # to get the associated Nokogiri node:
105
+ el = context.to_xml_node(worksheet)
106
+ end
107
+ end
108
+ ```
109
+
110
+ ### Themes
111
+
112
+ You can define themes, i.e. blocks of formatting code:
113
+ ```ruby
114
+ ToSpreadsheet.theme :zebra do
115
+ format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
116
+ end
117
+ ```
118
+
119
+ And then use them:
120
+ ```ruby
121
+ format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)
122
+ ```
123
+
124
+ ### Using along side axlsx-rails
125
+ If you are using [axlsx-rails](https://github.com/straydogstudio/axlsx_rails), :xlsx renderer might have already been defined. In that case define a custome renderer using
126
+ ```ruby
127
+ # app/config/application.rb
128
+ config.to_spreadsheet.renderer = :html2xlsx
129
+ ```
130
+
131
+ And then in controller
132
+ ```ruby
133
+ respond_to do |format|
134
+ format.html2xlsx
135
+ end
136
+ ```
137
+
138
+ ### Types
139
+
140
+ The default theme uses class names on td/th to cast values.
141
+ Here is the list of class to type mapping:
142
+
143
+ | CSS class | Format |
144
+ |------------------|--------------------------|
145
+ | decimal or float | Decimal |
146
+ | num or int | Integer |
147
+ | datetime | DateTime (Chronic.parse) |
148
+ | date | Date (Date.parse) |
149
+ | time | Time (Chronic.parse) |
data/Rakefile CHANGED
@@ -21,3 +21,12 @@ task :write_test_xlsx => :env do
21
21
  ToSpreadsheet::Renderer.to_package(html).serialize(path)
22
22
  puts "Written to #{path}"
23
23
  end
24
+
25
+ desc "Test all rails versions"
26
+ task :test_all_rails do
27
+ Dir.glob("./spec/gemfiles/*{[!.lock]}").each do |gemfile|
28
+ puts "TESTING WITH #{gemfile}"
29
+ system "BUNDLE_GEMFILE=#{gemfile} bundle | grep Installing"
30
+ system "BUNDLE_GEMFILE=#{gemfile} bundle exec rspec"
31
+ end
32
+ end
@@ -1,10 +1,13 @@
1
1
  require 'to_spreadsheet/version'
2
2
  require 'to_spreadsheet/context'
3
3
  require 'to_spreadsheet/renderer'
4
- require 'to_spreadsheet/railtie' if defined?(Rails)
5
4
 
6
5
  module ToSpreadsheet
7
6
  class << self
7
+ def renderer
8
+ @renderer ||= :xlsx
9
+ end
10
+
8
11
  def theme(name, &formats)
9
12
  @themes ||= {}
10
13
  if formats
@@ -16,5 +19,6 @@ module ToSpreadsheet
16
19
  end
17
20
  end
18
21
 
22
+ require 'to_spreadsheet/railtie' if defined?(Rails)
19
23
  require 'to_spreadsheet/themes/default'
20
24
  ToSpreadsheet::Context.global.format_xls ToSpreadsheet.theme(:default)
@@ -1,16 +1,26 @@
1
1
  require 'active_support'
2
2
  require 'action_controller/metal/renderers'
3
- require 'action_controller/responder'
3
+
4
+ # in rails 3.2 it's ActiveSupport::VERSION
5
+ # in rails 4.0+ it's ActiveSupport.version (instance of Gem::Version)
6
+ if ActiveSupport.respond_to?(:version) && ActiveSupport.version.to_s >= "4.2.0"
7
+ # For rails 4.2
8
+ require 'action_controller/responder'
9
+ else
10
+ # For rails 3.2 - rails 4.1
11
+ require 'action_controller/metal/responder'
12
+ end
13
+
4
14
 
5
15
  # This will let us do thing like `render :xlsx => 'index'`
6
16
  # This is similar to how Rails internally implements its :json and :xml renderers
7
- ActionController::Renderers.add :xlsx do |template, options|
17
+ ActionController::Renderers.add ToSpreadsheet.renderer do |template, options|
8
18
  filename = options[:filename] || options[:template] || 'data'
9
19
  data = ToSpreadsheet::Context.with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do |context|
10
20
  html = render_to_string(template, options.merge(template: template.to_s, formats: ['xlsx', 'html']))
11
21
  ToSpreadsheet::Renderer.to_data(html, context)
12
22
  end
13
- send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx")
23
+ send_data data, type: ToSpreadsheet.renderer, disposition: %(attachment; filename="#{filename}.xlsx")
14
24
  end
15
25
 
16
26
  class ActionController::Responder
@@ -18,7 +28,7 @@ class ActionController::Responder
18
28
  # respond_to do |format|
19
29
  # format.xlsx
20
30
  # end
21
- def to_xlsx
22
- controller.render xlsx: controller.action_name
31
+ define_method "to_#{ToSpreadsheet.renderer}" do
32
+ controller.render ToSpreadsheet.renderer => controller.action_name
23
33
  end
24
34
  end
@@ -1,2 +1,2 @@
1
1
  require 'action_dispatch/http/mime_type'
2
- Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", :xlsx unless Mime::Type.lookup_by_extension(:xlsx)
2
+ Mime::Type.register "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ToSpreadsheet.renderer unless Mime::Type.lookup_by_extension(ToSpreadsheet.renderer)
@@ -1,10 +1,15 @@
1
1
  require 'rails/railtie'
2
- require 'to_spreadsheet/rails/mime_types'
3
- require 'to_spreadsheet/rails/action_pack_renderers'
4
- require 'to_spreadsheet/rails/view_helpers'
5
2
  module ToSpreadsheet
6
3
  class Railtie < ::Rails::Railtie
7
- initializer "to_spreadsheet.view_helpers" do
4
+ config.to_spreadsheet = ActiveSupport::OrderedOptions.new
5
+ config.to_spreadsheet.renderer = ToSpreadsheet.renderer
6
+
7
+ config.after_initialize do |app|
8
+ ToSpreadsheet.instance_variable_set("@renderer", app.config.to_spreadsheet.renderer)
9
+
10
+ require 'to_spreadsheet/rails/action_pack_renderers'
11
+ require 'to_spreadsheet/rails/view_helpers'
12
+ require 'to_spreadsheet/rails/mime_types'
8
13
  ActionView::Base.send :include, ToSpreadsheet::Rails::ViewHelpers
9
14
  end
10
15
  end
@@ -1,3 +1,3 @@
1
1
  module ToSpreadsheet
2
- VERSION = '1.0.5'
2
+ VERSION = '1.0.6'
3
3
  end
@@ -0,0 +1,10 @@
1
+ require 'to_spreadsheet/railtie'
2
+
3
+ describe ToSpreadsheet::Railtie do
4
+
5
+ it "registers a renderer" do
6
+ expect(ToSpreadsheet.renderer).to eq(:html2xlsx)
7
+ expect(ActionController::Renderers::RENDERERS).to include(ToSpreadsheet.renderer)
8
+ end
9
+
10
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: to_spreadsheet
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Mazovetskiy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-28 00:00:00.000000000 Z
11
+ date: 2015-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,15 +108,29 @@ dependencies:
108
108
  - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: combustion
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
111
125
  description: Render XLSX from Rails using existing views ( .*.html => .xlsx )
112
126
  email: glex.spb@gmail.com
113
127
  executables: []
114
128
  extensions: []
115
129
  extra_rdoc_files:
116
- - README.textile
130
+ - README.md
117
131
  files:
118
132
  - LICENSE
119
- - README.textile
133
+ - README.md
120
134
  - Rakefile
121
135
  - lib/to_spreadsheet.rb
122
136
  - lib/to_spreadsheet/context.rb
@@ -139,6 +153,7 @@ files:
139
153
  - lib/to_spreadsheet/version.rb
140
154
  - spec/defaults_spec.rb
141
155
  - spec/format_spec.rb
156
+ - spec/rails_integration_spec.rb
142
157
  - spec/types_spec.rb
143
158
  - spec/worksheets_spec.rb
144
159
  homepage: https://github.com/glebm/to_spreadsheet
@@ -160,13 +175,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
175
  version: '0'
161
176
  requirements: []
162
177
  rubyforge_project:
163
- rubygems_version: 2.4.6
178
+ rubygems_version: 2.4.8
164
179
  signing_key:
165
180
  specification_version: 4
166
181
  summary: Render existing views as Excel documents with style!
167
182
  test_files:
168
- - spec/defaults_spec.rb
169
183
  - spec/format_spec.rb
184
+ - spec/rails_integration_spec.rb
170
185
  - spec/types_spec.rb
186
+ - spec/defaults_spec.rb
171
187
  - spec/worksheets_spec.rb
172
- has_rdoc: true
data/README.textile DELETED
@@ -1,116 +0,0 @@
1
- to_spreadsheet is a gem that lets you render xls from your existing slim/haml/erb/etc views from Rails (>= 3.0). !https://secure.travis-ci.org/glebm/to_spreadsheet.png?branch=master(Build Status)!:http://travis-ci.org/glebm/to_spreadsheet
2
-
3
- h2. Installation
4
-
5
- Add it to your Gemfile:
6
-
7
- bc. gem 'to_spreadsheet'
8
-
9
- h2. Usage
10
-
11
- In your controller:
12
-
13
- bc. # my_thingies_controller.rb
14
- class MyThingiesController < ApplicationController
15
- respond_to :xls, :html
16
- def index
17
- @my_items = MyItem.all
18
- respond_to do |format|
19
- format.html
20
- format.xlsx { render xlsx: :index, filename: "my_items_doc" }
21
- end
22
- end
23
- end
24
-
25
- In your view partial:
26
-
27
- bc. # _my_items.haml
28
- %table
29
- %caption My items
30
- %thead
31
- %tr
32
- %td ID
33
- %td Name
34
- %tbody
35
- - my_items.each do |my_item|
36
- %tr
37
- %td.number= my_item.id
38
- %td= my_item.name
39
- %tfoot
40
- %tr
41
- %td(colspan="2") #{my_items.length}
42
-
43
- In your index.xls.haml:
44
-
45
- bc. # index.xls.haml
46
- = render 'my_items', my_items: @my_items
47
-
48
- In your index.html.haml:
49
-
50
- bc. # index.html.haml
51
- = link_to 'Download spreadsheet', my_items_url(format: :xlsx)
52
- = render 'my_items', my_items: @my_items
53
-
54
- h3. Worksheets
55
-
56
- Every table in the view will be converted to a separate sheet.
57
- The sheet title will be assigned to the value of the table's caption element if it exists.
58
-
59
- h3. Formatting
60
-
61
- You can define formats in your view file (local to the view) or in the initializer
62
-
63
- bc. format_xls 'table.my-table' do
64
- workbook use_autowidth: true
65
- sheet orientation: landscape
66
- format 'th', b: true # bold
67
- format 'tbody tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
68
- format 'A3:B10', i: true # italic
69
- format column: 0, width: 35
70
- format 'td.custom', lambda { |cell| modify cell somehow.}
71
- # default value (fallback value when value is blank or 0 for integer / float)
72
- default 'td.price', 10
73
-
74
- For the full list of supported properties head here: http://rubydoc.info/github/randym/axlsx/Axlsx/Styles#add_style-instance_method
75
- In addition, for column formats, Axlsx columnInfo properties are also supported
76
-
77
- h3. Advanced formatting
78
-
79
- to_spreadsheet "associates":https://github.com/glebm/to_spreadsheet/blob/master/lib/to_spreadsheet/renderer.rb#L33 HTML nodes with Axlsx objects as follows:
80
-
81
- |_. HTML tag |_. Axlsx object |
82
- | table | worksheet |
83
- | tr | row |
84
- | td, th | cell |
85
-
86
- For example, to directly manipulate a worksheet:
87
-
88
- bc. format_xls do
89
- format 'table' do |worksheet|
90
- worksheet.add_chart ...
91
- # to get the associated Nokogiri node:
92
- el = context.to_xml_node(worksheet)
93
-
94
-
95
- h3. Themes
96
-
97
- You can define "themes" - blocks of formatting code:
98
-
99
- bc. ToSpreadsheet.theme :zebra do
100
- format 'tr', bg_color: lambda { |row| 'ddffdd' if row.index.odd? }
101
-
102
- And then use them:
103
-
104
- bc. format_xls 'table.zebra', ToSpreadsheet.theme(:zebra)
105
-
106
- h3. Types
107
-
108
- The default theme uses class names on td/th to cast values.
109
- Here is the list of class to type mapping:
110
-
111
- |_. ==CSS== class |_. Format |
112
- | decimal or float | Decimal |
113
- | num or int | Integer |
114
- | datetime | DateTime (Chronic.parse) |
115
- | date | Date (Date.parse) |
116
- | time | Time (Chronic.parse) |