tramway-export 0.1.5.1 → 0.2.0.2

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
  SHA256:
3
- metadata.gz: ca1d5e0b55a03d41f70b47106f693e1d40fe8d63a932e6e260f6b5bd885a2189
4
- data.tar.gz: 51c9af0f074f8fbdfd7611852a34954d1d4e3f0dd84c5acd5a0bfc29ea58534b
3
+ metadata.gz: 797b8646f1afd946efe3b2926e75d95f056b6dd190d8424357e5b78c06bf70b9
4
+ data.tar.gz: 1ff12e2b514aa49dec363ebb473276df849f3504dbfd693ec29c8333424ca15f
5
5
  SHA512:
6
- metadata.gz: d8d7dc48a48d69eaaed4438d86de190461e67c707172438d574cf8a33b490035e6f532fcd53e98c5d2b85604bb5e9ddd9d07395786ad6bb59eb966de364f52ce
7
- data.tar.gz: b72abd00f7100ce4ae019ceaab3d40f65bc15b184bd9ce70dadbef69b4f178dd13bc4faa09cefe83400d108673426978176b1bd554b8fbb53d36178e2d754123
6
+ metadata.gz: 7faa2448f27af2740baafc8ec75c32c5b545098c4955f237696213a41ce8a4e03a298e4994169fd29773e01184768f614d3b31828087eb1c58baac388e449c9d
7
+ data.tar.gz: bc1e60703f934e9803d1c9e345bf6ef9576e0401c7407918894cd1865190ab29ef0a20f93667c45cc16d00cf4a98377cd3ea1c5efde3b5b08d37e44fe8de1cd6
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,8 +55,7 @@ class YourModelXlsDecorator < Tramway::Export::Xls::ApplicationDecorator
54
55
  end
55
56
  end
56
57
  ```
57
-
58
- #### 5. You can set flexible columns for each record. Just use `flexible_columns` method in object area
58
+ **If you don't have constant list of columns, use `flexible_columns` this way**
59
59
 
60
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
61
 
@@ -64,13 +64,13 @@ Just imagine: our model contains method `values` and we want to have columns acc
64
64
  class YourModelXlsDecorator < Tramway::Export::Xls::ApplicationDecorator
65
65
  def flexible_columns
66
66
  object.values.keys.map do |key|
67
- { key.to_sym => -> { values.dig(key) } }
67
+ { key.to_sym => -> { object.values.dig(key) } }
68
68
  end
69
69
  end
70
70
  end
71
71
  ```
72
72
 
73
- #### 6. Restart your server and visit index page of models in your admin panel
73
+ #### 5. Restart your server and visit index page of models in your admin panel
74
74
 
75
75
  ### User instructions
76
76
 
@@ -3,13 +3,13 @@
3
3
  require 'xls_exporter'
4
4
 
5
5
  class Tramway::Export::ExportsController < Tramway::Admin::ApplicationController
6
- def show
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 "#{current_user.role}_scope", current_user.id
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|
@@ -28,9 +28,48 @@ class Tramway::Export::ExportsController < Tramway::Admin::ApplicationController
28
28
  send_data stream.string, content_type: 'application/xls', filename: xls_decorator_class.filename
29
29
  end
30
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
+
31
56
  private
32
57
 
33
58
  def xls_decorator_class_name(model_name)
34
59
  "#{model_name}XlsDecorator".constantize
35
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?(model_class, 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
36
75
  end
@@ -6,6 +6,8 @@ class Tramway::Export::ApplicationDecorator
6
6
  end
7
7
 
8
8
  class << self
9
+ include Tramway::Core::Delegating::ClassHelper
10
+
9
11
  def columns
10
12
  []
11
13
  end
@@ -4,6 +4,9 @@ module Tramway
4
4
  module Export
5
5
  module Xls
6
6
  class ApplicationDecorator < Tramway::Export::ApplicationDecorator
7
+ def flexible_columns
8
+ []
9
+ end
7
10
  end
8
11
  end
9
12
  end
data/config/routes.rb CHANGED
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  Tramway::Export::Engine.routes.draw do
4
- resources :exports, only: [] do
5
- collection do
6
- get :show
7
- end
8
- end
4
+ resources :exports, only: [ :show, :index ]
9
5
  end
@@ -7,17 +7,20 @@ 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? model_class
21
+ @exportable_models[project.to_sym].map(&:to_s)&.include?(model_class.to_s) ||
22
+ @exportable_models[project.to_sym]&.map { |config| config.is_a?(Hash) && config.keys.first.to_s == model_class.to_s }&.include?(true)
18
23
  end
19
-
20
- attr_reader :exportable_models
21
24
  end
22
25
  end
23
26
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Tramway
4
4
  module Export
5
- VERSION = '0.1.5.1'
5
+ VERSION = '0.2.0.2'
6
6
  end
7
7
  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.1.5.1
4
+ version: 0.2.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: 2020-03-28 00:00:00.000000000 Z
11
+ date: 2021-03-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xls_exporter
@@ -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.1.2
72
+ rubygems_version: 3.1.4
73
73
  signing_key:
74
74
  specification_version: 4
75
75
  summary: Rails engine for exporting data