active_model_exporters 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +61 -0
- data/Rakefile +9 -0
- data/active_model_exporters.gemspec +24 -0
- data/lib/action_controller/exportation.rb +28 -0
- data/lib/active_model/array_exporter.rb +26 -0
- data/lib/active_model/exporter/version.rb +5 -0
- data/lib/active_model/exporter.rb +41 -0
- data/lib/active_model_exporters.rb +18 -0
- data/test/fixtures/exporters.rb +11 -0
- data/test/fixtures/models.rb +6 -0
- data/test/integration/action_controller/exportation/csv_test.rb +51 -0
- data/test/test_helper.rb +24 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d0e8ae9df112adf65da8eaff2d6a8c45ab29c70a
|
4
|
+
data.tar.gz: 55d33fe90283de2aa7a5ec73769c1383908b1bdc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eabf725f796afc1a2f8a0fbf72b863fe1a15e553a3a23a25c78317bb1a3b996c46f0617e4d94fb82e9ba9609f91107fb82fd7a4b327bd5d1bd8de4fd2ee4ee20
|
7
|
+
data.tar.gz: c374e7f6852fdd95422dc9eb895174ef00766856e2b1ff1e1f7ab830be499175e947f7c1baedb82432be811c09049451f4c94ebcd5e53744ad8850cf8daf2756
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Alejandro Gutiérrez
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# ActiveModel::Exporters
|
2
|
+
[](https://travis-ci.org/alejandrogutierrez/active_model_exporters) [](https://coveralls.io/r/alejandrogutierrez/active_model_exporters)
|
3
|
+
|
4
|
+
`ActiveModel::Exporters` aims to provide an easy way to export
|
5
|
+
collections of `ActiveModel` or `ActiveRecord` objects.
|
6
|
+
It's based on object-oriented development and inspired on
|
7
|
+
[active_model_serializers](https://github.com/rails-api/active_model_serializers).
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your Gemfile:
|
12
|
+
```ruby
|
13
|
+
gem 'active_model_exporters'
|
14
|
+
```
|
15
|
+
Run the bundle command to install it.
|
16
|
+
|
17
|
+
## Getting started
|
18
|
+
|
19
|
+
Generate an exporter in `app/exporters/post_exporter.rb`:
|
20
|
+
```ruby
|
21
|
+
class PostExporter < ActiveModel::Exporter
|
22
|
+
attributes :id, :name, :email
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
In your controller:
|
27
|
+
```ruby
|
28
|
+
class PostsController < ApplicationController
|
29
|
+
def index
|
30
|
+
@posts = Post.all
|
31
|
+
|
32
|
+
respond_to do |format|
|
33
|
+
format.csv { render csv: @posts }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
### Custom exporter
|
40
|
+
To specify a custom exporter for an object, you can do the next:
|
41
|
+
```ruby
|
42
|
+
render csv: @posts, exporter: OtherPostExporter
|
43
|
+
```
|
44
|
+
|
45
|
+
### Computed properties
|
46
|
+
As `ActiveModel::Serializers` does, you can access the object being exported as `object`.
|
47
|
+
```ruby
|
48
|
+
class PostExporter < ActiveModel::Exporter
|
49
|
+
attributes :id, :name, :full_name
|
50
|
+
|
51
|
+
def full_name
|
52
|
+
"#{object.name} #{object.last_name}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Contributing
|
58
|
+
|
59
|
+
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.
|
60
|
+
|
61
|
+
Thank you :heart:
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
$:.unshift File.expand_path("../lib", __FILE__)
|
4
|
+
require 'active_model/exporter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'active_model_exporters'
|
8
|
+
spec.version = ActiveModel::Exporter::VERSION
|
9
|
+
spec.authors = ['Alejandro Gutiérrez']
|
10
|
+
spec.email = ['alejandrodevs@gmail.com']
|
11
|
+
spec.description = 'A simple way to export data in Rails.'
|
12
|
+
spec.summary = 'A simple way to export data in Rails.'
|
13
|
+
spec.homepage = 'https://github.com/alejandrogutierrez/active_model_exporters'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.required_ruby_version = '>= 1.9.3'
|
21
|
+
|
22
|
+
spec.add_dependency 'activemodel', '>= 3.2'
|
23
|
+
spec.add_development_dependency 'rails', '>= 3.2'
|
24
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module ActionController
|
2
|
+
module Exportation
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class_attribute :_exportation_scope
|
7
|
+
self._exportation_scope = :current_user
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def exportation_scope(scope)
|
12
|
+
self._exportation_scope = scope
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def _render_option_csv(resource, options)
|
17
|
+
exporter = build_exporter(resource, options)
|
18
|
+
exporter ? super(exporter, options) : super
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def build_exporter(resource, options)
|
24
|
+
exporter = ActiveModel::Exporter.exporter_for(resource)
|
25
|
+
exporter.new(resource, options) if exporter
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class ArrayExporter
|
3
|
+
attr_reader :collection, :exporter
|
4
|
+
|
5
|
+
def initialize(collection, options = {})
|
6
|
+
@collection = collection
|
7
|
+
@exporter = options[:exporter]
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_csv
|
11
|
+
CSV.generate do |file|
|
12
|
+
collection.each do |object|
|
13
|
+
exporter = exporter_for(object)
|
14
|
+
file << exporter.values
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def exporter_for(object)
|
22
|
+
exporter_class = exporter || Exporter.exporter_for(object)
|
23
|
+
exporter_class.new(object)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActiveModel
|
2
|
+
class Exporter
|
3
|
+
class << self
|
4
|
+
attr_accessor :_attributes
|
5
|
+
|
6
|
+
def inherited(base)
|
7
|
+
base._attributes = (_attributes || []).dup
|
8
|
+
end
|
9
|
+
|
10
|
+
def attributes(*attrs)
|
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
|
+
end
|
19
|
+
|
20
|
+
def exporter_for(resource)
|
21
|
+
if resource.respond_to?(:to_ary)
|
22
|
+
ArrayExporter
|
23
|
+
else
|
24
|
+
"#{resource.class.name}Exporter".safe_constantize
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
attr_accessor :object, :attributes
|
31
|
+
|
32
|
+
def initialize(object, options = {})
|
33
|
+
@object = object
|
34
|
+
@attributes = self.class._attributes.dup
|
35
|
+
end
|
36
|
+
|
37
|
+
def values
|
38
|
+
attributes.map { |attr| send(attr) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'csv'
|
2
|
+
require 'active_model'
|
3
|
+
require 'active_model/exporter'
|
4
|
+
require 'active_model/array_exporter'
|
5
|
+
require 'active_model/exporter/version'
|
6
|
+
|
7
|
+
if defined?(ActionController)
|
8
|
+
require 'action_controller/exportation'
|
9
|
+
|
10
|
+
ActionController::Renderers.add :csv do |csv, options|
|
11
|
+
self.content_type ||= Mime::CSV
|
12
|
+
csv.respond_to?(:to_csv) ? csv.to_csv : csv
|
13
|
+
end
|
14
|
+
|
15
|
+
ActiveSupport.on_load(:action_controller) do
|
16
|
+
include ::ActionController::Exportation
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,11 @@
|
|
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}"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class FancyUserExporter < ActiveModel::Exporter
|
10
|
+
attributes :first_name, :last_name
|
11
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
module Exportation
|
5
|
+
class CSVTest < ActionController::TestCase
|
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
|
14
|
+
render csv: [
|
15
|
+
User.new(first_name: 'Foo1', last_name: 'Bar1'),
|
16
|
+
User.new(first_name: 'Foo2', last_name: 'Bar2'),
|
17
|
+
User.new(first_name: 'Foo3', last_name: 'Bar3')
|
18
|
+
]
|
19
|
+
end
|
20
|
+
|
21
|
+
def export_to_csv_with_other_exporter
|
22
|
+
render csv: [
|
23
|
+
User.new(first_name: 'Foo1', last_name: 'Bar1')
|
24
|
+
], exporter: FancyUserExporter
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
tests TestsController
|
29
|
+
|
30
|
+
def test_export_to_csv
|
31
|
+
get :export_to_csv
|
32
|
+
assert_equal 'text/csv', @response.content_type
|
33
|
+
assert_equal "Foo1,Bar1,Foo1-Bar1\n", @response.body
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_export_multiple_to_csv
|
37
|
+
get :export_multiple_to_csv
|
38
|
+
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
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_export_to_csv_with_other_exporter
|
45
|
+
get :export_to_csv_with_other_exporter
|
46
|
+
assert_equal 'text/csv', @response.content_type
|
47
|
+
assert_equal "Foo1,Bar1\n", @response.body
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'active_model_exporters'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'minitest/unit'
|
6
|
+
require 'minitest/pride'
|
7
|
+
require 'fixtures/models'
|
8
|
+
require 'fixtures/exporters'
|
9
|
+
|
10
|
+
module TestHelper
|
11
|
+
Routes = ActionDispatch::Routing::RouteSet.new
|
12
|
+
Routes.draw do
|
13
|
+
get ':controller(/:action(/:id))'
|
14
|
+
get ':controller(/:action)'
|
15
|
+
end
|
16
|
+
|
17
|
+
ActionController::Base.send(:include, Routes.url_helpers)
|
18
|
+
end
|
19
|
+
|
20
|
+
class ActionController::TestCase
|
21
|
+
def setup
|
22
|
+
@routes = TestHelper::Routes
|
23
|
+
end
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_model_exporters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alejandro Gutiérrez
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activemodel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.2'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.2'
|
41
|
+
description: A simple way to export data in Rails.
|
42
|
+
email:
|
43
|
+
- alejandrodevs@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .travis.yml
|
50
|
+
- CHANGELOG.md
|
51
|
+
- Gemfile
|
52
|
+
- LICENSE.txt
|
53
|
+
- README.md
|
54
|
+
- Rakefile
|
55
|
+
- active_model_exporters.gemspec
|
56
|
+
- lib/action_controller/exportation.rb
|
57
|
+
- lib/active_model/array_exporter.rb
|
58
|
+
- lib/active_model/exporter.rb
|
59
|
+
- lib/active_model/exporter/version.rb
|
60
|
+
- lib/active_model_exporters.rb
|
61
|
+
- test/fixtures/exporters.rb
|
62
|
+
- test/fixtures/models.rb
|
63
|
+
- test/integration/action_controller/exportation/csv_test.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
homepage: https://github.com/alejandrogutierrez/active_model_exporters
|
66
|
+
licenses:
|
67
|
+
- MIT
|
68
|
+
metadata: {}
|
69
|
+
post_install_message:
|
70
|
+
rdoc_options: []
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 1.9.3
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 2.2.2
|
86
|
+
signing_key:
|
87
|
+
specification_version: 4
|
88
|
+
summary: A simple way to export data in Rails.
|
89
|
+
test_files:
|
90
|
+
- test/fixtures/exporters.rb
|
91
|
+
- test/fixtures/models.rb
|
92
|
+
- test/integration/action_controller/exportation/csv_test.rb
|
93
|
+
- test/test_helper.rb
|