From 362689ce962b847a6f502ef74b5d3aee12477476 Mon Sep 17 00:00:00 2001 From: Paul Bob <69730720+Paul-Bob@users.noreply.github.com> Date: Tue, 4 Mar 2025 09:22:53 +0200 Subject: [PATCH] enhancement: add `@index_query` access in actions (#3699) * enhancement: add `index_query` access in actions * lint * lint * add comment * index_query only when present * Refactor resource selection parameters for actions --- app/controllers/avo/actions_controller.rb | 29 ++++++++++++++----- .../js/controllers/action_controller.js | 11 ++++--- .../controllers/item_select_all_controller.js | 4 ++- app/views/avo/actions/show.html.erb | 3 +- lib/avo/base_action.rb | 4 +-- spec/dummy/app/avo/actions/export_csv.rb | 5 +++- 6 files changed, 39 insertions(+), 17 deletions(-) diff --git a/app/controllers/avo/actions_controller.rb b/app/controllers/avo/actions_controller.rb index eb0cd67e44..ddf29022c9 100644 --- a/app/controllers/avo/actions_controller.rb +++ b/app/controllers/avo/actions_controller.rb @@ -61,13 +61,25 @@ def handle private def set_query - resource_ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || [] + # If the user selected all records, use the decrypted index query + # Otherwise, find the records from the resource ids + @query = if action_params[:fields]&.dig(:avo_selected_all) == "true" + decrypted_index_query + else + find_records_from_resource_ids + end + end - @query = decrypted_query || (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : []) + def find_records_from_resource_ids + if (ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []).any? + @resource.find_record(ids, params: params) + else + [] + end end def set_fields - @fields = action_params[:fields].except(:avo_resource_ids, :avo_selected_query) + @fields = action_params[:fields].except(:avo_resource_ids, :avo_index_query) end def action_params @@ -82,7 +94,8 @@ def set_action # force the action view to in order to render new-related fields (hidden field) view: Avo::ViewInquirer.new(:new), arguments: BaseAction.decode_arguments(params[:arguments] || params.dig(:fields, :arguments)) || {}, - query: @query + query: @query, + index_query: decrypted_index_query ) # Fetch action's fields @@ -170,10 +183,10 @@ def get_messages end end - def decrypted_query - return if (encrypted_query = action_params[:fields]&.dig(:avo_selected_query)).blank? - - Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal) + def decrypted_index_query + @decrypted_index_query ||= if (encrypted_query = action_params[:fields]&.dig(:avo_index_query)).present? + Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal) + end end def flash_messages diff --git a/app/javascript/js/controllers/action_controller.js b/app/javascript/js/controllers/action_controller.js index 04a9a4f521..3e527bee9d 100644 --- a/app/javascript/js/controllers/action_controller.js +++ b/app/javascript/js/controllers/action_controller.js @@ -1,7 +1,7 @@ import { Controller } from '@hotwired/stimulus' export default class extends Controller { - static targets = ['resourceIds', 'form', 'selectedAllQuery'] + static targets = ['resourceIds', 'form', 'selectedAll', 'indexQuery'] static values = { noConfirmation: Boolean, @@ -13,9 +13,12 @@ export default class extends Controller { this.resourceIdsTarget.value = this.resourceIds } - // This value is picked up from the DOM so we check true/false as strings - if (this.selectionOptions.itemSelectAllSelectedAllValue === 'true') { - this.selectedAllQueryTarget.value = this.selectionOptions.itemSelectAllSelectedAllQueryValue + // Select all checkbox + this.selectedAllTarget.value = this.selectionOptions.itemSelectAllSelectedAllValue + + // Encrypted and encoded index query when it is present (index view) + if (this.selectionOptions.itemSelectAllSelectedAllQueryValue) { + this.indexQueryTarget.value = this.selectionOptions.itemSelectAllSelectedAllQueryValue } if (this.noConfirmationValue) { diff --git a/app/javascript/js/controllers/item_select_all_controller.js b/app/javascript/js/controllers/item_select_all_controller.js index 32164bfacc..56583daf7c 100644 --- a/app/javascript/js/controllers/item_select_all_controller.js +++ b/app/javascript/js/controllers/item_select_all_controller.js @@ -107,8 +107,10 @@ export default class extends Controller { if (param === 'resourceIds') { url.searchParams.set('fields[avo_resource_ids]', resourceIds) + url.searchParams.set('fields[avo_selected_all]', 'false') } else if (param === 'selectedQuery') { - url.searchParams.set('fields[avo_selected_query]', selectedQuery) + url.searchParams.set('fields[avo_index_query]', selectedQuery) + url.searchParams.set('fields[avo_selected_all]', 'true') } link.href = url.toString() diff --git a/app/views/avo/actions/show.html.erb b/app/views/avo/actions/show.html.erb index d8ba706b7c..bb96d565dc 100644 --- a/app/views/avo/actions/show.html.erb +++ b/app/views/avo/actions/show.html.erb @@ -30,7 +30,8 @@ <%= @action.get_message %> <%= 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 :avo_selected_all, 'data-action-target': 'selectedAll' %> + <%= form.hidden_field :avo_index_query, 'data-action-target': 'indexQuery' %> <%= form.hidden_field :arguments, value: params[:arguments] %> <% if @fields.present? %>
diff --git a/lib/avo/base_action.rb b/lib/avo/base_action.rb index c55dd0515c..8fa912fa0c 100644 --- a/lib/avo/base_action.rb +++ b/lib/avo/base_action.rb @@ -115,7 +115,7 @@ def action_name self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true) end - def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play, query: nil) + def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play, query: nil, index_query: nil) @record = record @resource = resource @user = user @@ -127,7 +127,7 @@ def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, record: record ).handle.with_indifferent_access @query = query - + @index_query = index_query self.class.message ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option") self.class.confirm_button_label ||= I18n.t("avo.run") self.class.cancel_button_label ||= I18n.t("avo.cancel") diff --git a/spec/dummy/app/avo/actions/export_csv.rb b/spec/dummy/app/avo/actions/export_csv.rb index 9f00c17364..42f4127cd4 100644 --- a/spec/dummy/app/avo/actions/export_csv.rb +++ b/spec/dummy/app/avo/actions/export_csv.rb @@ -18,7 +18,10 @@ def handle(**args) # uncomment if you want to download all the records if none was selected # records = resource.model_class.all if records.blank? - return error "No record selected" if records.blank? + if records.blank? + inform "@index_query.count: #{@index_query.count}" + return error "No record selected" + end # uncomment to get all the models' attributes. # attributes = get_attributes_from_record records.first