forest_admin_datasource_customizer 1.0.0.pre.beta.101 → 1.0.0.pre.beta.102

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: 51cfd9f4455056e5f53ef8e0186464cf083746e77ede366896ae5fedc86a06b6
4
- data.tar.gz: fcc9a1d4200353efdfe6576e5464c3e7d32889e6c0acbde83a007236393e2587
3
+ metadata.gz: 916589f8ad75c0b803ddd5bf005c36c44376651991114654912c54057b87098f
4
+ data.tar.gz: 76db3232dc1899a6e467d1650bdb946e409648ab6b90ca5283afb41069f041c8
5
5
  SHA512:
6
- metadata.gz: 49cd97915f4916bd2c208f657fb85f5b264bee7183fc877cc8c865a6349abcd3757d673c6f6f6acd03fa064e72bb12cb88b0f34b59f7d6eb5ac3161516e620ca
7
- data.tar.gz: 34a7a919cb6a31ad86cee5feb597b6f9a8db92dc05ead2a8b927189cf0a95e1ff439354f0adee6620f80b5bb4ceacc55c8dda37623189112f398c424f2e4751a
6
+ metadata.gz: 92c6fc56621eb603ab3fc3964944ee5e0022f992a46660e105ad6d1782059c0b64780005bbd14a33e9a77aba7ed677be8965fbd645d41222684f45b1b22d1faa
7
+ data.tar.gz: dd1928aa3c397c4ca0ee30067dabf36883ce22b0f82028ab1ae25db90f890aed3ad0ece6f0003e2a8d0afc14c7ebe959e5915a9fbc91aa55d6d7c696664241fc
@@ -45,6 +45,10 @@ module ForestAdminDatasourceCustomizer
45
45
  datasource.collections.each_value do |collection|
46
46
  @composite_datasource.add_collection(collection)
47
47
  end
48
+
49
+ datasource.schema[:charts].each do |chart|
50
+ @composite_datasource.add_chart(chart)
51
+ end
48
52
  })
49
53
 
50
54
  @datasources << datasource
@@ -88,6 +92,27 @@ module ForestAdminDatasourceCustomizer
88
92
  root_datasource
89
93
  end
90
94
 
95
+ def render_chart(caller, name)
96
+ return @composite_datasource.render_chart(caller, name) if @composite_datasource.schema[:charts].any?(name)
97
+
98
+ raise ForestAdminAgent::Http::Exceptions::NotFoundError, "Chart '#{name}' is not defined in the dataSource."
99
+ end
100
+
101
+ def reload!(logger: nil)
102
+ old_composite = @composite_datasource
103
+
104
+ begin
105
+ new_composite = ForestAdminDatasourceToolkit::Datasource.new
106
+ @stack.reload!(new_composite, logger)
107
+ @composite_datasource = new_composite
108
+ rescue StandardError => e
109
+ @composite_datasource = old_composite
110
+ raise e
111
+ end
112
+
113
+ datasource(logger)
114
+ end
115
+
91
116
  private
92
117
 
93
118
  def push_customization(&customization)
@@ -41,7 +41,7 @@ module ForestAdminDatasourceCustomizer
41
41
  used = []
42
42
  context = get_context(caller, action, form_values, filter, used, metas[:change_field])
43
43
 
44
- dynamic_fields = action.form
44
+ dynamic_fields = action.form.deep_dup
45
45
  dynamic_fields = select_in_form_fields(dynamic_fields, metas[:search_field]) if metas[:search_field]
46
46
  dynamic_fields = drop_defaults(context, dynamic_fields, form_values)
47
47
  dynamic_fields = drop_ifs(context, dynamic_fields) unless metas[:include_hidden_fields]
@@ -56,7 +56,7 @@ module ForestAdminDatasourceCustomizer
56
56
  end
57
57
 
58
58
  def refine_schema(sub_schema)
59
- sub_schema[:actions] = @actions
59
+ sub_schema[:actions] = sub_schema[:actions].merge(@actions)
60
60
 
61
61
  sub_schema
62
62
  end
@@ -2,7 +2,7 @@ module ForestAdminDatasourceCustomizer
2
2
  module Decorators
3
3
  module Action
4
4
  class BaseAction
5
- attr_reader :scope, :form, :is_generate_file, :description, :submit_button_label, :execute
5
+ attr_reader :scope, :form, :is_generate_file, :description, :submit_button_label, :execute, :static_form
6
6
 
7
7
  def initialize(scope:, form: nil, is_generate_file: false, description: nil, submit_button_label: nil, &execute)
8
8
  @scope = scope
@@ -11,16 +11,25 @@ module ForestAdminDatasourceCustomizer
11
11
  @description = description
12
12
  @submit_button_label = submit_button_label
13
13
  @execute = execute
14
+ @static_form = false
14
15
  end
15
16
 
16
- def build_elements
17
- @form = FormFactory.build_elements(form)
17
+ def self.from_plain_object(action)
18
+ new(
19
+ scope: action[:scope],
20
+ form: FormFactory.build_elements(action[:form]),
21
+ is_generate_file: action[:is_generate_file],
22
+ description: action[:description],
23
+ submit_button_label: action[:submit_button_label],
24
+ &action[:execute]
25
+ )
18
26
  end
19
27
 
20
- def static_form?
21
- return form&.all?(&:static?) if form
28
+ def build_elements
29
+ @form = FormFactory.build_elements(@form)
30
+ @static_form = @form ? @form&.all?(&:static?) : true
22
31
 
23
- true
32
+ self
24
33
  end
25
34
 
26
35
  def validate_fields_ids(form = @form, used = [])
@@ -6,7 +6,7 @@ module ForestAdminDatasourceCustomizer
6
6
  form&.map do |field|
7
7
  case field
8
8
  when Hash
9
- if field.key?(:widget)
9
+ if field.key?(:widget) && !field[:widget].nil?
10
10
  build_widget(field)
11
11
  elsif field[:type] == 'Layout'
12
12
  build_layout_element(field)
@@ -14,9 +14,11 @@ module ForestAdminDatasourceCustomizer
14
14
  DynamicField.new(**field)
15
15
  end
16
16
  when FormLayoutElement::RowElement
17
- build_elements(field.fields)
17
+ field.fields = build_elements(field.fields)
18
+ field
18
19
  when FormLayoutElement::PageElement
19
- build_elements(field.elements)
20
+ field.elements = build_elements(field.elements)
21
+ field
20
22
  else
21
23
  field
22
24
  end
@@ -9,7 +9,52 @@ module ForestAdminDatasourceCustomizer
9
9
 
10
10
  def initialize(datasource)
11
11
  @customizations = []
12
+ @applied_customizations = []
13
+ init_stack(datasource)
14
+ end
15
+
16
+ def queue_customization(customization)
17
+ @customizations << customization
18
+ end
19
+
20
+ # Apply all customizations
21
+ # Plugins may queue new customizations, or call other plugins which will queue customizations.
22
+ #
23
+ # This method will be called recursively and clears the queue at each recursion to ensure
24
+ # that all customizations are applied in the right order.
25
+ def apply_queued_customizations(logger, store_customizations: true)
26
+ queued_customizations = @customizations.clone
27
+ @customizations = []
28
+
29
+ while queued_customizations.length.positive?
30
+ customization = queued_customizations.shift
31
+ customization.call
32
+
33
+ @applied_customizations << customization if store_customizations
34
+
35
+ apply_queued_customizations(logger, store_customizations: false)
36
+ end
37
+ end
12
38
 
39
+ def reload!(datasource, logger)
40
+ backup = backup_stack
41
+ @customizations = @applied_customizations.dup
42
+ @applied_customizations = []
43
+
44
+ begin
45
+ init_stack(datasource)
46
+ apply_queued_customizations(logger)
47
+ logger.log('Debug', 'Reloading customizations')
48
+ rescue StandardError => e
49
+ logger.log('Error', "Error while reloading customizations: #{e.message}, restoring previous state")
50
+ restore_stack(backup)
51
+ raise e
52
+ end
53
+ end
54
+
55
+ private
56
+
57
+ def init_stack(datasource)
13
58
  last = datasource
14
59
  last = @override = DatasourceDecorator.new(last, Override::OverrideCollectionDecorator)
15
60
  last = DatasourceDecorator.new(last, Empty::EmptyCollectionDecorator)
@@ -42,22 +87,15 @@ module ForestAdminDatasourceCustomizer
42
87
  @datasource = last
43
88
  end
44
89
 
45
- def queue_customization(customization)
46
- @customizations << customization
90
+ def backup_stack
91
+ instance_variables.each_with_object({}) do |var, hash|
92
+ hash[var] = instance_variable_get(var)
93
+ end
47
94
  end
48
95
 
49
- # Apply all customizations
50
- # Plugins may queue new customizations, or call other plugins which will queue customizations.
51
- #
52
- # This method will be called recursively and clears the queue at each recursion to ensure
53
- # that all customizations are applied in the right order.
54
- def apply_queued_customizations(logger)
55
- queued_customizations = @customizations.clone
56
- @customizations = []
57
-
58
- while queued_customizations.length.positive?
59
- queued_customizations.shift.call
60
- apply_queued_customizations(logger)
96
+ def restore_stack(backup)
97
+ backup.each do |var, value|
98
+ instance_variable_set(var, value)
61
99
  end
62
100
  end
63
101
  end
@@ -31,9 +31,18 @@ module ForestAdminDatasourceCustomizer
31
31
  super
32
32
  end
33
33
 
34
- def rename_collections(renames = [])
35
- renames.each do |current_name, new_name|
36
- rename_collection(current_name, new_name)
34
+ def rename_collections(renames)
35
+ case renames
36
+ when Proc
37
+ collections.each_key do |name|
38
+ rename_collection(name, renames.call(name) || name)
39
+ end
40
+ when Hash
41
+ renames.each do |old_name, new_name|
42
+ rename_collection(old_name, new_name)
43
+ end
44
+ else
45
+ raise Exceptions::ForestException, 'Invalid argument for rename_collections, must be a function or a hash'
37
46
  end
38
47
  end
39
48
 
@@ -1,3 +1,3 @@
1
1
  module ForestAdminDatasourceCustomizer
2
- VERSION = "1.0.0-beta.101"
2
+ VERSION = "1.0.0-beta.102"
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.101
4
+ version: 1.0.0.pre.beta.102
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: 2025-05-21 00:00:00.000000000 Z
12
+ date: 2025-05-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport