tramway-export 0.1.4 → 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 +4 -4
- data/README.md +15 -0
- data/app/controllers/tramway/export/exports_controller.rb +43 -2
- data/app/decorators/tramway/export/application_decorator.rb +2 -0
- data/app/decorators/tramway/export/xls/application_decorator.rb +3 -0
- data/app/views/tramway/export/_button.html.haml +1 -1
- data/config/routes.rb +1 -5
- data/lib/tramway/export.rb +8 -7
- data/lib/tramway/export/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8629ca9795a930ddd76a2838536e4dca3d116a21fded0978ad8278b366e0d9d3
|
4
|
+
data.tar.gz: 178e67ebb1b2cce89b9ab863190151dedb5e93aa8924c019c0e0fc1042d96a81
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b6501955c42631a2607883c0a6ddd44443646ce35f090d4dface449d496906bf045ef7fdae7bd227b81c95230cea6c4e0088c3a1906c0bfdc16a1adac1e7d033
|
7
|
+
data.tar.gz: b8ec8841c5c3a493a54509030ed17c9b063c7922f58c4aad2900de771a82a26bcf1ebee6ace7acd39fd42aab55114e6f710d5c7215f61a44e211f4ac2ead63c2
|
data/README.md
CHANGED
@@ -26,6 +26,7 @@ Tramway::Export.set_exportable_models YourModel, project: :your_project_name
|
|
26
26
|
|
27
27
|
#### 4. Create decorator that describes, how your model should be exported. You need to create exactly decorator name `app/decorators/#{your_model_name}_xls_decorator.rb`
|
28
28
|
|
29
|
+
**If you have constant list of columns just use `columns` method this way**
|
29
30
|
|
30
31
|
*app/decorators/your_model_xls_decorator.rb*
|
31
32
|
|
@@ -54,6 +55,20 @@ class YourModelXlsDecorator < Tramway::Export::Xls::ApplicationDecorator
|
|
54
55
|
end
|
55
56
|
end
|
56
57
|
```
|
58
|
+
**If you don't have constant list of columns, use `flexible_columns` this way**
|
59
|
+
|
60
|
+
Just imagine: our model contains method `values` and we want to have columns according to `values` keys. You shouldn't create methods for every column. Just use `Proc`. It's provided by gem `xls_exporter`. You can read more about [here](https://github.com/kalashnikovisme/xls-exporter).
|
61
|
+
|
62
|
+
*app/decorators/your_model_xls_decorator.rb*
|
63
|
+
```ruby
|
64
|
+
class YourModelXlsDecorator < Tramway::Export::Xls::ApplicationDecorator
|
65
|
+
def flexible_columns
|
66
|
+
object.values.keys.map do |key|
|
67
|
+
{ key.to_sym => -> { object.values.dig(key) } }
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
57
72
|
|
58
73
|
#### 5. Restart your server and visit index page of models in your admin panel
|
59
74
|
|
@@ -3,13 +3,13 @@
|
|
3
3
|
require 'xls_exporter'
|
4
4
|
|
5
5
|
class Tramway::Export::ExportsController < Tramway::Admin::ApplicationController
|
6
|
-
def
|
6
|
+
def index
|
7
7
|
scope = params[:scope].present? ? params[:scope] : :all
|
8
8
|
model_class = model_class_name(params[:model])
|
9
9
|
xls_decorator_class = xls_decorator_class_name(params[:model])
|
10
10
|
records = model_class.active.order(id: :desc).send scope
|
11
11
|
records = records.ransack(params[:filter]).result if params[:filter].present?
|
12
|
-
records = records.send "#{
|
12
|
+
records = records.send "#{current_admin.role}_scope", current_admin.id
|
13
13
|
records = xls_decorator_class.decorate records
|
14
14
|
|
15
15
|
columns = xls_decorator_class.columns + records.map(&:flexible_columns).flatten.uniq do |hash|
|
@@ -17,6 +17,8 @@ class Tramway::Export::ExportsController < Tramway::Admin::ApplicationController
|
|
17
17
|
end
|
18
18
|
|
19
19
|
book = ::XlsExporter.export do
|
20
|
+
default_style horizontal_align: :center, vertical_align: :center, text_wrap: true
|
21
|
+
|
20
22
|
add_sheet 'List'
|
21
23
|
|
22
24
|
export_models records, *columns
|
@@ -26,9 +28,48 @@ class Tramway::Export::ExportsController < Tramway::Admin::ApplicationController
|
|
26
28
|
send_data stream.string, content_type: 'application/xls', filename: xls_decorator_class.filename
|
27
29
|
end
|
28
30
|
|
31
|
+
def show
|
32
|
+
head(:unprocessable_entity) && return unless available?
|
33
|
+
|
34
|
+
model_class = model_class_name(params[:model])
|
35
|
+
xls_collection_decorator_class = xls_collection_decorator_class_name(params[:model], params[:collection])
|
36
|
+
records = model_class.find(params[:id]).send(params[:collection]).active.order(id: :desc)
|
37
|
+
records = records.send "#{current_admin.role}_scope", current_admin.id
|
38
|
+
records = xls_collection_decorator_class.decorate records
|
39
|
+
|
40
|
+
columns = xls_collection_decorator_class.columns + records.map(&:flexible_columns).flatten.uniq do |hash|
|
41
|
+
hash&.keys&.first
|
42
|
+
end
|
43
|
+
|
44
|
+
book = ::XlsExporter.export do
|
45
|
+
default_style horizontal_align: :center, vertical_align: :center, text_wrap: true
|
46
|
+
|
47
|
+
add_sheet xls_collection_decorator_class.sheet_name
|
48
|
+
|
49
|
+
export_models records, *columns
|
50
|
+
end
|
51
|
+
stream = StringIO.new
|
52
|
+
book.write stream
|
53
|
+
send_data stream.string, content_type: 'application/xls', filename: xls_collection_decorator_class.filename
|
54
|
+
end
|
55
|
+
|
29
56
|
private
|
30
57
|
|
31
58
|
def xls_decorator_class_name(model_name)
|
32
59
|
"#{model_name}XlsDecorator".constantize
|
33
60
|
end
|
61
|
+
|
62
|
+
def xls_collection_decorator_class_name(model_name, collection)
|
63
|
+
"#{model_name}::#{collection.camelize}XlsDecorator".constantize
|
64
|
+
end
|
65
|
+
|
66
|
+
def available?
|
67
|
+
if params[:collection].present?
|
68
|
+
::Tramway::Export.exportable_model?(params[:model], project: @application.name) && Tramway::Export.exportable_models(project: @application.name)&.map do |config|
|
69
|
+
config.is_a?(Hash) && config.values.first.map(&:to_s).include?(params[:collection])
|
70
|
+
end&.include?(true)
|
71
|
+
else
|
72
|
+
::Tramway::Export.exportable_model?(params[:model], project: @application.name)
|
73
|
+
end
|
74
|
+
end
|
34
75
|
end
|
@@ -6,4 +6,4 @@
|
|
6
6
|
= fa_icon(:download)
|
7
7
|
.dropdown-menu{ aria: { labelledby: id } }
|
8
8
|
- decorator_class.collections.each do |collection|
|
9
|
-
= link_to collection_human_name(model_name: model_name, collection_name: collection), Tramway::Export::Engine.routes.url_helpers.exports_path(model: model_class, scope: collection, filter: params[:filter]
|
9
|
+
= link_to collection_human_name(model_name: model_name, collection_name: collection), Tramway::Export::Engine.routes.url_helpers.exports_path(model: model_class, scope: collection, filter: params[:filter].present? ? params[:filter].permit! : nil), class: 'dropdown-item'
|
data/config/routes.rb
CHANGED
data/lib/tramway/export.rb
CHANGED
@@ -7,18 +7,19 @@ module Tramway
|
|
7
7
|
class << self
|
8
8
|
def set_exportable_models(*models, project:)
|
9
9
|
@exportable_models ||= {}
|
10
|
-
@exportable_models[project] ||= []
|
11
|
-
@exportable_models[project] += models
|
10
|
+
@exportable_models[project.to_sym] ||= []
|
11
|
+
@exportable_models[project.to_sym] += models
|
12
|
+
end
|
13
|
+
|
14
|
+
def exportable_models(project:)
|
15
|
+
@exportable_models[project.to_sym]
|
12
16
|
end
|
13
17
|
|
14
18
|
def exportable_model?(model_class, project:)
|
15
19
|
return false unless project.present?
|
16
20
|
|
17
|
-
@exportable_models[project.to_sym]&.include?
|
18
|
-
|
19
|
-
|
20
|
-
def exportable_models
|
21
|
-
@exportable_models
|
21
|
+
@exportable_models[project.to_sym]&.include?(model_class) ||
|
22
|
+
@exportable_models[project.to_sym].map { |config| config.is_a?(Hash) && config.keys.first.to_s == model_class.to_s }.include?(true)
|
22
23
|
end
|
23
24
|
end
|
24
25
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tramway-export
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.2'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: xls_exporter
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '1.2'
|
20
20
|
type: :runtime
|
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: '
|
26
|
+
version: '1.2'
|
27
27
|
description: Rails engine for exporting data
|
28
28
|
email:
|
29
29
|
- kalashnikovisme@gmail.com
|
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
69
69
|
- !ruby/object:Gem::Version
|
70
70
|
version: '0'
|
71
71
|
requirements: []
|
72
|
-
rubygems_version: 3.
|
72
|
+
rubygems_version: 3.1.4
|
73
73
|
signing_key:
|
74
74
|
specification_version: 4
|
75
75
|
summary: Rails engine for exporting data
|