Class: ActiveadminMcp::RecordUpdater
- Inherits:
-
Object
- Object
- ActiveadminMcp::RecordUpdater
- Defined in:
- lib/activeadmin_mcp/record_updater.rb
Overview
Updates a single ActiveAdmin-managed record, enforcing the same three gates the admin UI would: the resource must expose the update action, the current user must be authorized, and only fields the admin form permits are written.
Defined Under Namespace
Classes: PermitError
Constant Summary collapse
- UPDATE =
:update
Instance Method Summary collapse
- #call(id:, attributes:) ⇒ Object
-
#initialize(resource:, current_user:) ⇒ RecordUpdater
constructor
A new instance of RecordUpdater.
Constructor Details
#initialize(resource:, current_user:) ⇒ RecordUpdater
Returns a new instance of RecordUpdater.
13 14 15 16 |
# File 'lib/activeadmin_mcp/record_updater.rb', line 13 def initialize(resource:, current_user:) @resource = resource @current_user = current_user end |
Instance Method Details
#call(id:, attributes:) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/activeadmin_mcp/record_updater.rb', line 18 def call(id:, attributes:) config = @resource[:config] return error("Resource is not editable: #{@resource[:name]}") unless editable?(config) record = @resource[:model].find_by(id: id) return error("Record not found: #{@resource[:name]}##{id}") unless record unless (config, record) return error("Not authorized to update #{@resource[:name]}##{id}") end begin permitted = permitted_attributes(config, attributes) rescue PermitError => e return error(e.) end return error("No permitted attributes to update") if permitted.empty? if record.update(permitted) { resource: @resource[:name], id: record.id, updated: permitted.keys, record: record.as_json, } else error("Validation failed", details: record.errors.) end end |