ntq_excelsior 1.0.3 → 1.1.0

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: c96b982e0a82d6d397c05c339052dfee85ccb954ece87b3a86e9f2af9cd22574
4
- data.tar.gz: 56fa79c641c2e54493d72068ab8bde737d9d60c2bc670c68107f32af2e9ac93b
3
+ metadata.gz: cb00b94aaef4ffb70a15389a97c245b70b990bc13b98bbdeff8d776ffe38ed18
4
+ data.tar.gz: c458cdc7bb84106d2a2d4c64ca93bb66cfce7e7592487fdcfedc04933c5cfe9a
5
5
  SHA512:
6
- metadata.gz: a7b4c533e1840113c21795842e7c2d0ee2d5fa27226b0e6b78d263412126d2c1df18c1c577ead64ef7e9dc7ee4604aa395d86ff30f58e0f5cdf82d4232d3c5a0
7
- data.tar.gz: 58fe7e1282c8ec6ab29be8d39ad3c1119e5adbe269723e21bec5ae0ab6b61be97c3a6b5168a583ea6f5a2decf98d7a269326ae614f11859296eac148d2e31655
6
+ metadata.gz: 3b441b018a18cc207f5afac705fd7eb3ee2a1cf42c1dd7066be4f8b59d374c66e477da93cd9e5efa5d67b32d11cae29e33d229260551eedbf8e573502f875c5a
7
+ data.tar.gz: ce1a262a95ebe529b5c65fd5cb4680699cd3f602cfffbf970cb785d8a9f673869440b71a2819e30aedb096dea32920435fc5d80bf6a9bc17a42f2607b00e819a
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.1.0)
5
5
  caxlsx (< 4)
6
6
  roo (< 3)
7
7
 
data/README.md CHANGED
@@ -32,7 +32,8 @@ class UserExporter < NtqExcelsior::Exporter
32
32
  [
33
33
  {
34
34
  title: "Utilisateurs",
35
- width: 4,
35
+ width: -> (context) { context[:current_user].can?(:access_to_email, User) ? 4 : 3 }
36
+ # width: 4,
36
37
  styles: [:bold]
37
38
  }
38
39
  ],
@@ -46,11 +47,13 @@ class UserExporter < NtqExcelsior::Exporter
46
47
  {
47
48
  title: 'Email',
48
49
  header_styles: [:blue],
49
- resolve: 'email'
50
+ resolve: 'email',
51
+ visible: -> (record, context) { context[:current_user].can?(:access_to_email, User) }
50
52
  },
51
53
  {
52
54
  title: 'Birthdate',
53
- resolve: 'birthdate'
55
+ resolve: 'birthdate',
56
+ visible: true # Optional
54
57
  }
55
58
  {
56
59
  title: 'Address (nested)',
@@ -71,6 +74,8 @@ class UserExporter < NtqExcelsior::Exporter
71
74
  end
72
75
 
73
76
  exporter = UserExporter.new(@users)
77
+ # Optional : Context can be passed to exporter
78
+ exporter.context = { current_user: current_user }
74
79
  stream = exporter.export.to_stream.read
75
80
 
76
81
  # In ruby file
@@ -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
@@ -151,7 +167,9 @@ module NtqExcelsior
151
167
  row = { values: [], styles: [], merge_cells: [], height: nil, types: [] }
152
168
  col_index = 1
153
169
  schema.each do |column|
154
- width = column[:width] || 1
170
+ next unless column_is_visible?(column, record)
171
+
172
+ width = column_width(column)
155
173
  formatted_value = format_value(column[:resolve], record)
156
174
  row[:values] << formatted_value[:value]
157
175
  row[:types] << (column[:type] || formatted_value[:type])
@@ -211,7 +229,7 @@ module NtqExcelsior
211
229
  end
212
230
  end
213
231
 
214
- sheet.column_widths *content[:col_widths] if content[:col_widths].present?
232
+ sheet.column_widths * content[:col_widths] if content[:col_widths].present?
215
233
  end
216
234
 
217
235
  sheet
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NtqExcelsior
4
- VERSION = "1.0.3"
4
+ VERSION = "1.1.0"
5
5
  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.1.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-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: caxlsx
@@ -67,7 +67,7 @@ metadata:
67
67
  homepage_uri: https://github.com/9troisquarts/ntq-excelsior
68
68
  source_code_uri: https://github.com/9troisquarts/ntq-excelsior
69
69
  changelog_uri: https://github.com/9troisquarts/ntq-excelsior/CHANGELOG.md
70
- post_install_message:
70
+ post_install_message:
71
71
  rdoc_options: []
72
72
  require_paths:
73
73
  - lib
@@ -83,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  version: '0'
84
84
  requirements: []
85
85
  rubygems_version: 3.3.7
86
- signing_key:
86
+ signing_key:
87
87
  specification_version: 4
88
88
  summary: Library use by 9tq for import/export
89
89
  test_files: []