From d76033f4714277f34a9858a9482e8c55dc2ab807 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:17:48 +0300 Subject: [PATCH] fix: `set_record` when fetching `fields` on actions' `handle` (#3093) * feature: hydrate `record` when fetching `fields` on actions' `handle` * action link improvements --- app/components/avo/actions_component.rb | 34 +---------------------- app/controllers/avo/actions_controller.rb | 2 +- app/views/avo/actions/show.html.erb | 3 +- lib/avo/base_action.rb | 1 + 4 files changed, 4 insertions(+), 36 deletions(-) diff --git a/app/components/avo/actions_component.rb b/app/components/avo/actions_component.rb index 752a488c3c..20c94d4401 100644 --- a/app/components/avo/actions_component.rb +++ b/app/components/avo/actions_component.rb @@ -46,19 +46,6 @@ def filter_actions end end - # When running an action for one record we should do it on a special path. - # We do that so we get the `record` param inside the action so we can prefill fields. - def action_path(action) - return single_record_path(action) if @as_row_control - return many_records_path(action) unless @resource.has_record_id? - - if on_record_page? - single_record_path action - else - many_records_path action - end - end - # How should the action be displayed by default def is_disabled?(action) return false if action.standalone || @as_row_control @@ -76,25 +63,6 @@ def on_index_page? !on_record_page? end - def single_record_path(action) - action_url(action, @resource.record_path) - end - - def many_records_path(action) - action_url(action, @resource.records_path) - end - - def action_url(action, path) - Avo::Services::URIService.parse(path) - .append_paths("actions") - .append_query( - { - action_id: action.to_param, - arguments: Avo::BaseAction.encode_arguments(action.arguments) - }.compact - ).to_s - end - def icon(action) svg action.icon, class: "h-5 shrink-0 mr-1 inline pointer-events-none" end @@ -116,7 +84,7 @@ def render_divider(action) end def render_action_link(action) - link_to action_path(action), + link_to action.link_arguments(resource: @resource).first, data: action_data_attributes(action), title: action.action_name, class: action_css_class(action) do diff --git a/app/controllers/avo/actions_controller.rb b/app/controllers/avo/actions_controller.rb index 89e0efe394..d01acf2ded 100644 --- a/app/controllers/avo/actions_controller.rb +++ b/app/controllers/avo/actions_controller.rb @@ -4,7 +4,7 @@ module Avo class ActionsController < ApplicationController before_action :set_resource_name before_action :set_resource - before_action :set_record, only: :show, if: ->(request) do + before_action :set_record, only: [:show, :handle], if: ->(request) do # Try to se the record only if the user is on the record page. # set_record will fail if it's tried to be used from the Index page. request.params[:id].present? diff --git a/app/views/avo/actions/show.html.erb b/app/views/avo/actions/show.html.erb index a91e12147b..ad4834aa5b 100644 --- a/app/views/avo/actions/show.html.erb +++ b/app/views/avo/actions/show.html.erb @@ -11,7 +11,7 @@ class="hidden text-slate-800" > <%= form_with scope: 'fields', - url: Avo::Services::URIService.parse(@resource.records_path).append_paths("actions").to_s, + url: @action.link_arguments(resource: @resource).first, local: true, html: { novalidate: true, @@ -29,7 +29,6 @@