active_model_exporters 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +49 -5
- data/lib/action_controller/exportation.rb +9 -2
- data/lib/active_model/array_exporter.rb +3 -2
- data/lib/active_model/exporter/version.rb +1 -1
- data/lib/active_model/exporter.rb +3 -2
- data/test/fixtures/exporters.rb +1 -1
- data/test/integration/action_controller/exportation/csv_test.rb +88 -20
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa0b27aca8f2ae7ea3c947b3ba0b7c757acbf006
|
4
|
+
data.tar.gz: b39a7542040b7dbbf246eaa3151247567b8ce035
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 536d772d63c5341f6729ba1c7084aa1eea74f379a73759620c064f7640010ac95f9f36b8fd28137f14f12f3df6ca27beb035ccf1861af68cdc0ecd150d439138
|
7
|
+
data.tar.gz: 396b4cff045ae6de02a669d5a47d782c3e060214ee265752461dfeb91978f0fee1f48e4e8a39b18b34b184a8b037040bdb81959928b5fe8db77c8d2b9e428e6e
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -19,7 +19,7 @@ Run the bundle command to install it.
|
|
19
19
|
Generate an exporter in `app/exporters/post_exporter.rb`:
|
20
20
|
```ruby
|
21
21
|
class PostExporter < ActiveModel::Exporter
|
22
|
-
attributes :id, :
|
22
|
+
attributes :id, :title, :content
|
23
23
|
end
|
24
24
|
```
|
25
25
|
|
@@ -36,8 +36,20 @@ class PostsController < ApplicationController
|
|
36
36
|
end
|
37
37
|
```
|
38
38
|
|
39
|
+
Or:
|
40
|
+
```ruby
|
41
|
+
class PostsController < ApplicationController
|
42
|
+
respond_to :csv
|
43
|
+
|
44
|
+
def index
|
45
|
+
@posts = Post.all
|
46
|
+
respond_with @posts
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
39
51
|
### Custom exporter
|
40
|
-
To specify a custom exporter for an object, you can do the next:
|
52
|
+
To specify a custom exporter for an object, you can do the next in your controller:
|
41
53
|
```ruby
|
42
54
|
render csv: @posts, exporter: OtherPostExporter
|
43
55
|
```
|
@@ -45,11 +57,43 @@ render csv: @posts, exporter: OtherPostExporter
|
|
45
57
|
### Computed properties
|
46
58
|
As `ActiveModel::Serializers` does, you can access the object being exported as `object`.
|
47
59
|
```ruby
|
48
|
-
class
|
49
|
-
attributes :
|
60
|
+
class UserExporter < ActiveModel::Exporter
|
61
|
+
attributes :first_name, :last_name, :full_name
|
50
62
|
|
51
63
|
def full_name
|
52
|
-
"#{object.
|
64
|
+
"#{object.first_name} #{object.last_name}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### Exporter scope
|
70
|
+
|
71
|
+
#### 1. Default scope
|
72
|
+
As `ActiveModel::Serializers` does, you can access to the current user application via `scope`.
|
73
|
+
```ruby
|
74
|
+
class UserExporter < ActiveModel::Exporter
|
75
|
+
attributes :name, :email
|
76
|
+
|
77
|
+
def email
|
78
|
+
object.name unless scope.admin?
|
79
|
+
end
|
80
|
+
end
|
81
|
+
```
|
82
|
+
|
83
|
+
#### 2. Explicit scope
|
84
|
+
In your controller, include the scope option:
|
85
|
+
```ruby
|
86
|
+
render csv: @posts, scope: :current_admin
|
87
|
+
```
|
88
|
+
|
89
|
+
#### 3. Calling exportation_scope
|
90
|
+
In your controller, set the exportation scope:
|
91
|
+
```ruby
|
92
|
+
class PostsController < ApplicationController
|
93
|
+
exportation_scope :current_admin
|
94
|
+
|
95
|
+
def index
|
96
|
+
# Do something...
|
53
97
|
end
|
54
98
|
end
|
55
99
|
```
|
@@ -20,9 +20,16 @@ module ActionController
|
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
+
def exportation_scope
|
24
|
+
scope = self.class._exportation_scope
|
25
|
+
send(scope) if scope && respond_to?(scope, true)
|
26
|
+
end
|
27
|
+
|
23
28
|
def build_exporter(resource, options)
|
24
|
-
exporter = ActiveModel::Exporter.exporter_for(resource)
|
25
|
-
|
29
|
+
if exporter = ActiveModel::Exporter.exporter_for(resource)
|
30
|
+
options[:scope] ||= exportation_scope
|
31
|
+
exporter.new(resource, options)
|
32
|
+
end
|
26
33
|
end
|
27
34
|
end
|
28
35
|
end
|
@@ -1,9 +1,10 @@
|
|
1
1
|
module ActiveModel
|
2
2
|
class ArrayExporter
|
3
|
-
attr_reader :collection, :exporter
|
3
|
+
attr_reader :collection, :exporter, :scope
|
4
4
|
|
5
5
|
def initialize(collection, options = {})
|
6
6
|
@collection = collection
|
7
|
+
@scope = options[:scope]
|
7
8
|
@exporter = options[:exporter]
|
8
9
|
end
|
9
10
|
|
@@ -20,7 +21,7 @@ module ActiveModel
|
|
20
21
|
|
21
22
|
def exporter_for(object)
|
22
23
|
exporter_class = exporter || Exporter.exporter_for(object)
|
23
|
-
exporter_class.new(object)
|
24
|
+
exporter_class.new(object, scope: scope)
|
24
25
|
end
|
25
26
|
end
|
26
27
|
end
|
@@ -27,10 +27,11 @@ module ActiveModel
|
|
27
27
|
end
|
28
28
|
|
29
29
|
|
30
|
-
attr_accessor :object, :attributes
|
30
|
+
attr_accessor :object, :attributes, :scope
|
31
31
|
|
32
32
|
def initialize(object, options = {})
|
33
|
-
@object
|
33
|
+
@object = object
|
34
|
+
@scope = options[:scope]
|
34
35
|
@attributes = self.class._attributes.dup
|
35
36
|
end
|
36
37
|
|
data/test/fixtures/exporters.rb
CHANGED
@@ -2,23 +2,31 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
module ActionController
|
4
4
|
module Exportation
|
5
|
-
class
|
5
|
+
class ImplicitExporterTest < ActionController::TestCase
|
6
6
|
class TestsController < ActionController::Base
|
7
|
-
def
|
8
|
-
render csv: [
|
9
|
-
User.new(first_name: 'Foo1', last_name: 'Bar1')
|
10
|
-
]
|
11
|
-
end
|
12
|
-
|
13
|
-
def export_multiple_to_csv
|
7
|
+
def render_using_implicit_exporter
|
14
8
|
render csv: [
|
15
9
|
User.new(first_name: 'Foo1', last_name: 'Bar1'),
|
16
10
|
User.new(first_name: 'Foo2', last_name: 'Bar2'),
|
17
11
|
User.new(first_name: 'Foo3', last_name: 'Bar3')
|
18
12
|
]
|
19
13
|
end
|
14
|
+
end
|
15
|
+
|
16
|
+
tests TestsController
|
20
17
|
|
21
|
-
|
18
|
+
def test_render_using_implicit_exporter
|
19
|
+
get :render_using_implicit_exporter
|
20
|
+
assert_equal 'text/csv', @response.content_type
|
21
|
+
assert_equal "Foo1,Bar1,Foo1-Bar1\n"\
|
22
|
+
"Foo2,Bar2,Foo2-Bar2\n"\
|
23
|
+
"Foo3,Bar3,Foo3-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
|
22
30
|
render csv: [
|
23
31
|
User.new(first_name: 'Foo1', last_name: 'Bar1')
|
24
32
|
], exporter: FancyUserExporter
|
@@ -27,24 +35,84 @@ module ActionController
|
|
27
35
|
|
28
36
|
tests TestsController
|
29
37
|
|
30
|
-
def
|
31
|
-
get :
|
38
|
+
def test_render_using_explicit_exporter
|
39
|
+
get :render_using_explicit_exporter
|
32
40
|
assert_equal 'text/csv', @response.content_type
|
33
|
-
assert_equal "Foo1,Bar1
|
41
|
+
assert_equal "Foo1,Bar1\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 csv: [
|
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
|
34
58
|
end
|
35
59
|
|
36
|
-
|
37
|
-
|
60
|
+
tests TestsController
|
61
|
+
|
62
|
+
def test_render_using_implicit_exportation_scope
|
63
|
+
get :render_using_implicit_exportation_scope
|
38
64
|
assert_equal 'text/csv', @response.content_type
|
39
|
-
assert_equal "Foo1,Bar1,Foo1-Bar1\n"
|
40
|
-
|
41
|
-
|
65
|
+
assert_equal "Foo1,Bar1,Foo1-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 csv: [
|
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
|
42
82
|
end
|
43
83
|
|
44
|
-
|
45
|
-
|
84
|
+
tests TestsController
|
85
|
+
|
86
|
+
def test_render_using_explicit_exportation_scope
|
87
|
+
get :render_using_explicit_exportation_scope
|
46
88
|
assert_equal 'text/csv', @response.content_type
|
47
|
-
assert_equal "Foo1,Bar1\n", @response.body
|
89
|
+
assert_equal "Foo1,Bar1,Foo1-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 csv: [
|
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 'text/csv', @response.content_type
|
115
|
+
assert_equal "Foo1,Bar1,Foo1-Bar1-current_admin\n", @response.body
|
48
116
|
end
|
49
117
|
end
|
50
118
|
end
|