active_model_exporters 0.0.2 → 0.0.3

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: fa0b27aca8f2ae7ea3c947b3ba0b7c757acbf006
4
- data.tar.gz: b39a7542040b7dbbf246eaa3151247567b8ce035
3
+ metadata.gz: 84de92b3db98397f7183d12a226532ca12ec0bda
4
+ data.tar.gz: abd93136ae604559252ec400d2c2a50763e97f69
5
5
  SHA512:
6
- metadata.gz: 536d772d63c5341f6729ba1c7084aa1eea74f379a73759620c064f7640010ac95f9f36b8fd28137f14f12f3df6ca27beb035ccf1861af68cdc0ecd150d439138
7
- data.tar.gz: 396b4cff045ae6de02a669d5a47d782c3e060214ee265752461dfeb91978f0fee1f48e4e8a39b18b34b184a8b037040bdb81959928b5fe8db77c8d2b9e428e6e
6
+ metadata.gz: a3bdd3fe7121e68e29cc4f9bdf25df780c7ffd321f48f0e8642b449f74efa7d749aca74e32e570e4302dd1e383fa23306d4606a1e4006ca3b34ff3259209de1f
7
+ data.tar.gz: 9e44063c9a4293d361c0068c8e52d214257fe659f97925f354744bafd3e678230d48b9786c8666767e57beaab1d3bc71c6fe5d60b3f6909e5312656ecf27f7f5
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CHANGELOG
2
2
 
3
+ ### Version 0.0.3
4
+ * Adding filter method
5
+
3
6
  ### Version 0.0.2
4
7
  * Default current_user scope
5
8
  * Explicit exportation scope
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.name unless scope.admin?
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, :attributes, :scope
30
+ attr_accessor :object, :scope
31
31
 
32
32
  def initialize(object, options = {})
33
- @object = object
34
- @scope = options[:scope]
35
- @attributes = self.class._attributes.dup
33
+ @object = object
34
+ @scope = options[:scope]
36
35
  end
37
36
 
38
37
  def values
39
- attributes.map { |attr| send(attr) }
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
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
@@ -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.2
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-27 00:00:00.000000000 Z
11
+ date: 2014-06-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel