administrate_exportable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +220 -0
- data/LICENSE.txt +21 -0
- data/README.md +92 -0
- data/Rakefile +6 -0
- data/administrate_exportable.gemspec +37 -0
- data/app/views/admin/application/index.html.erb +67 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/administrate_exportable.rb +8 -0
- data/lib/administrate_exportable/exporter.rb +21 -0
- data/lib/administrate_exportable/exporter_service.rb +91 -0
- data/lib/administrate_exportable/version.rb +3 -0
- metadata +137 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 55c0bdfe3b210474339bc39070f7610646c0b2c3
|
4
|
+
data.tar.gz: 3569c8037f32ed5196b81805abbf322a7a3c61cd
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 947b15a467c2c1dfa1d51c7a4b08409963a0123f22deb148883aa7d0029e7f8d8bbad1525e106c53d4e9dfafa6f1e4e70e189d0f14a9db7cc5290fd30f6f2e28
|
7
|
+
data.tar.gz: 27bb3cf01b756f046adfaa451f93a8e180231bb6658ae1bdf390e8ee7c2cab2da2dca268a980b0a91aff764a5e4c3e68845fa0ddb94e618de5e7908ad768a171
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
|
13
|
+
|
14
|
+
# Ignore the default SQLite database.
|
15
|
+
spec/dummy/db/*.sqlite3
|
16
|
+
spec/dummy/db/*.sqlite3-journal
|
17
|
+
|
18
|
+
# Ignore all logfiles and tempfiles.
|
19
|
+
spec/dummy/log/*
|
20
|
+
spec/dummy/tmp/*
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,220 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
administrate_exportable (0.1.0)
|
5
|
+
administrate (> 0.10.0)
|
6
|
+
rails (>= 4.2, < 6.0)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actioncable (5.2.1)
|
12
|
+
actionpack (= 5.2.1)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (>= 0.6.1)
|
15
|
+
actionmailer (5.2.1)
|
16
|
+
actionpack (= 5.2.1)
|
17
|
+
actionview (= 5.2.1)
|
18
|
+
activejob (= 5.2.1)
|
19
|
+
mail (~> 2.5, >= 2.5.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
actionpack (5.2.1)
|
22
|
+
actionview (= 5.2.1)
|
23
|
+
activesupport (= 5.2.1)
|
24
|
+
rack (~> 2.0)
|
25
|
+
rack-test (>= 0.6.3)
|
26
|
+
rails-dom-testing (~> 2.0)
|
27
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
28
|
+
actionview (5.2.1)
|
29
|
+
activesupport (= 5.2.1)
|
30
|
+
builder (~> 3.1)
|
31
|
+
erubi (~> 1.4)
|
32
|
+
rails-dom-testing (~> 2.0)
|
33
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
34
|
+
activejob (5.2.1)
|
35
|
+
activesupport (= 5.2.1)
|
36
|
+
globalid (>= 0.3.6)
|
37
|
+
activemodel (5.2.1)
|
38
|
+
activesupport (= 5.2.1)
|
39
|
+
activerecord (5.2.1)
|
40
|
+
activemodel (= 5.2.1)
|
41
|
+
activesupport (= 5.2.1)
|
42
|
+
arel (>= 9.0)
|
43
|
+
activestorage (5.2.1)
|
44
|
+
actionpack (= 5.2.1)
|
45
|
+
activerecord (= 5.2.1)
|
46
|
+
marcel (~> 0.3.1)
|
47
|
+
activesupport (5.2.1)
|
48
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
49
|
+
i18n (>= 0.7, < 2)
|
50
|
+
minitest (~> 5.1)
|
51
|
+
tzinfo (~> 1.1)
|
52
|
+
administrate (0.11.0)
|
53
|
+
actionpack (>= 4.2, < 6.0)
|
54
|
+
actionview (>= 4.2, < 6.0)
|
55
|
+
activerecord (>= 4.2, < 6.0)
|
56
|
+
autoprefixer-rails (>= 6.0)
|
57
|
+
datetime_picker_rails (~> 0.0.7)
|
58
|
+
jquery-rails (>= 4.0)
|
59
|
+
kaminari (>= 1.0)
|
60
|
+
momentjs-rails (~> 2.8)
|
61
|
+
sass-rails (~> 5.0)
|
62
|
+
selectize-rails (~> 0.6)
|
63
|
+
administrate-field-enumerate (0.0.1)
|
64
|
+
administrate
|
65
|
+
enumerate_it
|
66
|
+
rails (>= 4.2, < 6.0)
|
67
|
+
arel (9.0.0)
|
68
|
+
autoprefixer-rails (9.3.1)
|
69
|
+
execjs
|
70
|
+
builder (3.2.3)
|
71
|
+
byebug (10.0.2)
|
72
|
+
coderay (1.1.2)
|
73
|
+
concurrent-ruby (1.1.3)
|
74
|
+
crass (1.0.4)
|
75
|
+
datetime_picker_rails (0.0.7)
|
76
|
+
momentjs-rails (>= 2.8.1)
|
77
|
+
diff-lcs (1.3)
|
78
|
+
enumerate_it (1.7.0)
|
79
|
+
activesupport (>= 3.0.0)
|
80
|
+
erubi (1.7.1)
|
81
|
+
execjs (2.7.0)
|
82
|
+
ffi (1.9.25)
|
83
|
+
globalid (0.4.1)
|
84
|
+
activesupport (>= 4.2.0)
|
85
|
+
i18n (1.1.1)
|
86
|
+
concurrent-ruby (~> 1.0)
|
87
|
+
jquery-rails (4.3.3)
|
88
|
+
rails-dom-testing (>= 1, < 3)
|
89
|
+
railties (>= 4.2.0)
|
90
|
+
thor (>= 0.14, < 2.0)
|
91
|
+
kaminari (1.1.1)
|
92
|
+
activesupport (>= 4.1.0)
|
93
|
+
kaminari-actionview (= 1.1.1)
|
94
|
+
kaminari-activerecord (= 1.1.1)
|
95
|
+
kaminari-core (= 1.1.1)
|
96
|
+
kaminari-actionview (1.1.1)
|
97
|
+
actionview
|
98
|
+
kaminari-core (= 1.1.1)
|
99
|
+
kaminari-activerecord (1.1.1)
|
100
|
+
activerecord
|
101
|
+
kaminari-core (= 1.1.1)
|
102
|
+
kaminari-core (1.1.1)
|
103
|
+
loofah (2.2.3)
|
104
|
+
crass (~> 1.0.2)
|
105
|
+
nokogiri (>= 1.5.9)
|
106
|
+
mail (2.7.1)
|
107
|
+
mini_mime (>= 0.1.1)
|
108
|
+
marcel (0.3.3)
|
109
|
+
mimemagic (~> 0.3.2)
|
110
|
+
method_source (0.9.2)
|
111
|
+
mimemagic (0.3.2)
|
112
|
+
mini_mime (1.0.1)
|
113
|
+
mini_portile2 (2.3.0)
|
114
|
+
minitest (5.11.3)
|
115
|
+
momentjs-rails (2.20.1)
|
116
|
+
railties (>= 3.1)
|
117
|
+
nio4r (2.3.1)
|
118
|
+
nokogiri (1.8.5)
|
119
|
+
mini_portile2 (~> 2.3.0)
|
120
|
+
pry (0.12.2)
|
121
|
+
coderay (~> 1.1.0)
|
122
|
+
method_source (~> 0.9.0)
|
123
|
+
pry-byebug (3.6.0)
|
124
|
+
byebug (~> 10.0)
|
125
|
+
pry (~> 0.10)
|
126
|
+
rack (2.0.6)
|
127
|
+
rack-test (1.1.0)
|
128
|
+
rack (>= 1.0, < 3)
|
129
|
+
rails (5.2.1)
|
130
|
+
actioncable (= 5.2.1)
|
131
|
+
actionmailer (= 5.2.1)
|
132
|
+
actionpack (= 5.2.1)
|
133
|
+
actionview (= 5.2.1)
|
134
|
+
activejob (= 5.2.1)
|
135
|
+
activemodel (= 5.2.1)
|
136
|
+
activerecord (= 5.2.1)
|
137
|
+
activestorage (= 5.2.1)
|
138
|
+
activesupport (= 5.2.1)
|
139
|
+
bundler (>= 1.3.0)
|
140
|
+
railties (= 5.2.1)
|
141
|
+
sprockets-rails (>= 2.0.0)
|
142
|
+
rails-dom-testing (2.0.3)
|
143
|
+
activesupport (>= 4.2.0)
|
144
|
+
nokogiri (>= 1.6)
|
145
|
+
rails-html-sanitizer (1.0.4)
|
146
|
+
loofah (~> 2.2, >= 2.2.2)
|
147
|
+
railties (5.2.1)
|
148
|
+
actionpack (= 5.2.1)
|
149
|
+
activesupport (= 5.2.1)
|
150
|
+
method_source
|
151
|
+
rake (>= 0.8.7)
|
152
|
+
thor (>= 0.19.0, < 2.0)
|
153
|
+
rake (10.5.0)
|
154
|
+
rb-fsevent (0.10.3)
|
155
|
+
rb-inotify (0.9.10)
|
156
|
+
ffi (>= 0.5.0, < 2)
|
157
|
+
rspec-core (3.8.0)
|
158
|
+
rspec-support (~> 3.8.0)
|
159
|
+
rspec-expectations (3.8.2)
|
160
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
161
|
+
rspec-support (~> 3.8.0)
|
162
|
+
rspec-mocks (3.8.0)
|
163
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
164
|
+
rspec-support (~> 3.8.0)
|
165
|
+
rspec-rails (3.8.1)
|
166
|
+
actionpack (>= 3.0)
|
167
|
+
activesupport (>= 3.0)
|
168
|
+
railties (>= 3.0)
|
169
|
+
rspec-core (~> 3.8.0)
|
170
|
+
rspec-expectations (~> 3.8.0)
|
171
|
+
rspec-mocks (~> 3.8.0)
|
172
|
+
rspec-support (~> 3.8.0)
|
173
|
+
rspec-support (3.8.0)
|
174
|
+
sass (3.7.2)
|
175
|
+
sass-listen (~> 4.0.0)
|
176
|
+
sass-listen (4.0.0)
|
177
|
+
rb-fsevent (~> 0.9, >= 0.9.4)
|
178
|
+
rb-inotify (~> 0.9, >= 0.9.7)
|
179
|
+
sass-rails (5.0.7)
|
180
|
+
railties (>= 4.0.0, < 6)
|
181
|
+
sass (~> 3.1)
|
182
|
+
sprockets (>= 2.8, < 4.0)
|
183
|
+
sprockets-rails (>= 2.0, < 4.0)
|
184
|
+
tilt (>= 1.1, < 3)
|
185
|
+
selectize-rails (0.12.6)
|
186
|
+
sprockets (3.7.2)
|
187
|
+
concurrent-ruby (~> 1.0)
|
188
|
+
rack (> 1, < 3)
|
189
|
+
sprockets-rails (3.2.1)
|
190
|
+
actionpack (>= 4.0)
|
191
|
+
activesupport (>= 4.0)
|
192
|
+
sprockets (>= 3.0.0)
|
193
|
+
sqlite3 (1.3.13)
|
194
|
+
thor (0.20.3)
|
195
|
+
thread_safe (0.3.6)
|
196
|
+
tilt (2.0.8)
|
197
|
+
tzinfo (1.2.5)
|
198
|
+
thread_safe (~> 0.1)
|
199
|
+
websocket-driver (0.7.0)
|
200
|
+
websocket-extensions (>= 0.1.0)
|
201
|
+
websocket-extensions (0.1.3)
|
202
|
+
|
203
|
+
PLATFORMS
|
204
|
+
ruby
|
205
|
+
|
206
|
+
DEPENDENCIES
|
207
|
+
administrate
|
208
|
+
administrate-field-enumerate
|
209
|
+
administrate_exportable!
|
210
|
+
byebug
|
211
|
+
pry-byebug
|
212
|
+
rails (~> 5.2.1)
|
213
|
+
rspec-rails (~> 3.8)
|
214
|
+
sqlite3
|
215
|
+
|
216
|
+
RUBY VERSION
|
217
|
+
ruby 2.4.2p198
|
218
|
+
|
219
|
+
BUNDLED WITH
|
220
|
+
1.17.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jônatas Rancan de Souza
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
## Installation
|
2
|
+
|
3
|
+
Add this line to your application's Gemfile:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
gem 'administrate_exportable'
|
7
|
+
```
|
8
|
+
|
9
|
+
And then execute:
|
10
|
+
|
11
|
+
$ bundle
|
12
|
+
|
13
|
+
Or install it yourself as:
|
14
|
+
|
15
|
+
$ gem install administrate_exportable
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
For each resource you want to be exportable, add the following line to the their respective Administrate controller.
|
20
|
+
```ruby
|
21
|
+
include AdministrateExportable::Exporter
|
22
|
+
```
|
23
|
+
and the following line in the `db/routes.rb` file, correctly nested on resources
|
24
|
+
```ruby
|
25
|
+
get :export, on: :collection
|
26
|
+
```
|
27
|
+
|
28
|
+
Example:
|
29
|
+
```ruby
|
30
|
+
namespace :admin do
|
31
|
+
resources :organizations do
|
32
|
+
get :export, on: :collection
|
33
|
+
end
|
34
|
+
....
|
35
|
+
```
|
36
|
+
|
37
|
+
By default all the attributes from `ATTRIBUTE_TYPES` will be exported. If you want to exclude an attribute, you can use the option: `export: false`.
|
38
|
+
|
39
|
+
Example:
|
40
|
+
```ruby
|
41
|
+
class BusinessDashboard < Administrate::BaseDashboard
|
42
|
+
ATTRIBUTE_TYPES = {
|
43
|
+
id: Field::Number.with_options(export: false),
|
44
|
+
name: Field::String,
|
45
|
+
description: Field::Text,
|
46
|
+
...
|
47
|
+
```
|
48
|
+
|
49
|
+
For the default field types from Administrate, we export the values you got from the partial `views/fields/index`. And if it is a custom field, we just run `field.to_s`.
|
50
|
+
But if you want to specify the value to be exported, you can use the option `transform_on_export`, to pass a `Proc` that receives the `field`.
|
51
|
+
Example:
|
52
|
+
```ruby
|
53
|
+
ATTRIBUTE_TYPES = {
|
54
|
+
created_at: Field::DateTime.with_options(transform_on_export: -> (field) { field.data.strftime("%F") })
|
55
|
+
```
|
56
|
+
|
57
|
+
By default the gem adds the Export button to the view `views/admin/application/index.html.erb`. But if you have your own Administrate `index` views, you can add the link manually:
|
58
|
+
```ruby
|
59
|
+
link_to 'Export', [:export, namespace, page.resource_path], class: 'button'
|
60
|
+
```
|
61
|
+
|
62
|
+
Example:
|
63
|
+
|
64
|
+
```rails
|
65
|
+
....
|
66
|
+
<div>
|
67
|
+
<%= link_to(
|
68
|
+
t(
|
69
|
+
"administrate.actions.new_resource",
|
70
|
+
name: page.resource_name.titleize.downcase
|
71
|
+
),
|
72
|
+
[:new, namespace, page.resource_path],
|
73
|
+
class: "button",
|
74
|
+
) if valid_action?(:new) && show_action?(:new, new_resource) %>
|
75
|
+
<%= link_to 'Export', [:export, namespace, page.resource_path], class: 'button' %>
|
76
|
+
</div>
|
77
|
+
....
|
78
|
+
```
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
## Contributing
|
87
|
+
|
88
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SourceLabsLLC/administrate_exportable.
|
89
|
+
|
90
|
+
## License
|
91
|
+
|
92
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "administrate_exportable/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "administrate_exportable"
|
8
|
+
spec.version = AdministrateExportable::VERSION
|
9
|
+
spec.authors = ["Jônatas Rancan", "Andrei Bondarev"]
|
10
|
+
spec.email = ["hello@sourcelabs.io"]
|
11
|
+
spec.homepage = 'https://github.com/SourceLabsLLC/administrate_exportable'
|
12
|
+
spec.summary = "Simple plugin to add CSV export feature to Administrate"
|
13
|
+
spec.description = spec.summary
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
17
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
18
|
+
# if spec.respond_to?(:metadata)
|
19
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
20
|
+
# else
|
21
|
+
# raise "RubyGems 2.0 or newer is required to protect against " \
|
22
|
+
# "public gem pushes."
|
23
|
+
# end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
26
|
+
f.match(%r{^(test|spec|features)/})
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
spec.add_dependency 'administrate','> 0.10.0'
|
36
|
+
spec.add_dependency 'rails', '>= 4.2', '< 6.0'
|
37
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
<%#
|
2
|
+
# Index
|
3
|
+
This view is the template for the index page.
|
4
|
+
It is responsible for rendering the search bar, header and pagination.
|
5
|
+
It renders the `_table` partial to display details about the resources.
|
6
|
+
## Local variables:
|
7
|
+
- `page`:
|
8
|
+
An instance of [Administrate::Page::Collection][1].
|
9
|
+
Contains helper methods to help display a table,
|
10
|
+
and knows which attributes should be displayed in the resource's table.
|
11
|
+
- `resources`:
|
12
|
+
An instance of `ActiveRecord::Relation` containing the resources
|
13
|
+
that match the user's search criteria.
|
14
|
+
By default, these resources are passed to the table partial to be displayed.
|
15
|
+
- `search_term`:
|
16
|
+
A string containing the term the user has searched for, if any.
|
17
|
+
- `show_search_bar`:
|
18
|
+
A boolean that determines if the search bar should be shown.
|
19
|
+
[1]: http://www.rubydoc.info/gems/administrate/Administrate/Page/Collection
|
20
|
+
%>
|
21
|
+
|
22
|
+
<% content_for(:title) do %>
|
23
|
+
<%= display_resource_name(page.resource_name) %>
|
24
|
+
<% end %>
|
25
|
+
|
26
|
+
<header class="main-content__header" role="banner">
|
27
|
+
<h1 class="main-content__page-title" id="page-title">
|
28
|
+
<%= content_for(:title) %>
|
29
|
+
</h1>
|
30
|
+
|
31
|
+
<% if show_search_bar %>
|
32
|
+
<%= render(
|
33
|
+
"search",
|
34
|
+
search_term: search_term,
|
35
|
+
resource_name: display_resource_name(page.resource_name)
|
36
|
+
) %>
|
37
|
+
<% end %>
|
38
|
+
|
39
|
+
<div>
|
40
|
+
<%= link_to(
|
41
|
+
t(
|
42
|
+
"administrate.actions.new_resource",
|
43
|
+
name: page.resource_name.titleize.downcase
|
44
|
+
),
|
45
|
+
[:new, namespace, page.resource_path],
|
46
|
+
class: "button",
|
47
|
+
) if valid_action?(:new) && show_action?(:new, new_resource) %>
|
48
|
+
<%= link_to(
|
49
|
+
'Export',
|
50
|
+
[:export, namespace, page.resource_name.to_s.pluralize, { format: :csv }],
|
51
|
+
class: 'button'
|
52
|
+
) if valid_action?(:export) %>
|
53
|
+
</div>
|
54
|
+
</header>
|
55
|
+
|
56
|
+
<section class="main-content__body main-content__body--flush">
|
57
|
+
<%= render(
|
58
|
+
"collection",
|
59
|
+
collection_presenter: page,
|
60
|
+
collection_field_name: resource_name,
|
61
|
+
page: page,
|
62
|
+
resources: resources,
|
63
|
+
table_title: "page-title"
|
64
|
+
) %>
|
65
|
+
|
66
|
+
<%= paginate resources %>
|
67
|
+
</section>
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "administrate_exportable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module AdministrateExportable
|
2
|
+
module Exporter
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
exportable
|
7
|
+
end
|
8
|
+
|
9
|
+
class_methods do
|
10
|
+
def exportable
|
11
|
+
define_method(:export) do
|
12
|
+
csv_data = ExporterService.csv(dashboard, resource_class)
|
13
|
+
|
14
|
+
respond_to do |format|
|
15
|
+
format.csv { send_data csv_data, filename: "#{resource_name.to_s.pluralize}-#{Date.today}.csv" }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
module AdministrateExportable
|
4
|
+
class ExporterService
|
5
|
+
def self.csv(dashboard, resource_class)
|
6
|
+
new(dashboard, resource_class).csv
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(dashboard, resource_class)
|
10
|
+
@dashboard = dashboard
|
11
|
+
@resource_class = resource_class
|
12
|
+
end
|
13
|
+
|
14
|
+
def csv
|
15
|
+
CSV.generate(headers: true) do |csv|
|
16
|
+
|
17
|
+
csv << headers
|
18
|
+
|
19
|
+
collection.find_each do |record|
|
20
|
+
csv << attributes_to_export.map do |attribute_key, attribute_type|
|
21
|
+
record_attribute(record, attribute_key, attribute_type)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_reader :dashboard, :resource_class, :sanitizer
|
30
|
+
|
31
|
+
def record_attribute(record, attribute_key, attribute_type)
|
32
|
+
field = attribute_type.new(attribute_key, record.send(attribute_key), 'index', resource: record)
|
33
|
+
transform_on_export = attribute_type.respond_to?(:options) && attribute_type.options[:transform_on_export]
|
34
|
+
|
35
|
+
if transform_on_export.is_a? Proc
|
36
|
+
return transform_on_export.call(field)
|
37
|
+
end
|
38
|
+
|
39
|
+
case field.class.to_s
|
40
|
+
when Administrate::Field::BelongsTo.to_s, Administrate::Field::HasOne.to_s, Administrate::Field::Polymorphic.to_s
|
41
|
+
field.display_associated_resource if field.data
|
42
|
+
when Administrate::Field::HasMany.to_s
|
43
|
+
field.data.count if field.data
|
44
|
+
when Administrate::Field::DateTime.to_s
|
45
|
+
field.datetime if field.data
|
46
|
+
when Administrate::Field::Email.to_s, Administrate::Field::Select.to_s
|
47
|
+
field.data
|
48
|
+
when Administrate::Field::Password.to_s, Administrate::Field::String.to_s, Administrate::Field::Text.to_s
|
49
|
+
field.truncate
|
50
|
+
when Administrate::Field::Time.to_s
|
51
|
+
field.data.strftime("%I:%M%p").to_s if field.data
|
52
|
+
else
|
53
|
+
field.to_s
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def headers
|
58
|
+
attributes_to_export.map do |attribute_key, _|
|
59
|
+
attr_key = attribute_key.to_s
|
60
|
+
|
61
|
+
if attr_key.include?('_id')
|
62
|
+
attr_key
|
63
|
+
else
|
64
|
+
I18n.t(
|
65
|
+
"helpers.label.#{resource_class.name}.#{attr_key}",
|
66
|
+
default: attr_key,
|
67
|
+
).titleize
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def attributes_to_export
|
73
|
+
@attributes_to_export ||= begin
|
74
|
+
dashboard.class::ATTRIBUTE_TYPES.select do |attribute_key, attribute_type|
|
75
|
+
attribute_options = attribute_type.try(:options)
|
76
|
+
|
77
|
+
!attribute_options || attribute_options[:export] != false
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def collection
|
83
|
+
relation = resource_class.default_scoped
|
84
|
+
resource_includes = dashboard.association_includes
|
85
|
+
|
86
|
+
return relation if resource_includes.empty?
|
87
|
+
|
88
|
+
relation.includes(*resource_includes)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: administrate_exportable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jônatas Rancan
|
8
|
+
- Andrei Bondarev
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2018-11-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.16'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.16'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: rspec
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '3.0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '3.0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: administrate
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">"
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 0.10.0
|
63
|
+
type: :runtime
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 0.10.0
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: rails
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '4.2'
|
77
|
+
- - "<"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '6.0'
|
80
|
+
type: :runtime
|
81
|
+
prerelease: false
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '4.2'
|
87
|
+
- - "<"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '6.0'
|
90
|
+
description: Simple plugin to add CSV export feature to Administrate
|
91
|
+
email:
|
92
|
+
- hello@sourcelabs.io
|
93
|
+
executables: []
|
94
|
+
extensions: []
|
95
|
+
extra_rdoc_files: []
|
96
|
+
files:
|
97
|
+
- ".gitignore"
|
98
|
+
- ".rspec"
|
99
|
+
- ".travis.yml"
|
100
|
+
- Gemfile
|
101
|
+
- Gemfile.lock
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.md
|
104
|
+
- Rakefile
|
105
|
+
- administrate_exportable.gemspec
|
106
|
+
- app/views/admin/application/index.html.erb
|
107
|
+
- bin/console
|
108
|
+
- bin/setup
|
109
|
+
- lib/administrate_exportable.rb
|
110
|
+
- lib/administrate_exportable/exporter.rb
|
111
|
+
- lib/administrate_exportable/exporter_service.rb
|
112
|
+
- lib/administrate_exportable/version.rb
|
113
|
+
homepage: https://github.com/SourceLabsLLC/administrate_exportable
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata: {}
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - ">="
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0'
|
131
|
+
requirements: []
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 2.6.13
|
134
|
+
signing_key:
|
135
|
+
specification_version: 4
|
136
|
+
summary: Simple plugin to add CSV export feature to Administrate
|
137
|
+
test_files: []
|