active_model_exporters 0.0.4 → 0.0.5

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: f372c93931a76bf7ab6d04a611006f79ef7501b5
4
- data.tar.gz: 60027e0b93ee046ca7e72f7abc832a31877a01ac
3
+ metadata.gz: 30a964ee00e6f0e49c06fafa36d234ecc9f0c830
4
+ data.tar.gz: 4e4ccce791d424d0bc328a8233e3c7b71c624ad8
5
5
  SHA512:
6
- metadata.gz: 7c2b8f26b473902a504ee2282b03aef6c4fcb1959da569f4a7e9e48708d2343613f9e6bc813d236cad23d8eae751558436e7ac9a73ca5f5a7897beedbcadd13e
7
- data.tar.gz: 72db327f1475d63435a5f7035fdc16842c3e2a52288432245d0ddf0b861b43bd0fb614db8cd34a95287db2d7ad70fed7f84f5898ef863ee48f2ec92d06db68eb
6
+ metadata.gz: d7343fe3ab75b28039cea63a2a46a2cb3c076427ab550c06305fb60a15366a82fd068d02f1d8f1e1c696449f4d3c30123fb83a7f44a560b1e227737e2d539363
7
+ data.tar.gz: 059e57436e2e4d0fd1ac4fc9f1682d71c87660e5f20261ce192e6076328fd38fafb91082b20d605de7a82f03618eb242c8c20755cb5c531e4527d419861de1c3
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ### Version 0.0.5
4
+ * Using send_data method
5
+ * Customizable filename
6
+ * Customizable encode format
7
+
3
8
  ### Version 0.0.4
4
9
  * Exporting to XLS format
5
10
 
@@ -14,4 +19,4 @@
14
19
  ### Version 0.0.1
15
20
  * First version
16
21
  * Computed properties
17
- * Customized exporter
22
+ * Customizable exporter
data/README.md CHANGED
@@ -50,11 +50,24 @@ end
50
50
  ```
51
51
 
52
52
  ### Custom exporter
53
- To specify a custom exporter for an object, you can do the next in your controller:
53
+ To specify a custom exporter for each object, you can do the next in your controller:
54
54
  ```ruby
55
55
  render csv: @posts, exporter: OtherPostExporter
56
56
  ```
57
57
 
58
+ ### Custom filename
59
+ By default filename is the pluralized collection type. Example: `posts.xls`.
60
+ To specify another, you can do the next:
61
+ ```ruby
62
+ render xls: @posts, filename: 'super_posts.xls'
63
+ ```
64
+
65
+ ### Custom encode format
66
+ By default encode format is `iso-8859-1`. You can change it doing the next:
67
+ ```ruby
68
+ render csv: @posts, encode: 'UTF-8'
69
+ ```
70
+
58
71
  ### Computed properties
59
72
  As `ActiveModel::Serializers` does, you can access the object being exported as `object`.
60
73
  ```ruby
@@ -84,7 +97,7 @@ end
84
97
  #### 2. Explicit scope
85
98
  In your controller, include the scope option:
86
99
  ```ruby
87
- render csv: @posts, scope: :current_admin
100
+ render csv: @posts, scope: current_admin
88
101
  ```
89
102
 
90
103
  #### 3. Calling exportation_scope
@@ -0,0 +1 @@
1
+ Mime::Type.register('application/vnd.ms-excel', :xls)
@@ -0,0 +1,16 @@
1
+ ActiveModel::Exporter::TYPES.each do |type|
2
+ ActionController::Renderers.add type do |resource, options|
3
+ method = "to_#{type}".to_sym
4
+
5
+ if resource.respond_to?(method)
6
+ encode = options[:encode] || 'iso-8859-1'
7
+ mtype = "Mime::#{type.upcase}".safe_constantize
8
+ file = resource.send(method).encode(encode)
9
+
10
+ default_options = {type: mtype, disposition: 'attachment'}
11
+ send_data(file, default_options.merge(options))
12
+ else
13
+ resource
14
+ end
15
+ end
16
+ end
@@ -16,15 +16,12 @@ module ActiveModel
16
16
  generate_file(col_sep: "\t")
17
17
  end
18
18
 
19
- alias :to_xlsx :to_xls
20
-
21
19
  def generate_file(options = {})
22
20
  CSV.generate(options) do |file|
23
21
  collection.each do |object|
24
- exporter = exporter_for(object)
25
- file << exporter.values
22
+ file << exporter_for(object).values
26
23
  end
27
- end.encode('ISO-8859-1')
24
+ end
28
25
  end
29
26
 
30
27
  private
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- TYPES = [:csv, :xls, :xlsx]
3
+ TYPES = [:csv, :xls]
4
4
  end
5
5
  end
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- VERSION = '0.0.4'
3
+ VERSION = '0.0.5'
4
4
  end
5
5
  end
@@ -7,17 +7,8 @@ require 'active_model/exporter/version'
7
7
 
8
8
  if defined?(ActionController)
9
9
  require 'action_controller/exportation'
10
-
11
- Mime::Type.register('application/vnd.ms-excel', :xls)
12
- Mime::Type.register('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :xlsx)
13
-
14
- ActiveModel::Exporter::TYPES.each do |type|
15
- ActionController::Renderers.add type do |resource, options|
16
- method = "to_#{type}".to_sym
17
- self.content_type ||= "Mime::#{type.upcase}".safe_constantize
18
- resource.respond_to?(method) ? resource.send(method) : resource
19
- end
20
- end
10
+ require 'action_controller/exportation/mime_types'
11
+ require 'action_controller/exportation/renderers'
21
12
 
22
13
  ActiveSupport.on_load(:action_controller) do
23
14
  include ::ActionController::Exportation
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_exporters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-01 00:00:00.000000000 Z
11
+ date: 2014-07-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -54,6 +54,8 @@ files:
54
54
  - Rakefile
55
55
  - active_model_exporters.gemspec
56
56
  - lib/action_controller/exportation.rb
57
+ - lib/action_controller/exportation/mime_types.rb
58
+ - lib/action_controller/exportation/renderers.rb
57
59
  - lib/active_model/array_exporter.rb
58
60
  - lib/active_model/exporter.rb
59
61
  - lib/active_model/exporter/types.rb
@@ -63,7 +65,6 @@ files:
63
65
  - test/fixtures/models.rb
64
66
  - test/integration/action_controller/exportation/csv_test.rb
65
67
  - test/integration/action_controller/exportation/xls_test.rb
66
- - test/integration/action_controller/exportation/xlsx_test.rb
67
68
  - test/test_helper.rb
68
69
  homepage: https://github.com/alejandrogutierrez/active_model_exporters
69
70
  licenses:
@@ -94,5 +95,4 @@ test_files:
94
95
  - test/fixtures/models.rb
95
96
  - test/integration/action_controller/exportation/csv_test.rb
96
97
  - test/integration/action_controller/exportation/xls_test.rb
97
- - test/integration/action_controller/exportation/xlsx_test.rb
98
98
  - test/test_helper.rb
@@ -1,139 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module Exportation::XLSX
5
- class ImplicitExporterTest < ActionController::TestCase
6
- class TestsController < ActionController::Base
7
- def render_using_implicit_exporter
8
- render xlsx: [
9
- User.new(first_name: 'Foo1', last_name: 'Bar1'),
10
- User.new(first_name: 'Foo2', last_name: 'Bar2'),
11
- User.new(first_name: 'Foo3', last_name: 'Bar3')
12
- ]
13
- end
14
- end
15
-
16
- tests TestsController
17
-
18
- def test_render_using_implicit_exporter
19
- get :render_using_implicit_exporter
20
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
21
- assert_equal "Foo1\tBar1\tFoo1-Bar1\n"\
22
- "Foo2\tBar2\tFoo2-Bar2\n"\
23
- "Foo3\tBar3\tFoo3-Bar3\n", @response.body
24
- end
25
- end
26
-
27
- class ExplicitExporterTest < ActionController::TestCase
28
- class TestsController < ActionController::Base
29
- def render_using_explicit_exporter
30
- render xlsx: [
31
- User.new(first_name: 'Foo1', last_name: 'Bar1')
32
- ], exporter: FancyUserExporter
33
- end
34
- end
35
-
36
- tests TestsController
37
-
38
- def test_render_using_explicit_exporter
39
- get :render_using_explicit_exporter
40
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
41
- assert_equal "Foo1\tBar1\n", @response.body
42
- end
43
- end
44
-
45
- class ImplicitExportationScopeTest < ActionController::TestCase
46
- class TestsController < ActionController::Base
47
- def render_using_implicit_exportation_scope
48
- render xlsx: [
49
- User.new(first_name: 'Foo1', last_name: 'Bar1')
50
- ]
51
- end
52
-
53
- private
54
-
55
- def current_user
56
- 'current_user'
57
- end
58
- end
59
-
60
- tests TestsController
61
-
62
- def test_render_using_implicit_exportation_scope
63
- get :render_using_implicit_exportation_scope
64
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
65
- assert_equal "Foo1\tBar1\tFoo1-Bar1-current_user\n", @response.body
66
- end
67
- end
68
-
69
- class ExplicitExportationScopeTest < ActionController::TestCase
70
- class TestsController < ActionController::Base
71
- def render_using_explicit_exportation_scope
72
- render xlsx: [
73
- User.new(first_name: 'Foo1', last_name: 'Bar1')
74
- ], scope: current_admin
75
- end
76
-
77
- private
78
-
79
- def current_admin
80
- 'current_admin'
81
- end
82
- end
83
-
84
- tests TestsController
85
-
86
- def test_render_using_explicit_exportation_scope
87
- get :render_using_explicit_exportation_scope
88
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
89
- assert_equal "Foo1\tBar1\tFoo1-Bar1-current_admin\n", @response.body
90
- end
91
- end
92
-
93
- class CallingExportationScopeTest < ActionController::TestCase
94
- class TestsController < ActionController::Base
95
- exportation_scope :current_admin
96
-
97
- def render_calling_exportation_scope
98
- render xlsx: [
99
- User.new(first_name: 'Foo1', last_name: 'Bar1')
100
- ]
101
- end
102
-
103
- private
104
-
105
- def current_admin
106
- 'current_admin'
107
- end
108
- end
109
-
110
- tests TestsController
111
-
112
- def test_render_calling_exportation_scope
113
- get :render_calling_exportation_scope
114
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
115
- assert_equal "Foo1\tBar1\tFoo1-Bar1-current_admin\n", @response.body
116
- end
117
- end
118
-
119
- class UsingFilterAttributesTest < ActionController::TestCase
120
- class TestsController < ActionController::Base
121
- def render_using_filter_attributes
122
- render xlsx: [
123
- User.new(first_name: 'Foo1', last_name: 'Bar1', email: 'FooBar1'),
124
- User.new(first_name: 'Foo2', last_name: 'Bar2', email: 'FooBar2')
125
- ], exporter: FilterUserExporter
126
- end
127
- end
128
-
129
- tests TestsController
130
-
131
- def test_render_using_filter_attributes
132
- get :render_using_filter_attributes
133
- assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
134
- assert_equal "Foo1\t\tFooBar1\n"\
135
- "Foo2\tBar2\tFooBar2\n", @response.body
136
- end
137
- end
138
- end
139
- end