Skip to content

Commit

Permalink
enhancement: add @index_query access in actions (#3699)
Browse files Browse the repository at this point in the history
* enhancement: add `index_query` access in actions

* lint

* lint

* add comment

* index_query only when present

* Refactor resource selection parameters for actions
  • Loading branch information
Paul-Bob authored Mar 4, 2025
1 parent a766d6b commit 362689c
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 17 deletions.
29 changes: 21 additions & 8 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions app/javascript/js/controllers/action_controller.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion app/javascript/js/controllers/item_select_all_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion app/views/avo/actions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
<%= @action.get_message %>
</div>
<%= 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? %>
<div class="mt-4 -mx-6">
Expand Down
4 changes: 2 additions & 2 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion spec/dummy/app/avo/actions/export_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 362689c

Please sign in to comment.