active_model_exporters 0.4.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ ActiveModel::Exporter::TYPES.each do |type|
4
4
 
5
5
  if resource.respond_to?(method)
6
6
  encode = options[:encode] || 'iso-8859-1'
7
- mtype = "Mime::#{type.upcase}".safe_constantize
7
+ mtype = Mime[type.to_sym]
8
8
  file = resource.send(method).encode(encode)
9
9
 
10
10
  default_options = {type: mtype, disposition: 'attachment'}
@@ -19,7 +19,7 @@ module ActiveModel
19
19
  private
20
20
 
21
21
  def generate_file(options = {})
22
- CSV.generate(options) do |file|
22
+ CSV.generate(**options) do |file|
23
23
  file << headers
24
24
  collection.each do |object|
25
25
  file << exporter_for(object).values
@@ -9,12 +9,6 @@ module ActiveModel
9
9
 
10
10
  def attributes(*attrs)
11
11
  @_attributes.concat(attrs)
12
-
13
- attrs.each do |attr|
14
- define_method(attr) do
15
- object.send(attr)
16
- end unless method_defined?(attr)
17
- end
18
12
  end
19
13
 
20
14
  def exporter_for(resource)
@@ -32,7 +26,12 @@ module ActiveModel
32
26
 
33
27
  def values
34
28
  attrs = filter(attributes)
35
- attributes.map { |attr| send(attr) if attrs.include?(attr) }
29
+ attributes.map do |attr|
30
+ if attrs.include?(attr)
31
+ next send(attr) if respond_to?(attr)
32
+ object.send(attr)
33
+ end
34
+ end
36
35
  end
37
36
 
38
37
  def filter(attrs)
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- VERSION = '0.4.0'
3
+ VERSION = '0.6.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,43 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_model_exporters
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alejandro Gutiérrez
8
- autorequire:
9
- bindir: bin
8
+ autorequire:
9
+ bindir: exe
10
10
  cert_chain: []
11
- date: 2016-03-28 00:00:00.000000000 Z
11
+ date: 2021-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activemodel
14
+ name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '4.0'
20
- type: :runtime
19
+ version: '13.0'
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '4.0'
26
+ version: '13.0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: rails
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.2'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.2'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.14'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.14'
69
+ - !ruby/object:Gem::Dependency
70
+ name: appraisal
29
71
  requirement: !ruby/object:Gem::Requirement
30
72
  requirements:
31
- - - '>='
73
+ - - "~>"
32
74
  - !ruby/object:Gem::Version
33
- version: '4.0'
75
+ version: '2.4'
34
76
  type: :development
35
77
  prerelease: false
36
78
  version_requirements: !ruby/object:Gem::Requirement
37
79
  requirements:
38
- - - '>='
80
+ - - "~>"
39
81
  - !ruby/object:Gem::Version
40
- version: '4.0'
82
+ version: '2.4'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.8'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '5.2'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '5.2'
41
111
  description: A simple way to export data in Rails.
42
112
  email:
43
113
  - alejandrodevs@gmail.com
@@ -45,14 +115,24 @@ executables: []
45
115
  extensions: []
46
116
  extra_rdoc_files: []
47
117
  files:
48
- - .gitignore
49
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Appraisals
50
121
  - CHANGELOG.md
122
+ - CODE_OF_CONDUCT.md
51
123
  - Gemfile
52
124
  - LICENSE.txt
53
125
  - README.md
54
126
  - Rakefile
55
127
  - active_model_exporters.gemspec
128
+ - bin/console
129
+ - bin/setup
130
+ - gemfiles/rails_5.2.gemfile
131
+ - gemfiles/rails_5.2.gemfile.lock
132
+ - gemfiles/rails_6.0.gemfile
133
+ - gemfiles/rails_6.0.gemfile.lock
134
+ - gemfiles/rails_6.1.gemfile
135
+ - gemfiles/rails_6.1.gemfile.lock
56
136
  - lib/action_controller/exportation.rb
57
137
  - lib/action_controller/exportation/mime_types.rb
58
138
  - lib/action_controller/exportation/renderers.rb
@@ -61,46 +141,30 @@ files:
61
141
  - lib/active_model/exporter/types.rb
62
142
  - lib/active_model/exporter/version.rb
63
143
  - lib/active_model_exporters.rb
64
- - test/fixtures/active_record/exporters.rb
65
- - test/fixtures/active_record/models.rb
66
- - test/fixtures/exporters.rb
67
- - test/fixtures/locale.rb
68
- - test/fixtures/models.rb
69
- - test/integration/action_controller/exportation/csv_test.rb
70
- - test/integration/action_controller/exportation/xls_test.rb
71
- - test/integration/active_record/exportation/csv_test.rb
72
- - test/test_helper.rb
73
144
  homepage: https://github.com/alejandrodevs/active_model_exporters
74
145
  licenses:
75
146
  - MIT
76
- metadata: {}
77
- post_install_message:
147
+ metadata:
148
+ homepage_uri: https://github.com/alejandrodevs/active_model_exporters
149
+ source_code_uri: https://github.com/alejandrodevs/active_model_exporters
150
+ changelog_uri: https://github.com/alejandrodevs/active_model_exporters/blob/master/LICENSE.txt
151
+ post_install_message:
78
152
  rdoc_options: []
79
153
  require_paths:
80
154
  - lib
81
155
  required_ruby_version: !ruby/object:Gem::Requirement
82
156
  requirements:
83
- - - '>='
157
+ - - ">="
84
158
  - !ruby/object:Gem::Version
85
- version: 2.0.0
159
+ version: '0'
86
160
  required_rubygems_version: !ruby/object:Gem::Requirement
87
161
  requirements:
88
- - - '>='
162
+ - - ">="
89
163
  - !ruby/object:Gem::Version
90
164
  version: '0'
91
165
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.2.2
94
- signing_key:
166
+ rubygems_version: 3.2.3
167
+ signing_key:
95
168
  specification_version: 4
96
169
  summary: A simple way to export data in Rails.
97
- test_files:
98
- - test/fixtures/active_record/exporters.rb
99
- - test/fixtures/active_record/models.rb
100
- - test/fixtures/exporters.rb
101
- - test/fixtures/locale.rb
102
- - test/fixtures/models.rb
103
- - test/integration/action_controller/exportation/csv_test.rb
104
- - test/integration/action_controller/exportation/xls_test.rb
105
- - test/integration/active_record/exportation/csv_test.rb
106
- - test/test_helper.rb
170
+ test_files: []
@@ -1,3 +0,0 @@
1
- class ARUserExporter < ActiveModel::Exporter
2
- attributes :first_name, :last_name
3
- end
@@ -1,17 +0,0 @@
1
- require 'active_record'
2
-
3
- ActiveRecord::Base.establish_connection(
4
- adapter: 'sqlite3',
5
- database: ':memory:'
6
- )
7
-
8
- ActiveRecord::Schema.define do
9
- create_table :ar_users, force: true do |t|
10
- t.string :first_name
11
- t.string :last_name
12
- t.timestamps
13
- end
14
- end
15
-
16
- class ARUser < ActiveRecord::Base
17
- end
@@ -1,23 +0,0 @@
1
- class UserExporter < ActiveModel::Exporter
2
- attributes :first_name, :last_name, :full_name
3
-
4
- def full_name
5
- "#{object.first_name}-#{object.last_name}#{scope ? "-#{scope}" : ''}"
6
- end
7
- end
8
-
9
- class FancyUserExporter < ActiveModel::Exporter
10
- attributes :first_name, :last_name
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
@@ -1,20 +0,0 @@
1
- require 'i18n'
2
-
3
- class I18n::Backend::Simple
4
- def translations_store
5
- translations
6
- end
7
- end
8
-
9
- I18n.enforce_available_locales = false
10
-
11
- I18n.backend.translations_store[:en] = {
12
- activerecord: {
13
- attributes: {
14
- ar_user: {
15
- first_name: 'First',
16
- last_name: 'Last'
17
- }
18
- }
19
- }
20
- }
@@ -1,6 +0,0 @@
1
- class Model < OpenStruct
2
- include ActiveModel
3
- end
4
-
5
- class User < Model
6
- end
@@ -1,162 +0,0 @@
1
- require 'test_helper'
2
-
3
- module ActionController
4
- module Exportation::CSV
5
- class ImplicitExporterTest < ActionController::TestCase
6
- class TestsController < ActionController::Base
7
- def render_using_implicit_exporter
8
- render csv: [
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 'text/csv', @response.content_type
21
- assert_equal "first_name,last_name,full_name\n"\
22
- "Foo1,Bar1,Foo1-Bar1\n"\
23
- "Foo2,Bar2,Foo2-Bar2\n"\
24
- "Foo3,Bar3,Foo3-Bar3\n", @response.body
25
- end
26
- end
27
-
28
- class ExplicitExporterTest < ActionController::TestCase
29
- class TestsController < ActionController::Base
30
- def render_using_explicit_exporter
31
- render csv: [
32
- User.new(first_name: 'Foo1', last_name: 'Bar1')
33
- ], exporter: FancyUserExporter
34
- end
35
- end
36
-
37
- tests TestsController
38
-
39
- def test_render_using_explicit_exporter
40
- get :render_using_explicit_exporter
41
- assert_equal 'text/csv', @response.content_type
42
- assert_equal "first_name,last_name\n"\
43
- "Foo1,Bar1\n", @response.body
44
- end
45
- end
46
-
47
- class ImplicitExportationScopeTest < ActionController::TestCase
48
- class TestsController < ActionController::Base
49
- def render_using_implicit_exportation_scope
50
- render csv: [
51
- User.new(first_name: 'Foo1', last_name: 'Bar1')
52
- ]
53
- end
54
-
55
- private
56
-
57
- def current_user
58
- 'current_user'
59
- end
60
- end
61
-
62
- tests TestsController
63
-
64
- def test_render_using_implicit_exportation_scope
65
- get :render_using_implicit_exportation_scope
66
- assert_equal 'text/csv', @response.content_type
67
- assert_equal "first_name,last_name,full_name\n"\
68
- "Foo1,Bar1,Foo1-Bar1-current_user\n", @response.body
69
- end
70
- end
71
-
72
- class ExplicitExportationScopeTest < ActionController::TestCase
73
- class TestsController < ActionController::Base
74
- def render_using_explicit_exportation_scope
75
- render csv: [
76
- User.new(first_name: 'Foo1', last_name: 'Bar1')
77
- ], scope: current_admin
78
- end
79
-
80
- private
81
-
82
- def current_admin
83
- 'current_admin'
84
- end
85
- end
86
-
87
- tests TestsController
88
-
89
- def test_render_using_explicit_exportation_scope
90
- get :render_using_explicit_exportation_scope
91
- assert_equal 'text/csv', @response.content_type
92
- assert_equal "first_name,last_name,full_name\n"\
93
- "Foo1,Bar1,Foo1-Bar1-current_admin\n", @response.body
94
- end
95
- end
96
-
97
- class CallingExportationScopeTest < ActionController::TestCase
98
- class TestsController < ActionController::Base
99
- exportation_scope :current_admin
100
-
101
- def render_calling_exportation_scope
102
- render csv: [
103
- User.new(first_name: 'Foo1', last_name: 'Bar1')
104
- ]
105
- end
106
-
107
- private
108
-
109
- def current_admin
110
- 'current_admin'
111
- end
112
- end
113
-
114
- tests TestsController
115
-
116
- def test_render_calling_exportation_scope
117
- get :render_calling_exportation_scope
118
- assert_equal 'text/csv', @response.content_type
119
- assert_equal "first_name,last_name,full_name\n"\
120
- "Foo1,Bar1,Foo1-Bar1-current_admin\n", @response.body
121
- end
122
- end
123
-
124
- class UsingFilterAttributesTest < ActionController::TestCase
125
- class TestsController < ActionController::Base
126
- def render_using_filter_attributes
127
- render csv: [
128
- User.new(first_name: 'Foo1', last_name: 'Bar1', email: 'FooBar1'),
129
- User.new(first_name: 'Foo2', last_name: 'Bar2', email: 'FooBar2')
130
- ], exporter: FilterUserExporter
131
- end
132
- end
133
-
134
- tests TestsController
135
-
136
- def test_render_using_filter_attributes
137
- get :render_using_filter_attributes
138
- assert_equal 'text/csv', @response.content_type
139
- assert_equal "first_name,last_name,email\n"\
140
- "Foo1,,FooBar1\n"\
141
- "Foo2,Bar2,FooBar2\n", @response.body
142
- end
143
- end
144
-
145
- class ExporterSingleResourceTest < ActionController::TestCase
146
- class TestsController < ActionController::Base
147
- def render_single_resource
148
- render csv: User.new(first_name: 'Foo1', last_name: 'Bar1')
149
- end
150
- end
151
-
152
- tests TestsController
153
-
154
- def test_render_single_resource
155
- get :render_single_resource
156
- assert_equal 'text/csv', @response.content_type
157
- assert_equal "first_name,last_name,full_name\n"\
158
- "Foo1,Bar1,Foo1-Bar1\n", @response.body
159
- end
160
- end
161
- end
162
- end