ntq_excelsior 1.0.3 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c96b982e0a82d6d397c05c339052dfee85ccb954ece87b3a86e9f2af9cd22574
4
- data.tar.gz: 56fa79c641c2e54493d72068ab8bde737d9d60c2bc670c68107f32af2e9ac93b
3
+ metadata.gz: bf457474e835aa5716abbdf7004f795ca97d1796cf49c9528ca59b3a7b233d10
4
+ data.tar.gz: 71af4c5b26604522ee93d68013f238095d3f086091ae34c6e97e32749bec42d8
5
5
  SHA512:
6
- metadata.gz: a7b4c533e1840113c21795842e7c2d0ee2d5fa27226b0e6b78d263412126d2c1df18c1c577ead64ef7e9dc7ee4604aa395d86ff30f58e0f5cdf82d4232d3c5a0
7
- data.tar.gz: 58fe7e1282c8ec6ab29be8d39ad3c1119e5adbe269723e21bec5ae0ab6b61be97c3a6b5168a583ea6f5a2decf98d7a269326ae614f11859296eac148d2e31655
6
+ metadata.gz: e07c0aba24f0369cfdfe20635b78a61f9a2a9eded12bf116e698cda59b69345d1ef5cbf94149f4e1810ab2cbd88174beba3f43f2eff34525d8674c9899eb8239
7
+ data.tar.gz: f08ef0e18a77235445888759cb0d57c86d8885b215920960b75299978c1be91acf4602135eaa5d11e95a0cee5a72975658be1457a3b6916694154ea8c4412163
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ntq_excelsior (1.0.3)
4
+ ntq_excelsior (1.2.0)
5
5
  caxlsx (< 4)
6
6
  roo (< 3)
7
7
 
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # NtqExcelsior
2
2
 
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/8bc43b15a0a8bfc5d660/maintainability)](https://codeclimate.com/github/9troisquarts/ntq-excelsior/maintainability)
4
+
3
5
  Ntq excelsior simplifie l'import et l'export de vos données.
4
6
 
5
7
  ## Installation
@@ -32,7 +34,8 @@ class UserExporter < NtqExcelsior::Exporter
32
34
  [
33
35
  {
34
36
  title: "Utilisateurs",
35
- width: 4,
37
+ width: -> (context) { context[:current_user].can?(:access_to_email, User) ? 4 : 3 }
38
+ # width: 4,
36
39
  styles: [:bold]
37
40
  }
38
41
  ],
@@ -46,11 +49,13 @@ class UserExporter < NtqExcelsior::Exporter
46
49
  {
47
50
  title: 'Email',
48
51
  header_styles: [:blue],
49
- resolve: 'email'
52
+ resolve: 'email',
53
+ visible: -> (record, context) { context[:current_user].can?(:access_to_email, User) }
50
54
  },
51
55
  {
52
56
  title: 'Birthdate',
53
- resolve: 'birthdate'
57
+ resolve: 'birthdate',
58
+ visible: true # Optional
54
59
  }
55
60
  {
56
61
  title: 'Address (nested)',
@@ -71,18 +76,39 @@ class UserExporter < NtqExcelsior::Exporter
71
76
  end
72
77
 
73
78
  exporter = UserExporter.new(@users)
79
+ # Optional : Context can be passed to exporter
80
+ exporter.context = { current_user: current_user }
74
81
  stream = exporter.export.to_stream.read
75
82
 
76
83
  # In ruby file
77
- File.open("export.xlsx", "w") do |tpm|
78
- tpm.binmode
79
- tpm.write(stream)
84
+ File.open("export.xlsx", "w") do |tmp|
85
+ tmp.binmode
86
+ tmp.write(stream)
80
87
  end
81
88
 
82
89
  # In Controller action
83
90
  send_data stream, type: 'application/xlsx', filename: "filename.xlsx"
84
91
  ```
85
92
 
93
+ ### Multiple workbooks export
94
+
95
+ ```ruby
96
+ user_exporter = UserExporter.new(@user_data)
97
+ product_exporter = ProductExporter.new(@product_data)
98
+
99
+ exporter = NtqExcelsior::MultiWorkbookExporter.new([user_exporter, product_exporter])
100
+ stream = exporter.export.to_stream.read
101
+
102
+ # In ruby file
103
+ File.open("export.xlsx", "w") do |tmp|
104
+ tmp.binmode
105
+ tmp.write(stream)
106
+ end
107
+
108
+ # In Controller action
109
+ send_data stream, type: 'application/xlsx', filename: "filename.xlsx"
110
+ ```
111
+
86
112
  ### Import
87
113
 
88
114
  ```ruby
@@ -3,6 +3,7 @@ require 'caxlsx'
3
3
  module NtqExcelsior
4
4
  class Exporter
5
5
  attr_accessor :data
6
+ attr_accessor :context
6
7
 
7
8
  DEFAULT_STYLES = {
8
9
  date_format: {
@@ -91,13 +92,28 @@ module NtqExcelsior
91
92
  styles_hash
92
93
  end
93
94
 
95
+ def column_is_visible?(column, record = nil)
96
+ return true if !column.key?(:visible)
97
+ return column[:visible].call(record, context) if column[:visible].is_a?(Proc)
98
+
99
+ column[:visible]
100
+ end
101
+
102
+ def column_width(column)
103
+ return column[:width].call(context) if column[:width] && column[:width].is_a?(Proc)
104
+
105
+ column[:width] || 1
106
+ end
107
+
94
108
  def resolve_header_row(headers, index)
95
109
  row = { values: [], styles: [], merge_cells: [], height: nil }
96
110
  return row unless headers
97
-
111
+
98
112
  col_index = 1
99
113
  headers.each do |header|
100
- width = header[:width] || 1
114
+ next unless column_is_visible?(header)
115
+
116
+ width = column_width(header)
101
117
  row[:values] << header[:title] || ''
102
118
  row[:styles] << get_styles(header[:header_styles] || header[:styles])
103
119
  if width > 1
@@ -116,6 +132,8 @@ module NtqExcelsior
116
132
  v = value
117
133
  return v unless accessors && accessors.length > 0
118
134
 
135
+ return v.dig(*accessors) if v.is_a?(Hash)
136
+
119
137
  v = v.send(accessors[0])
120
138
  return v if accessors.length == 1
121
139
  return dig_value(v, accessors.slice(1..-1))
@@ -151,7 +169,9 @@ module NtqExcelsior
151
169
  row = { values: [], styles: [], merge_cells: [], height: nil, types: [] }
152
170
  col_index = 1
153
171
  schema.each do |column|
154
- width = column[:width] || 1
172
+ next unless column_is_visible?(column, record)
173
+
174
+ width = column_width(column)
155
175
  formatted_value = format_value(column[:resolve], record)
156
176
  row[:values] << formatted_value[:value]
157
177
  row[:types] << (column[:type] || formatted_value[:type])
@@ -211,7 +231,7 @@ module NtqExcelsior
211
231
  end
212
232
  end
213
233
 
214
- sheet.column_widths *content[:col_widths] if content[:col_widths].present?
234
+ sheet.column_widths * content[:col_widths] if content[:col_widths].present?
215
235
  end
216
236
 
217
237
  sheet
@@ -0,0 +1,28 @@
1
+ require 'caxlsx'
2
+
3
+ module NtqExcelsior
4
+ class MultiWorkbookExporter
5
+
6
+ attr_accessor :exporters
7
+
8
+ def initialize(exporters = [])
9
+ @exporters = exporters
10
+ end
11
+
12
+ def export
13
+ exports = exporters
14
+ exports = [exporters] if exporters && !exporters.is_a?(Array)
15
+
16
+ package = Axlsx::Package.new
17
+ wb = package.workbook
18
+ wb_styles = wb.styles
19
+
20
+ exports.each do |exporter|
21
+ exporter.generate_workbook(wb, wb_styles)
22
+ end
23
+
24
+ package
25
+ end
26
+
27
+ end
28
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NtqExcelsior
4
- VERSION = "1.0.3"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/ntq_excelsior.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative "ntq_excelsior/version"
4
4
  require 'ntq_excelsior/exporter'
5
+ require 'ntq_excelsior/multi_workbook_exporter'
5
6
  require 'ntq_excelsior/importer'
6
7
  module NtqExcelsior
7
8
  class Error < StandardError; end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ntq_excelsior
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-05 00:00:00.000000000 Z
11
+ date: 2023-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: caxlsx
@@ -58,6 +58,7 @@ files:
58
58
  - lib/ntq_excelsior.rb
59
59
  - lib/ntq_excelsior/exporter.rb
60
60
  - lib/ntq_excelsior/importer.rb
61
+ - lib/ntq_excelsior/multi_workbook_exporter.rb
61
62
  - lib/ntq_excelsior/version.rb
62
63
  - sig/ntq_excelsior.rbs
63
64
  homepage: https://github.com/9troisquarts/ntq-excelsior
@@ -67,7 +68,7 @@ metadata:
67
68
  homepage_uri: https://github.com/9troisquarts/ntq-excelsior
68
69
  source_code_uri: https://github.com/9troisquarts/ntq-excelsior
69
70
  changelog_uri: https://github.com/9troisquarts/ntq-excelsior/CHANGELOG.md
70
- post_install_message:
71
+ post_install_message:
71
72
  rdoc_options: []
72
73
  require_paths:
73
74
  - lib
@@ -83,7 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
84
  version: '0'
84
85
  requirements: []
85
86
  rubygems_version: 3.3.7
86
- signing_key:
87
+ signing_key:
87
88
  specification_version: 4
88
89
  summary: Library use by 9tq for import/export
89
90
  test_files: []