active_model_exporters 0.0.3 → 0.0.4

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: 84de92b3db98397f7183d12a226532ca12ec0bda
4
- data.tar.gz: abd93136ae604559252ec400d2c2a50763e97f69
3
+ metadata.gz: f372c93931a76bf7ab6d04a611006f79ef7501b5
4
+ data.tar.gz: 60027e0b93ee046ca7e72f7abc832a31877a01ac
5
5
  SHA512:
6
- metadata.gz: a3bdd3fe7121e68e29cc4f9bdf25df780c7ffd321f48f0e8642b449f74efa7d749aca74e32e570e4302dd1e383fa23306d4606a1e4006ca3b34ff3259209de1f
7
- data.tar.gz: 9e44063c9a4293d361c0068c8e52d214257fe659f97925f354744bafd3e678230d48b9786c8666767e57beaab1d3bc71c6fe5d60b3f6909e5312656ecf27f7f5
6
+ metadata.gz: 7c2b8f26b473902a504ee2282b03aef6c4fcb1959da569f4a7e9e48708d2343613f9e6bc813d236cad23d8eae751558436e7ac9a73ca5f5a7897beedbcadd13e
7
+ data.tar.gz: 72db327f1475d63435a5f7035fdc16842c3e2a52288432245d0ddf0b861b43bd0fb614db8cd34a95287db2d7ad70fed7f84f5898ef863ee48f2ec92d06db68eb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # CHANGELOG
2
2
 
3
+ ### Version 0.0.4
4
+ * Exporting to XLS format
5
+
3
6
  ### Version 0.0.3
4
7
  * Adding filter method
5
8
 
data/README.md CHANGED
@@ -31,6 +31,7 @@ class PostsController < ApplicationController
31
31
 
32
32
  respond_to do |format|
33
33
  format.csv { render csv: @posts }
34
+ format.xls { render xls: @posts }
34
35
  end
35
36
  end
36
37
  end
@@ -39,7 +40,7 @@ end
39
40
  Or:
40
41
  ```ruby
41
42
  class PostsController < ApplicationController
42
- respond_to :csv
43
+ respond_to :csv, :xls
43
44
 
44
45
  def index
45
46
  @posts = Post.all
@@ -13,9 +13,11 @@ module ActionController
13
13
  end
14
14
  end
15
15
 
16
- def _render_option_csv(resource, options)
17
- exporter = build_exporter(resource, options)
18
- exporter ? super(exporter, options) : super
16
+ ActiveModel::Exporter::TYPES.each do |type|
17
+ define_method "_render_option_#{type}" do |resource, options|
18
+ exporter = build_exporter(resource, options)
19
+ exporter ? super(exporter, options) : super
20
+ end
19
21
  end
20
22
 
21
23
  private
@@ -4,17 +4,27 @@ module ActiveModel
4
4
 
5
5
  def initialize(collection, options = {})
6
6
  @collection = collection
7
- @scope = options[:scope]
8
- @exporter = options[:exporter]
7
+ @scope = options.delete(:scope)
8
+ @exporter = options.delete(:exporter)
9
9
  end
10
10
 
11
11
  def to_csv
12
- CSV.generate do |file|
12
+ generate_file
13
+ end
14
+
15
+ def to_xls
16
+ generate_file(col_sep: "\t")
17
+ end
18
+
19
+ alias :to_xlsx :to_xls
20
+
21
+ def generate_file(options = {})
22
+ CSV.generate(options) do |file|
13
23
  collection.each do |object|
14
24
  exporter = exporter_for(object)
15
25
  file << exporter.values
16
26
  end
17
- end
27
+ end.encode('ISO-8859-1')
18
28
  end
19
29
 
20
30
  private
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ class Exporter
3
+ TYPES = [:csv, :xls, :xlsx]
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
2
  class Exporter
3
- VERSION = '0.0.3'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -1,15 +1,22 @@
1
1
  require 'csv'
2
2
  require 'active_model'
3
3
  require 'active_model/exporter'
4
+ require 'active_model/exporter/types'
4
5
  require 'active_model/array_exporter'
5
6
  require 'active_model/exporter/version'
6
7
 
7
8
  if defined?(ActionController)
8
9
  require 'action_controller/exportation'
9
10
 
10
- ActionController::Renderers.add :csv do |csv, options|
11
- self.content_type ||= Mime::CSV
12
- csv.respond_to?(:to_csv) ? csv.to_csv : csv
11
+ Mime::Type.register('application/vnd.ms-excel', :xls)
12
+ Mime::Type.register('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', :xlsx)
13
+
14
+ ActiveModel::Exporter::TYPES.each do |type|
15
+ ActionController::Renderers.add type do |resource, options|
16
+ method = "to_#{type}".to_sym
17
+ self.content_type ||= "Mime::#{type.upcase}".safe_constantize
18
+ resource.respond_to?(method) ? resource.send(method) : resource
19
+ end
13
20
  end
14
21
 
15
22
  ActiveSupport.on_load(:action_controller) do
@@ -1,7 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module ActionController
4
- module Exportation
4
+ module Exportation::CSV
5
5
  class ImplicitExporterTest < ActionController::TestCase
6
6
  class TestsController < ActionController::Base
7
7
  def render_using_implicit_exporter
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Exportation::XLS
5
+ class ImplicitExporterTest < ActionController::TestCase
6
+ class TestsController < ActionController::Base
7
+ def render_using_implicit_exporter
8
+ render xls: [
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 'application/vnd.ms-excel', @response.content_type
21
+ assert_equal "Foo1\tBar1\tFoo1-Bar1\n"\
22
+ "Foo2\tBar2\tFoo2-Bar2\n"\
23
+ "Foo3\tBar3\tFoo3-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
30
+ render xls: [
31
+ User.new(first_name: 'Foo1', last_name: 'Bar1')
32
+ ], exporter: FancyUserExporter
33
+ end
34
+ end
35
+
36
+ tests TestsController
37
+
38
+ def test_render_using_explicit_exporter
39
+ get :render_using_explicit_exporter
40
+ assert_equal 'application/vnd.ms-excel', @response.content_type
41
+ assert_equal "Foo1\tBar1\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 xls: [
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
58
+ end
59
+
60
+ tests TestsController
61
+
62
+ def test_render_using_implicit_exportation_scope
63
+ get :render_using_implicit_exportation_scope
64
+ assert_equal 'application/vnd.ms-excel', @response.content_type
65
+ assert_equal "Foo1\tBar1\tFoo1-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 xls: [
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
82
+ end
83
+
84
+ tests TestsController
85
+
86
+ def test_render_using_explicit_exportation_scope
87
+ get :render_using_explicit_exportation_scope
88
+ assert_equal 'application/vnd.ms-excel', @response.content_type
89
+ assert_equal "Foo1\tBar1\tFoo1-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 xls: [
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 'application/vnd.ms-excel', @response.content_type
115
+ assert_equal "Foo1\tBar1\tFoo1-Bar1-current_admin\n", @response.body
116
+ end
117
+ end
118
+
119
+ class UsingFilterAttributesTest < ActionController::TestCase
120
+ class TestsController < ActionController::Base
121
+ def render_using_filter_attributes
122
+ render xls: [
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 'application/vnd.ms-excel', @response.content_type
134
+ assert_equal "Foo1\t\tFooBar1\n"\
135
+ "Foo2\tBar2\tFooBar2\n", @response.body
136
+ end
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,139 @@
1
+ require 'test_helper'
2
+
3
+ module ActionController
4
+ module Exportation::XLSX
5
+ class ImplicitExporterTest < ActionController::TestCase
6
+ class TestsController < ActionController::Base
7
+ def render_using_implicit_exporter
8
+ render xlsx: [
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 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
21
+ assert_equal "Foo1\tBar1\tFoo1-Bar1\n"\
22
+ "Foo2\tBar2\tFoo2-Bar2\n"\
23
+ "Foo3\tBar3\tFoo3-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
30
+ render xlsx: [
31
+ User.new(first_name: 'Foo1', last_name: 'Bar1')
32
+ ], exporter: FancyUserExporter
33
+ end
34
+ end
35
+
36
+ tests TestsController
37
+
38
+ def test_render_using_explicit_exporter
39
+ get :render_using_explicit_exporter
40
+ assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
41
+ assert_equal "Foo1\tBar1\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 xlsx: [
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
58
+ end
59
+
60
+ tests TestsController
61
+
62
+ def test_render_using_implicit_exportation_scope
63
+ get :render_using_implicit_exportation_scope
64
+ assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
65
+ assert_equal "Foo1\tBar1\tFoo1-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 xlsx: [
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
82
+ end
83
+
84
+ tests TestsController
85
+
86
+ def test_render_using_explicit_exportation_scope
87
+ get :render_using_explicit_exportation_scope
88
+ assert_equal 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
89
+ assert_equal "Foo1\tBar1\tFoo1-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 xlsx: [
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 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
115
+ assert_equal "Foo1\tBar1\tFoo1-Bar1-current_admin\n", @response.body
116
+ end
117
+ end
118
+
119
+ class UsingFilterAttributesTest < ActionController::TestCase
120
+ class TestsController < ActionController::Base
121
+ def render_using_filter_attributes
122
+ render xlsx: [
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 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', @response.content_type
134
+ assert_equal "Foo1\t\tFooBar1\n"\
135
+ "Foo2\tBar2\tFooBar2\n", @response.body
136
+ end
137
+ end
138
+ end
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.3
4
+ version: 0.0.4
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-30 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -56,11 +56,14 @@ files:
56
56
  - lib/action_controller/exportation.rb
57
57
  - lib/active_model/array_exporter.rb
58
58
  - lib/active_model/exporter.rb
59
+ - lib/active_model/exporter/types.rb
59
60
  - lib/active_model/exporter/version.rb
60
61
  - lib/active_model_exporters.rb
61
62
  - test/fixtures/exporters.rb
62
63
  - test/fixtures/models.rb
63
64
  - test/integration/action_controller/exportation/csv_test.rb
65
+ - test/integration/action_controller/exportation/xls_test.rb
66
+ - test/integration/action_controller/exportation/xlsx_test.rb
64
67
  - test/test_helper.rb
65
68
  homepage: https://github.com/alejandrogutierrez/active_model_exporters
66
69
  licenses:
@@ -90,4 +93,6 @@ test_files:
90
93
  - test/fixtures/exporters.rb
91
94
  - test/fixtures/models.rb
92
95
  - test/integration/action_controller/exportation/csv_test.rb
96
+ - test/integration/action_controller/exportation/xls_test.rb
97
+ - test/integration/action_controller/exportation/xlsx_test.rb
93
98
  - test/test_helper.rb