forest_admin_agent 1.38.0 → 1.38.1

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: 5455a6164932af84cf535d72cd896814fcb24a83106cae4c6efbf94d3f15f650
4
- data.tar.gz: f31a0a6cde3793cd7bc2f5bfc1c2462a0001d4e80e414e7433064f2907dff45d
3
+ metadata.gz: bbec2d5605e6d383151f61a3b58c602b0c65099685c03cd7e2f910146a563058
4
+ data.tar.gz: 53f010f4b995181820015c7bda91ef07a7d84bd8a7ee66fa9887f4e933bcbbbc
5
5
  SHA512:
6
- metadata.gz: 929f4669e1b73c51b615f0259a6b34e6cfa59cb89c7e9dde693c7c47238b4c5b2fe608d5a3eceb73d6ed025fe5d5a83a33759920187c61bb1eddd3b0ab239bf1
7
- data.tar.gz: 638dd7596eae9fde1b272e5389d3da042e0aaf3e12080fbdf449866a61a8535a75ae8d46cbf39c5c3eed56792f249c7146d770179a976fe0961b5e6c6b445b02
6
+ metadata.gz: a34d395d4eab6ad9a034e9ef0b93b6737740a7a832da40e10ceab61787e5e3c2949776eb1c7bbefdd91cfdc175312e0e954e090c63804403e83e151080c6e16c
7
+ data.tar.gz: 18058de101b452b870c8ef90a2be2990ba07c197b2d64347e4e229d4f37156b26eaaf9cc96ab885911f5144e06ee939e9108044b85333eb8e97a6f442e22d2f7
@@ -17,9 +17,10 @@ module ForestAdminAgent
17
17
  def handle_request(args = {})
18
18
  context = build(args)
19
19
  context.permissions.can?(:add, context.collection)
20
+ relations = authorized_linked_one_to_one_relations(args, context)
20
21
  data = format_attributes(args, context.collection)
21
22
  record = context.collection.create(context.caller, data)
22
- link_one_to_one_relations(args, record, context)
23
+ link_one_to_one_relations(relations, record, context)
23
24
  id = ForestAdminDatasourceToolkit::Utils::Record.primary_keys(context.collection, record)
24
25
  filter = ForestAdminDatasourceToolkit::Components::Query::Filter.new(
25
26
  condition_tree: ConditionTree::ConditionTreeFactory.match_ids(context.collection, [id])
@@ -37,24 +38,63 @@ module ForestAdminAgent
37
38
  }
38
39
  end
39
40
 
40
- def link_one_to_one_relations(args, record, context)
41
- args[:params][:data][:relationships]&.map do |field, value|
42
- schema = context.collection.schema[:fields][field]
43
- next unless %w[OneToOne PolymorphicOneToOne].include?(schema.type)
41
+ private
44
42
 
45
- primary_key_values = Utils::Id.unpack_id(context.collection, value['data']['id'], with_key: true)
46
- foreign_collection = context.datasource.get_collection(schema.foreign_collection)
47
- # Load the value that will be used as origin_key
48
- origin_value = record[schema.origin_key_target]
43
+ def authorized_linked_one_to_one_relations(args, context)
44
+ linked_one_to_one_relations(args, context).map do |relation|
45
+ foreign_collection = relation[:foreign_collection]
49
46
 
50
- # update new relation (may update zero or one records).
51
- patch = { schema.origin_key => origin_value }
47
+ context.permissions.can?(:edit, foreign_collection)
48
+
49
+ relation.merge(
50
+ scope: context.permissions.get_scope(foreign_collection),
51
+ primary_key_values: Utils::Id.unpack_id(foreign_collection, relation[:id], with_key: true)
52
+ )
53
+ end
54
+ end
55
+
56
+ def linked_one_to_one_relations(args, context)
57
+ relationships = args.dig(:params, :data, :relationships) || {}
58
+
59
+ relationships.filter_map { |field, value| linked_one_to_one_relation(field, value, context) }
60
+ end
61
+
62
+ def linked_one_to_one_relation(field, value, context)
63
+ schema = context.collection.schema[:fields][field]
64
+ return unless %w[OneToOne PolymorphicOneToOne].include?(schema.type)
65
+
66
+ id = value.dig('data', 'id')
67
+ return if id.nil?
68
+
69
+ {
70
+ schema: schema,
71
+ foreign_collection: context.datasource.get_collection(schema.foreign_collection),
72
+ id: id
73
+ }
74
+ end
75
+
76
+ def link_one_to_one_relations(relations, record, context)
77
+ relations.each do |relation|
78
+ schema = relation[:schema]
79
+ foreign_collection = relation[:foreign_collection]
80
+
81
+ patch = { schema.origin_key => record[schema.origin_key_target] }
52
82
  if schema.type == 'PolymorphicOneToOne'
53
83
  patch[schema.origin_type_field] =
54
84
  context.collection.name.gsub('__', '::')
55
85
  end
56
- condition_tree = ConditionTree::ConditionTreeFactory.match_records(foreign_collection, [primary_key_values])
57
- filter = Filter.new(condition_tree: condition_tree)
86
+
87
+ new_fk_owner = ConditionTree::ConditionTreeFactory.match_records(
88
+ foreign_collection, [relation[:primary_key_values]]
89
+ )
90
+ filter = Filter.new(
91
+ condition_tree: ConditionTree::ConditionTreeFactory.intersect(
92
+ [
93
+ relation[:scope],
94
+ new_fk_owner
95
+ ]
96
+ )
97
+ )
58
98
  foreign_collection.update(context.caller, filter, patch)
59
99
  end
60
100
  end
@@ -6,7 +6,7 @@ module ForestAdminAgent
6
6
  module Schema
7
7
  class SchemaEmitter
8
8
  LIANA_NAME = "agent-ruby"
9
- LIANA_VERSION = "1.38.0"
9
+ LIANA_VERSION = "1.38.1"
10
10
 
11
11
  def self.generate(datasource)
12
12
  datasource.collections
@@ -1,3 +1,3 @@
1
1
  module ForestAdminAgent
2
- VERSION = "1.38.0"
2
+ VERSION = "1.38.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_admin_agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.38.0
4
+ version: 1.38.1
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: 2026-08-12 00:00:00.000000000 Z
12
+ date: 2026-08-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport