Skip to content

Commit

Permalink
fix: set_record when fetching fields on actions' handle (#3093)
Browse files Browse the repository at this point in the history
* feature: hydrate `record` when fetching `fields` on actions' `handle`

* action link improvements
  • Loading branch information
Paul-Bob authored Aug 5, 2024
1 parent 9d5864f commit d76033f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 36 deletions.
34 changes: 1 addition & 33 deletions app/components/avo/actions_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
3 changes: 1 addition & 2 deletions app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,7 +29,6 @@
<div class="flex-1 flex">
<%= @action.get_message %>
</div>
<%= hidden_field_tag :action_id, @action.to_param %>
<%= form.hidden_field :avo_resource_ids, value: params[:id] || params[:resource_ids], 'data-action-target': 'resourceIds' %>
<%= form.hidden_field :avo_selected_query, 'data-action-target': 'selectedAllQuery' %>
<%= form.hidden_field :arguments, value: params[:arguments] %>
Expand Down
1 change: 1 addition & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def current_user
delegate :avo, to: :view_context
delegate :main_app, to: :view_context
delegate :to_param, to: :class
delegate :link_arguments, to: :class

class << self
delegate :context, to: ::Avo::Current
Expand Down

0 comments on commit d76033f

Please sign in to comment.