Skip to content

Commit 362689c

Browse files
authored
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
1 parent a766d6b commit 362689c

File tree

6 files changed

+39
-17
lines changed

6 files changed

+39
-17
lines changed

Diff for: app/controllers/avo/actions_controller.rb

+21-8
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,25 @@ def handle
6161
private
6262

6363
def set_query
64-
resource_ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []
64+
# If the user selected all records, use the decrypted index query
65+
# Otherwise, find the records from the resource ids
66+
@query = if action_params[:fields]&.dig(:avo_selected_all) == "true"
67+
decrypted_index_query
68+
else
69+
find_records_from_resource_ids
70+
end
71+
end
6572

66-
@query = decrypted_query || (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : [])
73+
def find_records_from_resource_ids
74+
if (ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []).any?
75+
@resource.find_record(ids, params: params)
76+
else
77+
[]
78+
end
6779
end
6880

6981
def set_fields
70-
@fields = action_params[:fields].except(:avo_resource_ids, :avo_selected_query)
82+
@fields = action_params[:fields].except(:avo_resource_ids, :avo_index_query)
7183
end
7284

7385
def action_params
@@ -82,7 +94,8 @@ def set_action
8294
# force the action view to in order to render new-related fields (hidden field)
8395
view: Avo::ViewInquirer.new(:new),
8496
arguments: BaseAction.decode_arguments(params[:arguments] || params.dig(:fields, :arguments)) || {},
85-
query: @query
97+
query: @query,
98+
index_query: decrypted_index_query
8699
)
87100

88101
# Fetch action's fields
@@ -170,10 +183,10 @@ def get_messages
170183
end
171184
end
172185

173-
def decrypted_query
174-
return if (encrypted_query = action_params[:fields]&.dig(:avo_selected_query)).blank?
175-
176-
Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal)
186+
def decrypted_index_query
187+
@decrypted_index_query ||= if (encrypted_query = action_params[:fields]&.dig(:avo_index_query)).present?
188+
Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal)
189+
end
177190
end
178191

179192
def flash_messages

Diff for: app/javascript/js/controllers/action_controller.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Controller } from '@hotwired/stimulus'
22

33
export default class extends Controller {
4-
static targets = ['resourceIds', 'form', 'selectedAllQuery']
4+
static targets = ['resourceIds', 'form', 'selectedAll', 'indexQuery']
55

66
static values = {
77
noConfirmation: Boolean,
@@ -13,9 +13,12 @@ export default class extends Controller {
1313
this.resourceIdsTarget.value = this.resourceIds
1414
}
1515

16-
// This value is picked up from the DOM so we check true/false as strings
17-
if (this.selectionOptions.itemSelectAllSelectedAllValue === 'true') {
18-
this.selectedAllQueryTarget.value = this.selectionOptions.itemSelectAllSelectedAllQueryValue
16+
// Select all checkbox
17+
this.selectedAllTarget.value = this.selectionOptions.itemSelectAllSelectedAllValue
18+
19+
// Encrypted and encoded index query when it is present (index view)
20+
if (this.selectionOptions.itemSelectAllSelectedAllQueryValue) {
21+
this.indexQueryTarget.value = this.selectionOptions.itemSelectAllSelectedAllQueryValue
1922
}
2023

2124
if (this.noConfirmationValue) {

Diff for: app/javascript/js/controllers/item_select_all_controller.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ export default class extends Controller {
107107

108108
if (param === 'resourceIds') {
109109
url.searchParams.set('fields[avo_resource_ids]', resourceIds)
110+
url.searchParams.set('fields[avo_selected_all]', 'false')
110111
} else if (param === 'selectedQuery') {
111-
url.searchParams.set('fields[avo_selected_query]', selectedQuery)
112+
url.searchParams.set('fields[avo_index_query]', selectedQuery)
113+
url.searchParams.set('fields[avo_selected_all]', 'true')
112114
}
113115

114116
link.href = url.toString()

Diff for: app/views/avo/actions/show.html.erb

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
<%= @action.get_message %>
3131
</div>
3232
<%= form.hidden_field :avo_resource_ids, value: params[:id] || params[:resource_ids], 'data-action-target': 'resourceIds' %>
33-
<%= form.hidden_field :avo_selected_query, 'data-action-target': 'selectedAllQuery' %>
33+
<%= form.hidden_field :avo_selected_all, 'data-action-target': 'selectedAll' %>
34+
<%= form.hidden_field :avo_index_query, 'data-action-target': 'indexQuery' %>
3435
<%= form.hidden_field :arguments, value: params[:arguments] %>
3536
<% if @fields.present? %>
3637
<div class="mt-4 -mx-6">

Diff for: lib/avo/base_action.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def action_name
115115
self.class.to_s.demodulize.underscore.humanize(keep_id_suffix: true)
116116
end
117117

118-
def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play, query: nil)
118+
def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play, query: nil, index_query: nil)
119119
@record = record
120120
@resource = resource
121121
@user = user
@@ -127,7 +127,7 @@ def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {},
127127
record: record
128128
).handle.with_indifferent_access
129129
@query = query
130-
130+
@index_query = index_query
131131
self.class.message ||= I18n.t("avo.are_you_sure_you_want_to_run_this_option")
132132
self.class.confirm_button_label ||= I18n.t("avo.run")
133133
self.class.cancel_button_label ||= I18n.t("avo.cancel")

Diff for: spec/dummy/app/avo/actions/export_csv.rb

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ def handle(**args)
1818
# uncomment if you want to download all the records if none was selected
1919
# records = resource.model_class.all if records.blank?
2020

21-
return error "No record selected" if records.blank?
21+
if records.blank?
22+
inform "@index_query.count: #{@index_query.count}"
23+
return error "No record selected"
24+
end
2225

2326
# uncomment to get all the models' attributes.
2427
# attributes = get_attributes_from_record records.first

0 commit comments

Comments
 (0)