activeadmin_mcp 0.0.1 → 0.0.2

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: 99312ac0bef360e80a8d058b79adf5f0af182d3bedc19fdfbf4caa69964f7e44
4
- data.tar.gz: bba982b59472e271c18214bc4674e60a3ae96729b78172163d54eed756b3997c
3
+ metadata.gz: e336c562299eb5de7192243106f42dc18602589811fff5eb0282f12b605ea9b8
4
+ data.tar.gz: 006f6aac09bceb97605517a9b1b7346a4aef6fc2ddc4c59ab48b36b658825078
5
5
  SHA512:
6
- metadata.gz: 861ea8e3d8eeff901f12f4fa99385db60fd0854a0eae22462cf314cce67820632033b8e55ebc13252bcaa3389f0b6aceda4cc712d5bceb4ba0c342ac10a5815d
7
- data.tar.gz: 035b92456d5b3ca111c5e4715c8f495f52531ab053eb7974f1c1dda12de73d87f08ba34b40f226723d8a207bc98a89c929c4d6bb8ecdc9ee0873fdc6fbefb275
6
+ metadata.gz: 602940dbd9eb2a0337220672258f343aff84d0851eaec4b0199b38cb99f843adca48ee085ab201ed896ec29f260fe5e8ae6a8844b567990611e4c866d3abd05f
7
+ data.tar.gz: 54e2309b725add92b02af4ebbaefdafd5cfa8c349890211c22f0bb391f00863764e89cf50f7e5f6ace5f293962f473ad75f5197f7813702b939cfc2dc95d5b9d
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveadminMcp
4
+ # Records the field names declared by an ActiveAdmin `form do ... end` block.
5
+ #
6
+ # ActiveAdmin form blocks are arbitrary Formtastic DSL — `input`, `inputs`,
7
+ # `actions`, helper calls, conditionals — so the block is run against this
8
+ # stand-in form builder. Every `input :field` records `:field`; every other
9
+ # message (including unknown helpers) is swallowed and returns self, so the
10
+ # block executes without a real view context. Nested `has_many` associations
11
+ # are intentionally not descended into: the updater only writes flat
12
+ # attributes, and descending would record association fields as top-level.
13
+ class FormFieldCollector
14
+ attr_reader :fields
15
+
16
+ def initialize
17
+ @fields = []
18
+ end
19
+
20
+ def collect(&block)
21
+ instance_exec(self, &block)
22
+ @fields.uniq
23
+ end
24
+
25
+ def input(name, *_args, **_opts, &_block)
26
+ @fields << name.to_sym if name.respond_to?(:to_sym)
27
+ self
28
+ end
29
+
30
+ def inputs(*_args, **_opts, &block)
31
+ instance_exec(self, &block) if block
32
+ self
33
+ end
34
+
35
+ def has_many(*_args, **_opts)
36
+ self
37
+ end
38
+
39
+ def method_missing(_name, *_args, **_opts, &block)
40
+ instance_exec(self, &block) if block
41
+ self
42
+ end
43
+
44
+ def respond_to_missing?(_name, _include_private = false)
45
+ true
46
+ end
47
+ end
48
+ end
@@ -58,24 +58,47 @@ module ActiveadminMcp
58
58
  adapter_class.new(config, @current_user).authorized?(UPDATE, record)
59
59
  end
60
60
 
61
- # Runs the incoming attributes through the resource controller's own
62
- # permitted_params (compiled from `permit_params`), so we accept exactly what
63
- # the admin form accepts. Fails closed if that cannot be resolved.
61
+ # Resolves the fields we may write, accepting exactly what the admin form
62
+ # accepts. Prefers the resource's own `permit_params` (via the controller's
63
+ # compiled permitted_params); when a resource declares its writable fields
64
+ # through a `form do ... end` block instead — as ActiveAdmin's default
65
+ # permitted_params then returns nil — derives them from the form inputs.
66
+ # Fails closed if neither can be resolved.
64
67
  def permitted_attributes(config, attributes)
68
+ from_permit_params(config, attributes) ||
69
+ from_form(config, attributes) ||
70
+ raise(PermitError, "Could not determine permitted attributes: #{@resource[:name]}")
71
+ end
72
+
73
+ def from_permit_params(config, attributes)
65
74
  param_key = config.param_key.to_sym
66
75
  controller = config.controller.new
67
-
68
- unless controller.respond_to?(:permitted_params, true)
69
- raise PermitError, "Resource does not declare permit_params: #{@resource[:name]}"
70
- end
76
+ return nil unless controller.respond_to?(:permitted_params, true)
71
77
 
72
78
  controller.params = ActionController::Parameters.new(param_key => attributes)
73
- permitted = controller.send(:permitted_params)[param_key]
74
- permitted ? permitted.to_h.symbolize_keys : {}
75
- rescue PermitError
76
- raise
77
- rescue StandardError => e
78
- raise PermitError, "Could not determine permitted attributes: #{e.message}"
79
+ permitted = controller.send(:permitted_params)
80
+ scoped = permitted && permitted[param_key]
81
+ scoped ? scoped.to_h.symbolize_keys : nil
82
+ rescue StandardError
83
+ nil
84
+ end
85
+
86
+ def from_form(config, attributes)
87
+ fields = form_fields(config)
88
+ return nil if fields.empty?
89
+
90
+ ActionController::Parameters.new(attributes).permit(*fields).to_h.symbolize_keys
91
+ rescue StandardError
92
+ nil
93
+ end
94
+
95
+ def form_fields(config)
96
+ return [] unless config.respond_to?(:page_presenters)
97
+
98
+ block = config.page_presenters[:form]&.block
99
+ return [] unless block
100
+
101
+ FormFieldCollector.new.collect(&block)
79
102
  end
80
103
 
81
104
  def error(message, details: nil)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveadminMcp
4
- VERSION = "0.0.1"
4
+ VERSION = "0.0.2"
5
5
  end
@@ -3,6 +3,7 @@
3
3
  require_relative "activeadmin_mcp/version"
4
4
  require_relative "activeadmin_mcp/configuration"
5
5
  require_relative "activeadmin_mcp/resource_registry"
6
+ require_relative "activeadmin_mcp/form_field_collector"
6
7
  require_relative "activeadmin_mcp/record_updater"
7
8
  require_relative "activeadmin_mcp/request_handler"
8
9
  require_relative "activeadmin_mcp/engine"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_mcp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - harunkumars
@@ -97,6 +97,7 @@ files:
97
97
  - lib/activeadmin_mcp.rb
98
98
  - lib/activeadmin_mcp/configuration.rb
99
99
  - lib/activeadmin_mcp/engine.rb
100
+ - lib/activeadmin_mcp/form_field_collector.rb
100
101
  - lib/activeadmin_mcp/record_updater.rb
101
102
  - lib/activeadmin_mcp/request_handler.rb
102
103
  - lib/activeadmin_mcp/resource_registry.rb