to_spreadsheet 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,7 @@
1
- require 'nokogiri'
2
- require 'to_spreadsheet/action_pack_renderers'
3
- require 'to_spreadsheet/mime_types'
4
1
  require 'to_spreadsheet/version'
5
- require 'to_spreadsheet/helpers'
6
2
  require 'to_spreadsheet/context'
3
+ require 'to_spreadsheet/renderer'
4
+ require 'to_spreadsheet/railtie' if defined?(Rails)
7
5
 
8
6
  module ToSpreadsheet
9
7
  class << self
@@ -17,10 +17,26 @@ module ToSpreadsheet
17
17
  def global
18
18
  @global ||= new
19
19
  end
20
+
21
+ def current
22
+ Thread.current[:_to_spreadsheet_ctx]
23
+ end
24
+
25
+ def current=(ctx)
26
+ Thread.current[:_to_spreadsheet_ctx] = ctx
27
+ end
28
+
29
+ def with_context(ctx, &block)
30
+ old = current
31
+ self.current = ctx
32
+ r = block.call(ctx)
33
+ self.current = old
34
+ r
35
+ end
20
36
  end
21
37
 
22
38
  def initialize(wb_options = nil)
23
- @rules = []
39
+ @rules = []
24
40
  workbook wb_options if wb_options
25
41
  end
26
42
 
@@ -83,7 +99,7 @@ module ToSpreadsheet
83
99
 
84
100
  # A new context
85
101
  def merge(other_context)
86
- ctx = Context.new()
102
+ ctx = Context.new()
87
103
  ctx.rules = rules + other_context.rules
88
104
  ctx
89
105
  end
@@ -1,32 +1,24 @@
1
- require 'active_support'
2
- require 'action_controller/metal/renderers'
3
- require 'action_controller/metal/responder'
4
-
5
- require 'to_spreadsheet/renderer'
6
-
7
- # This will let us do thing like `render :xlsx => 'index'`
8
- # This is similar to how Rails internally implements its :json and :xml renderers
9
- ActionController::Renderers.add :xlsx do |template, options|
10
- filename = options[:filename] || options[:template] || 'data'
11
-
12
- html = with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do
13
- # local context
14
- @local_formats.each do |selector, &block|
15
- context.process_dsl selector, &block
16
- end if @local_formats
17
- render_to_string(options[:template], options)
18
- end
19
-
20
- data = ToSpreadsheet::Axlsx::Renderer.to_data(html)
21
- send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx")
22
- end
23
-
24
- class ActionController::Responder
25
- # This sets up a default render call for when you do
26
- # respond_to do |format|
27
- # format.xlsx
28
- # end
29
- def to_xlsx
30
- controller.render xlsx: controller.action_name
31
- end
32
- end
1
+ require 'active_support'
2
+ require 'action_controller/metal/renderers'
3
+ require 'action_controller/metal/responder'
4
+
5
+ # This will let us do thing like `render :xlsx => 'index'`
6
+ # This is similar to how Rails internally implements its :json and :xml renderers
7
+ ActionController::Renderers.add :xlsx do |template, options|
8
+ filename = options[:filename] || options[:template] || 'data'
9
+ data = ToSpreadsheet::Context.with_context ToSpreadsheet::Context.global.merge(ToSpreadsheet::Context.new) do |context|
10
+ html = render_to_string(template, options.merge(template: template, formats: ['html']))
11
+ ToSpreadsheet::Renderer.to_data(html, context)
12
+ end
13
+ send_data data, type: :xlsx, disposition: %(attachment; filename="#{filename}.xlsx")
14
+ end
15
+
16
+ class ActionController::Responder
17
+ # This sets up a default render call for when you do
18
+ # respond_to do |format|
19
+ # format.xlsx
20
+ # end
21
+ def to_xlsx
22
+ controller.render xlsx: controller.action_name
23
+ end
24
+ end
@@ -1,2 +1,2 @@
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)
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)
@@ -0,0 +1,11 @@
1
+ module ToSpreadsheet
2
+ module Rails
3
+ module ViewHelpers
4
+ def format_xls(selector = nil, &block)
5
+ ctx = ToSpreadsheet::Context.current
6
+ return unless ctx
7
+ ctx.format_xls selector, &block
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
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
+ module ToSpreadsheet
6
+ class Railtie < ::Rails::Railtie
7
+ initializer "to_spreadsheet.view_helpers" do
8
+ ActionView::Base.send :include, ToSpreadsheet::Rails::ViewHelpers
9
+ end
10
+ end
11
+ end
@@ -1,34 +1,30 @@
1
1
  require 'axlsx'
2
+ require 'nokogiri'
3
+
2
4
  module ToSpreadsheet
3
5
  module Renderer
4
6
  extend self
5
7
 
6
- def to_stream(html, local_context = nil)
7
- to_package(html, local_context).to_stream
8
+ def to_stream(html, context = nil)
9
+ to_package(html, context).to_stream
8
10
  end
9
11
 
10
- def to_data(html, local_context = nil)
11
- to_package(html, local_context).to_stream.read
12
+ def to_data(html, context = nil)
13
+ to_package(html, context).to_stream.read
12
14
  end
13
15
 
14
- def to_package(html, local_context = nil)
15
- with_context init_context(local_context) do
16
- package = build_package(html, context)
17
- context.rules.each do |rule|
18
- puts "Applying #{rule}"
19
- rule.apply(context, package)
20
- end
21
- package
16
+ def to_package(html, context = nil)
17
+ context ||= ToSpreadsheet::Context.global.merge(Context.new)
18
+ package = build_package(html, context)
19
+ context.rules.each do |rule|
20
+ #Rails.logger.debug "Applying #{rule}"
21
+ rule.apply(context, package)
22
22
  end
23
+ package
23
24
  end
24
25
 
25
26
  private
26
27
 
27
- def init_context(local_context)
28
- local_context ||= ToSpreadsheet::Context.new
29
- ToSpreadsheet::Context.global.merge local_context
30
- end
31
-
32
28
  def build_package(html, context)
33
29
  package = ::Axlsx::Package.new
34
30
  spreadsheet = package.workbook
@@ -5,10 +5,11 @@ module ToSpreadsheet
5
5
  class Format < Base
6
6
  include ::ToSpreadsheet::TypeFromValue
7
7
  def apply(context, sheet)
8
+ wb = sheet.workbook
8
9
  case selector_type
9
10
  when :css
10
11
  css_match selector_query, context.to_xml_node(sheet) do |xml_node|
11
- apply_inline_styles context, context.to_xls_entity(xml_node)
12
+ add_and_apply_style wb, context, context.to_xls_entity(xml_node)
12
13
  end
13
14
  when :row
14
15
  sheet.row_style selector_query, options if options.present?
@@ -17,7 +18,7 @@ module ToSpreadsheet
17
18
  sheet.col_style selector_query, inline_styles if inline_styles.present?
18
19
  apply_col_info sheet.column_info[selector_query]
19
20
  when :range
20
- apply_inline_styles range_match(selector_type, sheet), context
21
+ add_and_apply_style wb, range_match(selector_type, sheet), context
21
22
  end
22
23
  end
23
24
 
@@ -32,7 +33,7 @@ module ToSpreadsheet
32
33
  end
33
34
  end
34
35
 
35
- def apply_inline_styles(context, xls_ent)
36
+ def add_and_apply_style(wb, context, xls_ent)
36
37
  # Custom format rule
37
38
  # format 'td.sel', lambda { |node| ...}
38
39
  if self.options.is_a?(Proc)
@@ -47,17 +48,9 @@ module ToSpreadsheet
47
48
  options[k] = context.instance_exec(xls_ent, &v) if v.is_a?(Proc)
48
49
  end
49
50
 
50
- # Apply inline styles
51
- options.each do |k, v|
52
- next if v.nil?
53
- setter = :"#{k}="
54
- xls_ent.send setter, v if xls_ent.respond_to?(setter)
55
- if xls_ent.respond_to?(:cells)
56
- xls_ent.cells.each do |cell|
57
- cell.send setter, v if cell.respond_to?(setter)
58
- end
59
- end
60
- end
51
+ style = wb.styles.add_style options
52
+ cells = xls_ent.respond_to?(:cells) ? xls_ent.cells : [xls_ent]
53
+ cells.each { |cell| cell.style = style }
61
54
  end
62
55
  end
63
56
  end
@@ -1,3 +1,3 @@
1
- module ToSpreadsheet
2
- VERSION = '1.0.0'
3
- end
1
+ module ToSpreadsheet
2
+ VERSION = '1.0.1'
3
+ end
data/spec/format_spec.rb CHANGED
@@ -6,7 +6,7 @@ describe ToSpreadsheet::Rule::Format do
6
6
  :ruby
7
7
  format_xls do
8
8
  format column: 0, width: 25
9
- format 'tr', color: lambda { |row| 'cccccc' if row.index.odd? }
9
+ format 'tr', fg_color: lambda { |row| 'cccccc' if row.index.odd? }
10
10
  end
11
11
  %table
12
12
  %tr
@@ -23,7 +23,10 @@ describe ToSpreadsheet::Rule::Format do
23
23
  sheet.column_info[0].width.should == 25
24
24
  end
25
25
  it 'runs lambdas' do
26
- sheet.rows[1].cells[0].color.rgb.should == Axlsx::Color.new(rgb: 'cccccc').rgb
26
+ cell = sheet.rows[1].cells[0]
27
+ styles = sheet.workbook.styles
28
+ font_id = styles.cellXfs[cell.style].fontId
29
+ styles.fonts[font_id].color.rgb.should == Axlsx::Color.new(rgb: 'cccccc').rgb
27
30
  end
28
31
  end
29
32
  end
metadata CHANGED
@@ -1,111 +1,128 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: to_spreadsheet
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 1
7
- - 0
8
- - 0
9
- version: 1.0.0
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.1
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Gleb Mazovetskiy
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2012-11-29 00:00:00 +01:00
18
- default_executable:
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ none: false
21
21
  name: rails
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- requirements:
25
- - - ">="
26
- - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
- version: "0"
30
22
  type: :runtime
31
- version_requirements: *id001
32
- - !ruby/object:Gem::Dependency
33
- name: nokogiri
34
23
  prerelease: false
35
- requirement: &id002 !ruby/object:Gem::Requirement
36
- requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- segments:
40
- - 0
41
- version: "0"
24
+ requirement: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ! '>='
27
+ - !ruby/object:Gem::Version
28
+ version: '0'
29
+ none: false
30
+ - !ruby/object:Gem::Dependency
31
+ version_requirements: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ none: false
37
+ name: nokogiri
42
38
  type: :runtime
43
- version_requirements: *id002
44
- - !ruby/object:Gem::Dependency
45
- name: axlsx
46
39
  prerelease: false
47
- requirement: &id003 !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
- version: "0"
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ none: false
46
+ - !ruby/object:Gem::Dependency
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ none: false
53
+ name: axlsx
54
54
  type: :runtime
55
- version_requirements: *id003
56
- - !ruby/object:Gem::Dependency
57
- name: chronic
58
55
  prerelease: false
59
- requirement: &id004 !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- segments:
64
- - 0
65
- version: "0"
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ none: false
62
+ - !ruby/object:Gem::Dependency
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ none: false
69
+ name: chronic
66
70
  type: :runtime
67
- version_requirements: *id004
68
- - !ruby/object:Gem::Dependency
69
- name: mocha
70
71
  prerelease: false
71
- requirement: &id005 !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- segments:
76
- - 0
77
- version: "0"
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ none: false
78
+ - !ruby/object:Gem::Dependency
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ none: false
85
+ name: mocha
78
86
  type: :development
79
- version_requirements: *id005
80
- - !ruby/object:Gem::Dependency
81
- name: sqlite3-ruby
82
87
  prerelease: false
83
- requirement: &id006 !ruby/object:Gem::Requirement
84
- requirements:
85
- - - ">="
86
- - !ruby/object:Gem::Version
87
- segments:
88
- - 0
89
- version: "0"
88
+ requirement: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ none: false
94
+ - !ruby/object:Gem::Dependency
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ none: false
101
+ name: sqlite3-ruby
90
102
  type: :development
91
- version_requirements: *id006
103
+ prerelease: false
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ none: false
92
110
  description: Rendering spreadsheets from Rails made easy
93
111
  email: glex.spb@gmail.com
94
112
  executables: []
95
-
96
113
  extensions: []
97
-
98
- extra_rdoc_files:
114
+ extra_rdoc_files:
99
115
  - README.textile
100
- files:
116
+ files:
101
117
  - README.textile
102
118
  - LICENSE
103
119
  - Rakefile
104
- - lib/to_spreadsheet/action_pack_renderers.rb
105
120
  - lib/to_spreadsheet/context/pairing.rb
106
121
  - lib/to_spreadsheet/context.rb
107
- - lib/to_spreadsheet/helpers.rb
108
- - lib/to_spreadsheet/mime_types.rb
122
+ - lib/to_spreadsheet/rails/action_pack_renderers.rb
123
+ - lib/to_spreadsheet/rails/mime_types.rb
124
+ - lib/to_spreadsheet/rails/view_helpers.rb
125
+ - lib/to_spreadsheet/railtie.rb
109
126
  - lib/to_spreadsheet/renderer.rb
110
127
  - lib/to_spreadsheet/rule/base.rb
111
128
  - lib/to_spreadsheet/rule/container.rb
@@ -119,37 +136,35 @@ files:
119
136
  - lib/to_spreadsheet/type_from_value.rb
120
137
  - lib/to_spreadsheet/version.rb
121
138
  - lib/to_spreadsheet.rb
122
- has_rdoc: true
139
+ - spec/defaults_spec.rb
140
+ - spec/format_spec.rb
141
+ - spec/types_spec.rb
142
+ - spec/worksheets_spec.rb
123
143
  homepage: https://github.com/glebm/to_spreadsheet
124
144
  licenses: []
125
-
126
145
  post_install_message:
127
146
  rdoc_options: []
128
-
129
- require_paths:
147
+ require_paths:
130
148
  - lib
131
- required_ruby_version: !ruby/object:Gem::Requirement
132
- requirements:
133
- - - ">="
134
- - !ruby/object:Gem::Version
135
- segments:
136
- - 0
137
- version: "0"
138
- required_rubygems_version: !ruby/object:Gem::Requirement
139
- requirements:
140
- - - ">="
141
- - !ruby/object:Gem::Version
142
- segments:
143
- - 0
144
- version: "0"
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ! '>='
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ none: false
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ! '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ none: false
145
161
  requirements: []
146
-
147
162
  rubyforge_project:
148
- rubygems_version: 1.3.6
163
+ rubygems_version: 1.8.24
149
164
  signing_key:
150
165
  specification_version: 3
151
166
  summary: Render existing views as Excel documents with style!
152
- test_files:
167
+ test_files:
153
168
  - spec/defaults_spec.rb
154
169
  - spec/format_spec.rb
155
170
  - spec/types_spec.rb
@@ -1,19 +0,0 @@
1
- module ToSpreadsheet
2
- module Helpers
3
- def format_xls(selector = nil, &block)
4
- context.format_xls selector, &block
5
- end
6
-
7
- def context
8
- @context || ToSpreadsheet::Context.global
9
- end
10
-
11
- def with_context(context, &block)
12
- context_was = self.context
13
- @context = context
14
- result = block.call
15
- @context = context_was
16
- result
17
- end
18
- end
19
- end