active_model_exporters 0.0.1 → 0.0.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0e8ae9df112adf65da8eaff2d6a8c45ab29c70a
4
- data.tar.gz: 55d33fe90283de2aa7a5ec73769c1383908b1bdc
3
+ metadata.gz: fa0b27aca8f2ae7ea3c947b3ba0b7c757acbf006
4
+ data.tar.gz: b39a7542040b7dbbf246eaa3151247567b8ce035
5
5
  SHA512:
6
- metadata.gz: eabf725f796afc1a2f8a0fbf72b863fe1a15e553a3a23a25c78317bb1a3b996c46f0617e4d94fb82e9ba9609f91107fb82fd7a4b327bd5d1bd8de4fd2ee4ee20
7
- data.tar.gz: c374e7f6852fdd95422dc9eb895174ef00766856e2b1ff1e1f7ab830be499175e947f7c1baedb82432be811c09049451f4c94ebcd5e53744ad8850cf8daf2756
6
+ metadata.gz: 536d772d63c5341f6729ba1c7084aa1eea74f379a73759620c064f7640010ac95f9f36b8fd28137f14f12f3df6ca27beb035ccf1861af68cdc0ecd150d439138
7
+ data.tar.gz: 396b4cff045ae6de02a669d5a47d782c3e060214ee265752461dfeb91978f0fee1f48e4e8a39b18b34b184a8b037040bdb81959928b5fe8db77c8d2b9e428e6e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ### Version 0.0.2
4
+ * Default current_user scope
5
+ * Explicit exportation scope
6
+ * Scope calling exportation_scope
7
+
3
8
  ### Version 0.0.1
4
9
  * First version
5
10
  * Computed properties
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, :name, :email
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 PostExporter < ActiveModel::Exporter
49
- attributes :id, :name, :full_name
60
+ class UserExporter < ActiveModel::Exporter
61
+ attributes :first_name, :last_name, :full_name
50
62
 
51
63
  def full_name
52
- "#{object.name} #{object.last_name}"
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
- exporter.new(resource, options) if exporter
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
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- VERSION = '0.0.1'
3
+ VERSION = '0.0.2'
4
4
  end
5
5
  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 = object
33
+ @object = object
34
+ @scope = options[:scope]
34
35
  @attributes = self.class._attributes.dup
35
36
  end
36
37
 
@@ -2,7 +2,7 @@ class UserExporter < ActiveModel::Exporter
2
2
  attributes :first_name, :last_name, :full_name
3
3
 
4
4
  def full_name
5
- "#{object.first_name}-#{object.last_name}"
5
+ "#{object.first_name}-#{object.last_name}#{scope ? "-#{scope}" : ''}"
6
6
  end
7
7
  end
8
8
 
@@ -2,23 +2,31 @@ require 'test_helper'
2
2
 
3
3
  module ActionController
4
4
  module Exportation
5
- class CSVTest < ActionController::TestCase
5
+ class ImplicitExporterTest < ActionController::TestCase
6
6
  class TestsController < ActionController::Base
7
- def export_to_csv
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
- def export_to_csv_with_other_exporter
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 test_export_to_csv
31
- get :export_to_csv
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,Foo1-Bar1\n", @response.body
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
- def test_export_multiple_to_csv
37
- get :export_multiple_to_csv
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
- "Foo2,Bar2,Foo2-Bar2\n"\
41
- "Foo3,Bar3,Foo3-Bar3\n", @response.body
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
- def test_export_to_csv_with_other_exporter
45
- get :export_to_csv_with_other_exporter
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_exporters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez