active_model_exporters 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +20 -1
- data/lib/active_model/exporter.rb +15 -5
- data/lib/active_model/exporter/version.rb +1 -1
- data/test/fixtures/exporters.rb +12 -0
- data/test/integration/action_controller/exportation/csv_test.rb +20 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84de92b3db98397f7183d12a226532ca12ec0bda
|
4
|
+
data.tar.gz: abd93136ae604559252ec400d2c2a50763e97f69
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3bdd3fe7121e68e29cc4f9bdf25df780c7ffd321f48f0e8642b449f74efa7d749aca74e32e570e4302dd1e383fa23306d4606a1e4006ca3b34ff3259209de1f
|
7
|
+
data.tar.gz: 9e44063c9a4293d361c0068c8e52d214257fe659f97925f354744bafd3e678230d48b9786c8666767e57beaab1d3bc71c6fe5d60b3f6909e5312656ecf27f7f5
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -75,7 +75,7 @@ class UserExporter < ActiveModel::Exporter
|
|
75
75
|
attributes :name, :email
|
76
76
|
|
77
77
|
def email
|
78
|
-
object.
|
78
|
+
object.email unless scope.admin?
|
79
79
|
end
|
80
80
|
end
|
81
81
|
```
|
@@ -98,6 +98,25 @@ class PostsController < ApplicationController
|
|
98
98
|
end
|
99
99
|
```
|
100
100
|
|
101
|
+
### Filter attributes
|
102
|
+
As `ActiveModel::Serializers` does, you can reject some attributes
|
103
|
+
according to your business rules:
|
104
|
+
```ruby
|
105
|
+
class UserExporter < ActiveModel::Exporter
|
106
|
+
attributes :name, :email, :address
|
107
|
+
|
108
|
+
def filter(attrs)
|
109
|
+
if object.admin?
|
110
|
+
attrs - [:address]
|
111
|
+
else
|
112
|
+
attrs
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
Rejected attributes will be blank in the downloaded file.
|
118
|
+
|
119
|
+
|
101
120
|
## Contributing
|
102
121
|
|
103
122
|
New feature or code refactoring? Submit a pull request that implements it. Don't forget to write your tests and include a CHANGELOG with your updates.
|
@@ -27,16 +27,26 @@ module ActiveModel
|
|
27
27
|
end
|
28
28
|
|
29
29
|
|
30
|
-
attr_accessor :object, :
|
30
|
+
attr_accessor :object, :scope
|
31
31
|
|
32
32
|
def initialize(object, options = {})
|
33
|
-
@object
|
34
|
-
@scope
|
35
|
-
@attributes = self.class._attributes.dup
|
33
|
+
@object = object
|
34
|
+
@scope = options[:scope]
|
36
35
|
end
|
37
36
|
|
38
37
|
def values
|
39
|
-
|
38
|
+
attrs = filter(attributes)
|
39
|
+
attributes.map { |attr| send(attr) if attrs.include?(attr) }
|
40
|
+
end
|
41
|
+
|
42
|
+
def filter(attrs)
|
43
|
+
attrs
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def attributes
|
49
|
+
self.class._attributes.dup
|
40
50
|
end
|
41
51
|
end
|
42
52
|
end
|
data/test/fixtures/exporters.rb
CHANGED
@@ -9,3 +9,15 @@ end
|
|
9
9
|
class FancyUserExporter < ActiveModel::Exporter
|
10
10
|
attributes :first_name, :last_name
|
11
11
|
end
|
12
|
+
|
13
|
+
class FilterUserExporter < ActiveModel::Exporter
|
14
|
+
attributes :first_name, :last_name, :email
|
15
|
+
|
16
|
+
def filter(attrs)
|
17
|
+
if object.last_name == 'Bar1'
|
18
|
+
attrs - [:last_name]
|
19
|
+
else
|
20
|
+
attrs
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -115,5 +115,25 @@ module ActionController
|
|
115
115
|
assert_equal "Foo1,Bar1,Foo1-Bar1-current_admin\n", @response.body
|
116
116
|
end
|
117
117
|
end
|
118
|
+
|
119
|
+
class UsingFilterAttributesTest < ActionController::TestCase
|
120
|
+
class TestsController < ActionController::Base
|
121
|
+
def render_using_filter_attributes
|
122
|
+
render csv: [
|
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 'text/csv', @response.content_type
|
134
|
+
assert_equal "Foo1,,FooBar1\n"\
|
135
|
+
"Foo2,Bar2,FooBar2\n", @response.body
|
136
|
+
end
|
137
|
+
end
|
118
138
|
end
|
119
139
|
end
|
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
|
+
version: 0.0.3
|
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-06-
|
11
|
+
date: 2014-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|