forest_admin_datasource_customizer 1.0.0.pre.beta.59 → 1.0.0.pre.beta.60

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: 3a160e0769fe4734ac04080ed73d361950fe0c6472b176b4d740c3fcf8629f4c
4
- data.tar.gz: b780957f6e918f2e3087c13f2238e16d588424330b87936d4c28f25fc808da53
3
+ metadata.gz: 546f0cd46dfa45397848a4cf173fc66573b7adf07c68e2255b7b2ee44703d440
4
+ data.tar.gz: 62ee92da96f2e7c5b4d1fd3ed90c9aff152356c26545cec579d03ff40da5f4c3
5
5
  SHA512:
6
- metadata.gz: 712302bdd282a8b93b9a60dbdae302de284cf88d20d1fad01e80686d22030e4b4a023e686e7ad5362afa11f46bb052471156a4e0d3add5a0cc5f5150b3fa5e34
7
- data.tar.gz: 4f36d52a349d605feac5d44e1b118dfd7a3306083f7e9bfbb2a797a1335097691763a96a3531ba22aa7f3f4a3501211012938a33e00afd66126e260c69a57509
6
+ metadata.gz: b435320c4af013dcef24be9ef7bcf9095cec3deaddb32923d82a027e4149377eddb82fcfbc12e569f5ab34e6121a616d52dba920a0cf22e3558d820016f7af9c
7
+ data.tar.gz: '09f6e4b2379e15017598b9b90331f6e9bae0b9fe39a2bb4ff7da6c3a90c1e135eedf295b7d514f2927605e1a37dd91ac1efa23bd06c852521f10a938ccf1e5c7'
@@ -19,28 +19,36 @@ module ForestAdminDatasourceCustomizer
19
19
  return if paths.include?(new_path)
20
20
 
21
21
  computed = collection.get_computed(new_path)
22
- nested_dependencies = Projection.new(computed.dependencies)
23
- .nest(prefix: new_path.include?(':') ? new_path.split(':')[0] : nil)
22
+ computed_dependencies = Flattener.with_null_marker(computed.dependencies)
23
+ nested_dependencies = Projection.new(computed_dependencies)
24
+ .nest(
25
+ prefix: if new_path.include?(':')
26
+ new_path.slice(0, new_path.rindex(':'))
27
+ end
28
+ )
24
29
 
25
30
  nested_dependencies.each do |path|
26
31
  queue_field(ctx, collection, path, paths, flatten)
27
32
  end
28
33
 
29
34
  dependency_values = nested_dependencies.map { |path| flatten[paths.index(path)] }
35
+
30
36
  paths.push(new_path)
31
37
 
32
- flatten << compute_field(ctx, computed, computed.dependencies, dependency_values)
38
+ flatten << compute_field(ctx, computed, computed_dependencies, dependency_values)
33
39
  end
34
40
 
35
41
  def self.compute_from_records(ctx, collection, records_projection, desired_projection, records)
36
- paths = records_projection.clone
42
+ paths = Flattener.with_null_marker(records_projection)
37
43
  flatten = Flattener.flatten(records, paths)
38
44
 
39
45
  desired_projection.each do |path|
40
46
  queue_field(ctx, collection, path, paths, flatten)
41
47
  end
42
48
 
43
- Flattener.un_flatten(desired_projection.map { |path| flatten[paths.index(path)] }, desired_projection)
49
+ final_projection = Flattener.with_null_marker(desired_projection)
50
+
51
+ Flattener.un_flatten(final_projection.map { |path| flatten[paths.index(path)] }, final_projection)
44
52
  end
45
53
 
46
54
  def self.transform_unique_values(inputs, callback)
@@ -3,9 +3,55 @@ module ForestAdminDatasourceCustomizer
3
3
  module Computed
4
4
  module Utils
5
5
  class Flattener
6
+ include ForestAdminDatasourceToolkit::Components::Query
7
+
8
+ class Undefined
9
+ def key?(_key)
10
+ false
11
+ end
12
+
13
+ def dig(*_keys)
14
+ self
15
+ end
16
+ end
17
+
18
+ MARKER_NAME = '__null_marker'.freeze
19
+ def self.with_null_marker(projection)
20
+ new_projection = Projection.new(projection)
21
+ projection.each do |path|
22
+ parts = path.split(':')
23
+
24
+ parts.slice(1, parts.size).each_with_index do |_item, index|
25
+ new_projection << "#{parts.slice(0, index + 1).join(":")}:#{MARKER_NAME}"
26
+ end
27
+ end
28
+
29
+ new_projection.uniq
30
+ end
31
+
6
32
  def self.flatten(records, projection)
7
33
  projection.map do |field|
8
- records.map { |record| ForestAdminDatasourceToolkit::Utils::Record.field_value(record, field) }
34
+ parts = field.split(':')
35
+ records.map do |record|
36
+ value = record
37
+
38
+ parts.slice(0, parts.size - 1).each_with_index do |_item, index|
39
+ value = if value&.key?(parts[index])
40
+ value[parts[index]]
41
+ else
42
+ Undefined.new
43
+ end
44
+ end
45
+
46
+ # for markers, the value tells us which fields are null so that we can set them.
47
+ if parts[parts.length - 1] == MARKER_NAME
48
+ value.nil? ? nil : Undefined.new
49
+ elsif value&.key?(parts[parts.length - 1])
50
+ value[parts[parts.length - 1]]
51
+ else
52
+ Undefined.new
53
+ end
54
+ end
9
55
  end
10
56
  end
11
57
 
@@ -17,11 +63,11 @@ module ForestAdminDatasourceCustomizer
17
63
  records[record_index] = {}
18
64
 
19
65
  projection.each_with_index do |path, path_index|
20
- parts = path.split(':').reject { |part| part.nil? || part.empty? }
66
+ parts = path.split(':').reject { |part| part == MARKER_NAME }
21
67
  value = flatten[path_index][record_index]
22
68
 
23
69
  # Ignore undefined values.
24
- next if value.nil?
70
+ next if value.is_a? Undefined
25
71
 
26
72
  # Set all others (including null)
27
73
  record = records[record_index]
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceCustomizer
2
- VERSION = "1.0.0-beta.59"
2
+ VERSION = "1.0.0-beta.60"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_datasource_customizer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.beta.59
4
+ version: 1.0.0.pre.beta.60
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthieu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-07-01 00:00:00.000000000 Z
12
+ date: 2024-07-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport