forest_admin_datasource_toolkit 1.0.0.pre.beta.61 → 1.0.0.pre.beta.62

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: 75c2a7dd3e73ab35a21a709f66a26b010451b12b351beee9a406f0d8bd603c30
4
- data.tar.gz: af73c66990a1e2428bec50bcf84f7114ac2b7b5ae4a41a4541ede3a4722460c7
3
+ metadata.gz: bc0b57418aca698691178e0871421fd15bbd914dc89b2b449cc4af62114b46dc
4
+ data.tar.gz: ab89da8265eb3bb14c2e3cf7db7d7d795e4a64f623b8bb088def57488a043f8e
5
5
  SHA512:
6
- metadata.gz: 4514679b848d96a9321d9a8fd0082e2f7dbde52ffa3e9168cbb7073a0b98cadaaf70141091a934df1a1021c6a5b65a7c66f913e636be651bee8c7935197a1b97
7
- data.tar.gz: be5eb58b7380562ae3e9fea7de7f8c540bc7f9502d45b4c0b6e7bdb8760567f6c32ea68f1be61e22db0a9ec4cb4aca0a58aa3ec1c054e22056c86729306bc856
6
+ metadata.gz: d68d8cd582bdafaab2f8aa183f2197518be454567cc0ec661f2ca20fc21adc147eedc9e45d9ca558211124a36443d75356f2ecb9bdee20cec43ec01dcf67924d
7
+ data.tar.gz: 77a33ebd8cc869736e6f68a89325278790806d79e7aa111a63bd13b24069c83128705af27bf098a732fba2f5e6cf633037d71435a4779f38ed554f535023a41d
@@ -58,14 +58,24 @@ module ForestAdminDatasourceToolkit
58
58
  relation = ForestAdminDatasourceToolkit::Utils::Schema.get_to_many_relation(collection, relation_name)
59
59
  origin_value = ForestAdminDatasourceToolkit::Utils::Collection.get_value(collection, caller, id,
60
60
  relation.origin_key_target)
61
+
61
62
  if relation.is_a?(OneToManySchema)
62
63
  origin_tree = Nodes::ConditionTreeLeaf.new(relation.origin_key, Operators::EQUAL, origin_value)
64
+ elsif relation.is_a?(PolymorphicOneToManySchema)
65
+ origin_tree = ConditionTreeFactory.intersect(
66
+ [
67
+ Nodes::ConditionTreeLeaf.new(relation.origin_key, Operators::EQUAL, origin_value),
68
+ Nodes::ConditionTreeLeaf.new(relation.origin_type_field, Operators::EQUAL, relation.origin_type_value)
69
+ ]
70
+ )
63
71
  else
64
72
  through_collection = collection.datasource.get_collection(relation.through_collection)
65
- through_tree = ConditionTreeFactory.intersect([
66
- Nodes::ConditionTreeLeaf.new(relation.origin_key, Operators::EQUAL, origin_value),
67
- Nodes::ConditionTreeLeaf.new(relation.foreign_key, Operators::PRESENT)
68
- ])
73
+ through_tree = ConditionTreeFactory.intersect(
74
+ [
75
+ Nodes::ConditionTreeLeaf.new(relation.origin_key, Operators::EQUAL, origin_value),
76
+ Nodes::ConditionTreeLeaf.new(relation.foreign_key, Operators::PRESENT)
77
+ ]
78
+ )
69
79
  records = through_collection.list(
70
80
  caller,
71
81
  Filter.new(condition_tree: through_tree),
@@ -10,6 +10,8 @@ module ForestAdminDatasourceToolkit
10
10
 
11
11
  relations.each do |relation, projection|
12
12
  schema = collection.schema[:fields][relation]
13
+ next unless schema.type != 'PolymorphicManyToOne'
14
+
13
15
  association = collection.datasource.get_collection(schema.foreign_collection)
14
16
  projection_with_pks = projection.with_pks(association).nest(prefix: relation)
15
17
 
@@ -23,15 +25,19 @@ module ForestAdminDatasourceToolkit
23
25
  reject { |field| field.include?(':') }
24
26
  end
25
27
 
26
- def relations
27
- each_with_object({}) do |path, memo|
28
+ def relations(only_keys: false)
29
+ relations = each_with_object({}) do |path, memo|
28
30
  next unless path.include?(':')
29
31
 
30
32
  original_path = path.split(':')
33
+ next if original_path.size == 1 && !only_keys
34
+
31
35
  relation = original_path.shift
32
36
 
33
37
  memo[relation] = Projection.new([original_path.join(':')].union(memo[relation] || []))
34
38
  end
39
+
40
+ only_keys ? relations.keys : relations
35
41
  end
36
42
 
37
43
  def nest(prefix: nil)
@@ -9,7 +9,7 @@ module ForestAdminDatasourceToolkit
9
9
  schema = path[1]
10
10
  memo += [column_name] if schema.type == 'Column'
11
11
 
12
- if schema.type == 'OneToOne' || schema.type == 'ManyToOne'
12
+ if schema.type == 'OneToOne' || schema.type == 'ManyToOne' || schema.type == 'PolymorphicOneToOne'
13
13
  relation = collection.datasource.get_collection(schema.foreign_collection)
14
14
  relation_columns = relation.schema[:fields]
15
15
  .select { |_column_name, relation_column| relation_column.type == 'Column' }
@@ -19,6 +19,8 @@ module ForestAdminDatasourceToolkit
19
19
  memo += relation_columns
20
20
  end
21
21
 
22
+ memo += ["#{column_name}:*"] if schema.type == 'PolymorphicManyToOne'
23
+
22
24
  memo
23
25
  end
24
26
 
@@ -0,0 +1,23 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Schema
3
+ module Relations
4
+ class PolymorphicManyToOneSchema
5
+ attr_reader :foreign_key_target, :foreign_key, :foreign_key_targets, :foreign_key_type_field,
6
+ :foreign_collections, :type
7
+
8
+ def initialize(
9
+ foreign_key_type_field:,
10
+ foreign_key:,
11
+ foreign_key_targets:,
12
+ foreign_collections:
13
+ )
14
+ @foreign_key = foreign_key
15
+ @foreign_key_targets = foreign_key_targets
16
+ @foreign_key_type_field = foreign_key_type_field
17
+ @foreign_collections = foreign_collections
18
+ @type = 'PolymorphicManyToOne'
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Schema
3
+ module Relations
4
+ class PolymorphicOneToManySchema < RelationSchema
5
+ attr_accessor :origin_key
6
+ attr_reader :origin_key_target, :origin_type_field, :origin_type_value
7
+
8
+ def initialize(origin_key:, origin_key_target:, foreign_collection:, origin_type_field:, origin_type_value:)
9
+ super(foreign_collection, 'PolymorphicOneToMany')
10
+ @origin_key = origin_key
11
+ @origin_key_target = origin_key_target
12
+ @origin_type_field = origin_type_field
13
+ @origin_type_value = origin_type_value
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module ForestAdminDatasourceToolkit
2
+ module Schema
3
+ module Relations
4
+ class PolymorphicOneToOneSchema < RelationSchema
5
+ attr_accessor :origin_key
6
+ attr_reader :origin_key_target, :origin_type_field, :origin_type_value
7
+
8
+ def initialize(origin_key:, origin_key_target:, foreign_collection:, origin_type_field:, origin_type_value:)
9
+ super(foreign_collection, 'PolymorphicOneToOne')
10
+ @origin_key = origin_key
11
+ @origin_key_target = origin_key_target
12
+ @origin_type_field = origin_type_field
13
+ @origin_type_value = origin_type_value
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -9,20 +9,25 @@ module ForestAdminDatasourceToolkit
9
9
  def self.get_inverse_relation(collection, relation_name)
10
10
  relation_field = collection.schema[:fields][relation_name]
11
11
  foreign_collection = collection.datasource.get_collection(relation_field.foreign_collection)
12
+ polymorphic_relations = %w[PolymorphicOneToOne PolymorphicOneToMany]
12
13
 
13
14
  inverse = foreign_collection.schema[:fields].select do |_name, field|
14
- field.is_a?(RelationSchema) &&
15
- field.foreign_collection == collection.name &&
16
- (
17
- (field.is_a?(ManyToManySchema) &&
18
- relation_field.is_a?(ManyToManySchema) &&
19
- many_to_many_inverse?(field, relation_field)) ||
20
- (field.is_a?(ManyToOneSchema) &&
21
- (relation_field.is_a?(OneToOneSchema) || relation_field.is_a?(OneToManySchema)) &&
22
- many_to_one_inverse?(field, relation_field)) ||
23
- ((field.is_a?(OneToOneSchema) || field.is_a?(OneToManySchema)) &&
24
- relation_field.is_a?(ManyToOneSchema) && other_inverse?(field, relation_field))
25
- )
15
+ if polymorphic_relations.include?(relation_field.type)
16
+ field.is_a?(PolymorphicManyToOneSchema) &&
17
+ field.foreign_collections.include?(collection.name)
18
+ else
19
+ field.is_a?(RelationSchema) &&
20
+ field.foreign_collection == collection.name &&
21
+ (
22
+ (field.is_a?(ManyToManySchema) && relation_field.is_a?(ManyToManySchema) &&
23
+ many_to_many_inverse?(field, relation_field)) ||
24
+ (field.is_a?(ManyToOneSchema) &&
25
+ (relation_field.is_a?(OneToOneSchema) || relation_field.is_a?(OneToManySchema)) &&
26
+ many_to_one_inverse?(field, relation_field)) ||
27
+ ((field.is_a?(OneToOneSchema) || field.is_a?(OneToManySchema)) &&
28
+ relation_field.is_a?(ManyToOneSchema) && other_inverse?(field, relation_field))
29
+ )
30
+ end
26
31
  end.keys.first
27
32
 
28
33
  inverse || nil
@@ -138,7 +143,7 @@ module ForestAdminDatasourceToolkit
138
143
  projection.nest(prefix: foreign_relation)
139
144
  )
140
145
 
141
- return records.map { |r| r.try(foreign_relation) }
146
+ return records.map { |r| r[foreign_relation] }
142
147
  end
143
148
  end
144
149
 
@@ -30,7 +30,7 @@ module ForestAdminDatasourceToolkit
30
30
 
31
31
  relation = collection.schema[:fields][relation_name]
32
32
 
33
- if relation.type != 'OneToMany' && relation.type != 'ManyToMany'
33
+ if relation.type != 'OneToMany' && relation.type != 'PolymorphicOneToMany' && relation.type != 'ManyToMany'
34
34
  raise Exceptions::ForestException,
35
35
  "Relation #{relation_name} has invalid type should be one of OneToMany or ManyToMany."
36
36
  end
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceToolkit
2
- VERSION = "1.0.0-beta.61"
2
+ VERSION = "1.0.0-beta.62"
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.61
4
+ version: 1.0.0.pre.beta.62
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-26 00:00:00.000000000 Z
12
+ date: 2024-08-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -100,6 +100,9 @@ files:
100
100
  - lib/forest_admin_datasource_toolkit/schema/relations/many_to_one_schema.rb
101
101
  - lib/forest_admin_datasource_toolkit/schema/relations/one_to_many_schema.rb
102
102
  - lib/forest_admin_datasource_toolkit/schema/relations/one_to_one_schema.rb
103
+ - lib/forest_admin_datasource_toolkit/schema/relations/polymorphic_many_to_one_schema.rb
104
+ - lib/forest_admin_datasource_toolkit/schema/relations/polymorphic_one_to_many_schema.rb
105
+ - lib/forest_admin_datasource_toolkit/schema/relations/polymorphic_one_to_one_schema.rb
103
106
  - lib/forest_admin_datasource_toolkit/utils/collection.rb
104
107
  - lib/forest_admin_datasource_toolkit/utils/hash_helper.rb
105
108
  - lib/forest_admin_datasource_toolkit/utils/record.rb