forest_admin_datasource_toolkit 1.0.0.pre.beta.32 → 1.0.0.pre.beta.34

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: 3f865bef547fbeaff014ef335c04a88cf63fd37bb6064b09973aab82042a5e42
4
- data.tar.gz: 690380cc8bb555950b7c9149a1fa3c314b387a3d8fe36c4bd38a36225277d59b
3
+ metadata.gz: c86aa154dce215a6e9d6888f6a2d9b2856d7ae4820246c6528a7d1b337db685c
4
+ data.tar.gz: 357cd5a8f885251fd59fe70e8b3c082e6171ae4297ebcbc9c6ac6015fcea1c2e
5
5
  SHA512:
6
- metadata.gz: b91235272e0de2880cb403a47d73c048a687e10262614d9093743a5cf10788d79ea83538e883246aaa4dcb9d16e9cf675f3ffbc778239a97803dcd68c6a8a77e
7
- data.tar.gz: 1e9cbe19df7596855bae67fc3415c7b2c8698367ab5b66f8c81c4756edbefb3a23151dca38754244dd54c8d370b637f27151a46b058912fe93246d60dee2c13e
6
+ metadata.gz: 7de219bdb22ae93487e9f96428d58e3820e8f8e1a7cc9bb9222b567c79060f05fbd2e8c170f45f3eb20a5bf2d04bac8f7dec07cd23ef025bfe6f8e18fbf515d9
7
+ data.tar.gz: 485077904b49e4c80db4ad277cf52f11c4ce08ffe340c279a7a8a3be597869ae84d414d58c7af7c39638d74635b4a8b7bf719f84994c7b2c680234abb8cc8bdb
@@ -23,10 +23,10 @@ module ForestAdminDatasourceToolkit
23
23
 
24
24
  def projection
25
25
  aggregate_fields = []
26
- aggregate_fields << field if field
26
+ aggregate_fields << field.to_s if field
27
27
 
28
28
  groups.each do |group|
29
- aggregate_fields << group[:field]
29
+ aggregate_fields << group[:field].to_s
30
30
  end
31
31
 
32
32
  Projection.new(aggregate_fields)
@@ -132,7 +132,7 @@ module ForestAdminDatasourceToolkit
132
132
  group = {}
133
133
 
134
134
  groups.each do |value|
135
- group_value = record[value[:field]]
135
+ group_value = ForestAdminDatasourceToolkit::Utils::Record.field_value(record, value[:field])
136
136
  group[value[:field]] = apply_date_operation(group_value, value[:operation], timezone)
137
137
  end
138
138
 
@@ -61,7 +61,8 @@ module ForestAdminDatasourceToolkit
61
61
 
62
62
  def match(record, collection, timezone)
63
63
  field_value = Record.field_value(record, @field)
64
- column_type = Utils::Collection.get_field_schema(collection, @field).column_type
64
+ column_type = ForestAdminDatasourceToolkit::Utils::Collection.get_field_schema(collection,
65
+ @field).column_type
65
66
 
66
67
  supported = [
67
68
  Operators::IN,
@@ -81,7 +82,7 @@ module ForestAdminDatasourceToolkit
81
82
 
82
83
  case @operator
83
84
  when Operators::IN
84
- Array(@value).include?(field_value)
85
+ @value.include?(field_value)
85
86
  when Operators::EQUAL
86
87
  field_value == @value
87
88
  when Operators::LESS_THAN
@@ -105,8 +105,8 @@ module ForestAdminDatasourceToolkit
105
105
 
106
106
  def self.make_through_filter(collection, id, relation_name, caller, base_foreign_filter)
107
107
  relation = collection.schema[:fields][relation_name]
108
- origin_value = Utils::Collection.get_value(collection, caller, id, relation.origin_key_target)
109
- foreign_relation = Utils::Collection.get_through_target(collection, relation_name)
108
+ origin_value = ForestAdminDatasourceToolkit::Utils::Collection.get_value(collection, caller, id, relation.origin_key_target)
109
+ foreign_relation = ForestAdminDatasourceToolkit::Utils::Collection.get_through_target(collection, relation_name)
110
110
 
111
111
  # Optimization for many to many when there is not search/segment (saves one query)
112
112
  if foreign_relation && base_foreign_filter.nestable?
@@ -27,9 +27,10 @@ module ForestAdminDatasourceToolkit
27
27
  each_with_object({}) do |path, memo|
28
28
  next unless path.include?(':')
29
29
 
30
- split_path = path.split(':')
31
- relation = split_path[0]
32
- memo[relation] = Projection.new([split_path[1]].union(memo[relation] || []))
30
+ original_path = path.split(':')
31
+ relation = original_path.shift
32
+
33
+ memo[relation] = Projection.new([original_path.join(':')].union(memo[relation] || []))
33
34
  end
34
35
  end
35
36
 
@@ -47,10 +48,12 @@ module ForestAdminDatasourceToolkit
47
48
  def replace(...)
48
49
  Projection.new(
49
50
  map(...)
50
- .reduce(Projection.new) do |memo, path|
51
- return memo.union([path]) if path.is_a?(String)
52
-
53
- memo.union(path)
51
+ .reduce(Projection.new) do |memo, path|
52
+ if path.is_a?(String)
53
+ memo.union([path])
54
+ else
55
+ memo.union(path)
56
+ end
54
57
  end
55
58
  )
56
59
  end
@@ -58,6 +61,27 @@ module ForestAdminDatasourceToolkit
58
61
  def equals(other)
59
62
  length == other.length && all? { |field| other.include?(field) }
60
63
  end
64
+
65
+ def apply(records)
66
+ records.map { |record| re_project(record) }
67
+ end
68
+
69
+ def re_project(record)
70
+ result = nil
71
+
72
+ if record
73
+ record = HashHelper.convert_keys(record, :to_s)
74
+ result = {}
75
+ columns.each { |column| result[column.to_s] = record[column.to_s] }
76
+ relations.each { |relation, projection| result[relation] = projection.re_project(record[relation]) }
77
+ end
78
+
79
+ result
80
+ end
81
+
82
+ def union(other_arrays)
83
+ Projection.new(other_arrays.to_a.union(self))
84
+ end
61
85
  end
62
86
  end
63
87
  end
@@ -0,0 +1,68 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Components
3
+ module Query
4
+ class Sort < Array
5
+ def projection
6
+ Projection.new(map { |clause| clause[:field] })
7
+ end
8
+
9
+ def replace_clauses(...)
10
+ Sort.new(
11
+ map(...)
12
+ .reduce(Sort.new) do |memo, clause|
13
+ if clause.is_a?(Array) || clause.is_a?(self.class)
14
+ memo.union(clause)
15
+ else
16
+ memo.union([clause])
17
+ end
18
+ end
19
+ )
20
+ end
21
+
22
+ def nest(prefix)
23
+ if prefix&.length
24
+ self.class.new(map do |ob|
25
+ { field: "#{prefix}:#{ob[:field]}", ascending: ob[:ascending] }
26
+ end)
27
+ else
28
+ self
29
+ end
30
+ end
31
+
32
+ def inverse
33
+ self.class.new(map { |ob| { field: ob[:field], ascending: !ob[:ascending] } })
34
+ end
35
+
36
+ def unnest
37
+ prefix = first[:field].split(':')[0]
38
+ raise 'Cannot unnest sort.' unless all? { |ob| ob[:field].start_with?(prefix) }
39
+
40
+ self.class.new(map do |ob|
41
+ { field: ob[:field][prefix.length + 1, ob[:field].length - prefix.length - 1],
42
+ ascending: ob[:ascending] }
43
+ end)
44
+ end
45
+
46
+ def apply(records)
47
+ records.sort do |a, b|
48
+ comparison = 0
49
+ (0..length - 1).each do |i|
50
+ field = self[i][:field]
51
+ ascending = self[i][:ascending]
52
+ break unless comparison.zero?
53
+
54
+ value_on_a = ForestAdminDatasourceToolkit::Utils::Record.field_value(a, field)
55
+ value_on_b = ForestAdminDatasourceToolkit::Utils::Record.field_value(b, field)
56
+
57
+ comparison = value_on_a <=> value_on_b
58
+ comparison = 1 if comparison.nil?
59
+ comparison *= -1 unless ascending
60
+ end
61
+
62
+ comparison
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,16 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Components
3
+ module Query
4
+ module SortUtils
5
+ class SortFactory
6
+ def self.by_primary_keys(collection)
7
+ ForestAdminDatasourceToolkit::Components::Query::Sort.new(
8
+ ForestAdminDatasourceToolkit::Utils::Schema.primary_keys(collection)
9
+ .map { |pk| { field: pk, ascending: true } }
10
+ )
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Validations
3
+ class SortValidator
4
+ def self.validate(collection, sort)
5
+ sort&.each do |s|
6
+ FieldValidator.validate(collection, s[:field])
7
+ unless s[:ascending].is_a?(TrueClass) || s[:ascending].is_a?(FalseClass)
8
+ raise ForestAdminDatasourceToolkit::Exceptions::ValidationError,
9
+ "Invalid sort_utils.ascending value: #{s[:ascending]}"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceToolkit
2
- VERSION = "1.0.0-beta.32"
2
+ VERSION = "1.0.0-beta.34"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_datasource_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre.beta.32
4
+ version: 1.0.0.pre.beta.34
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-03-01 00:00:00.000000000 Z
12
+ date: 2024-03-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -83,6 +83,8 @@ files:
83
83
  - lib/forest_admin_datasource_toolkit/components/query/page.rb
84
84
  - lib/forest_admin_datasource_toolkit/components/query/projection.rb
85
85
  - lib/forest_admin_datasource_toolkit/components/query/projection_factory.rb
86
+ - lib/forest_admin_datasource_toolkit/components/query/sort.rb
87
+ - lib/forest_admin_datasource_toolkit/components/query/sort_utils/sort_factory.rb
86
88
  - lib/forest_admin_datasource_toolkit/datasource.rb
87
89
  - lib/forest_admin_datasource_toolkit/decorators/collection_decorator.rb
88
90
  - lib/forest_admin_datasource_toolkit/decorators/datasource_decorator.rb
@@ -104,6 +106,7 @@ files:
104
106
  - lib/forest_admin_datasource_toolkit/validations/field_validator.rb
105
107
  - lib/forest_admin_datasource_toolkit/validations/projection_validator.rb
106
108
  - lib/forest_admin_datasource_toolkit/validations/rules.rb
109
+ - lib/forest_admin_datasource_toolkit/validations/sort_validator.rb
107
110
  - lib/forest_admin_datasource_toolkit/validations/type_getter.rb
108
111
  - lib/forest_admin_datasource_toolkit/version.rb
109
112
  - sig/forest_admin_datasource_toolkit.rbs