rails-excel 0.0.2 → 0.0.3
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/Changelog +4 -0
- data/README.md +55 -47
- data/lib/rails-excel.rb +1 -1
- data/lib/rails-excel/template_handler.rb +6 -4
- data/lib/rails-excel/version.rb +1 -1
- metadata +4 -4
data/Changelog
CHANGED
data/README.md
CHANGED
@@ -14,15 +14,17 @@ It comes with two builtin strategies based on the very good gems [writeexcel](ht
|
|
14
14
|
|
15
15
|
In your config/environment.rb
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
```ruby
|
18
|
+
config.gem 'rails-excel'
|
19
|
+
```
|
19
20
|
|
20
21
|
Create an initializer : config/initializers/excel.rb
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
23
|
+
```ruby
|
24
|
+
Rails::Excel.configure do |config|
|
25
|
+
config.strategy = :spreadsheet # by default or :write_excel
|
26
|
+
end
|
27
|
+
```
|
26
28
|
|
27
29
|
If you wan tot implement your own strategy, here are the requirements :
|
28
30
|
|
@@ -33,60 +35,66 @@ If you wan tot implement your own strategy, here are the requirements :
|
|
33
35
|
|
34
36
|
Example extracted from code source:
|
35
37
|
|
36
|
-
|
38
|
+
```ruby
|
39
|
+
# lib/my_strategy.rb
|
37
40
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
41
|
+
class MyStrategy
|
42
|
+
def compile(io, &block)
|
43
|
+
workbook = ::Spreadsheet::Workbook.new
|
44
|
+
yield(workbook)
|
45
|
+
workbook.write(io)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
45
49
|
|
46
50
|
Then in your config/initializers/excel.rb
|
47
51
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
52
|
+
```ruby
|
53
|
+
require 'my_strategy'
|
54
|
+
Rails::Excel.configure do |config|
|
55
|
+
config.add_strategy :my_strategy, MyStrategy.new
|
56
|
+
# Redefining default strategy
|
57
|
+
config.strategy = :my_strategy # by default it was :spreadsheet
|
58
|
+
end
|
59
|
+
```
|
55
60
|
|
56
61
|
You can use any object as long as it responds to described `compile` method
|
57
62
|
|
58
|
-
|
59
63
|
The strategy defined in the initializer will be used in all of your controllers
|
60
64
|
|
61
65
|
To use another strategy you can set it by controller :
|
62
66
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
```ruby
|
68
|
+
class UsersController < ApplicationController
|
69
|
+
self.excel_strategy = :write_excel
|
70
|
+
end
|
71
|
+
```
|
67
72
|
|
68
73
|
Or you can set strategy per action by redefining instance method excel_strategy
|
69
74
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
75
|
+
```ruby
|
76
|
+
class UsersController < ApplicationController
|
77
|
+
self.excel_strategy = :write_excel
|
78
|
+
def index
|
79
|
+
# ...
|
80
|
+
end
|
81
|
+
|
82
|
+
def show
|
83
|
+
# ...
|
84
|
+
end
|
85
|
+
|
86
|
+
protected
|
87
|
+
def excel_strategy
|
88
|
+
case action_name
|
89
|
+
when 'index'
|
90
|
+
:spreadsheet
|
91
|
+
else
|
92
|
+
self.class.excel_strategy
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
```
|
81
97
|
|
82
|
-
|
83
|
-
def excel_strategy
|
84
|
-
case action_name
|
85
|
-
when 'index'
|
86
|
-
:spreadsheet
|
87
|
-
else
|
88
|
-
self.class.excel_strategy
|
89
|
-
end
|
90
|
-
end
|
98
|
+
# Contributors
|
91
99
|
|
92
|
-
|
100
|
+
* [edpaget](https://github.com/edpaget)
|
data/lib/rails-excel.rb
CHANGED
@@ -2,18 +2,20 @@ require 'action_controller'
|
|
2
2
|
require 'action_view'
|
3
3
|
module Rails
|
4
4
|
module Excel
|
5
|
-
class TemplateHandler
|
6
|
-
include ::ActionView::TemplateHandlers::Compilable
|
5
|
+
class TemplateHandler
|
7
6
|
|
7
|
+
def self.call(template, *args)
|
8
|
+
new.compile(template)
|
9
|
+
end
|
10
|
+
|
8
11
|
def compile(template)
|
9
12
|
%Q{
|
10
|
-
_set_controller_content_type(Mime::XLS);
|
11
13
|
io = StringIO.new
|
12
14
|
Rails::Excel.available_strategies[self.excel_strategy].compile(io) do |workbook|
|
13
15
|
#{template.source}
|
14
16
|
end
|
15
17
|
self.output_buffer = io.string
|
16
|
-
|
18
|
+
}
|
17
19
|
end
|
18
20
|
|
19
21
|
end
|
data/lib/rails-excel/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-excel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 3
|
10
|
+
version: 0.0.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Ramihajamalala Hery
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-09-26 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|