ntq_excelsior 1.0.2 → 1.1.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: b29b9647c8d1431ec7f81883960af69dbaa8686bb29398e015c30b1d07c6e5f9
4
- data.tar.gz: c3b9e0e4fd0718d2cdeea72484f07755dff6376878ae4042599056233f08ae82
3
+ metadata.gz: cb00b94aaef4ffb70a15389a97c245b70b990bc13b98bbdeff8d776ffe38ed18
4
+ data.tar.gz: c458cdc7bb84106d2a2d4c64ca93bb66cfce7e7592487fdcfedc04933c5cfe9a
5
5
  SHA512:
6
- metadata.gz: dc5abd51daaae03d38f51329ee2201e04f758102b0bcc5794e987f033ddead70f04fb7da8755fb9b476742d20d724b0f827790291140c422e15b31c8319a8ab9
7
- data.tar.gz: 94a68a1fda84f0a79f1f3ac432622f18582f11e083ce9e4e5878bab5cebaef85bdd92830fcc90457acf9af9ee62270239d508bda6a1a20dd07bb38934c99a9b3
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.2)
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
@@ -64,7 +64,9 @@ module NtqExcelsior
64
64
  def spreadsheet_data
65
65
  begin
66
66
  spreadsheet_data = spreadsheet.sheet(spreadsheet.sheets[0]).parse(header_search: required_headers)
67
- raise 'File is inconsistent, please check all headers of your file for specials characters (, / ; etc...)' unless spreadsheet_data.size > 0
67
+ raise 'File is inconsistent, please check you have data in it or check for invalid characters in headers like , / ; etc...' unless spreadsheet_data.size > 0
68
+
69
+ spreadsheet_data
68
70
  rescue Roo::HeaderRowNotFoundError => e
69
71
  missing_headers = []
70
72
 
@@ -89,6 +91,7 @@ module NtqExcelsior
89
91
  return @header_scheme if @header_scheme
90
92
 
91
93
  @header_scheme = {}
94
+ # Read the first line of file (not header)
92
95
  l = spreadsheet_data[0].dup || []
93
96
 
94
97
  self.class.schema.each do |field, column_config|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NtqExcelsior
4
- VERSION = "1.0.2"
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.2
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: []