Skip to content

Commit

Permalink
Merge branch 'main' into depfu/batch_all/2025-01-16
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob authored Jan 17, 2025
2 parents 3530c2c + 09b4270 commit ec6f579
Show file tree
Hide file tree
Showing 8 changed files with 236 additions and 117 deletions.
14 changes: 7 additions & 7 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ class ActionsController < ApplicationController
# set_record will fail if it's tried to be used from the Index page.
request.params[:id].present?
end
before_action :set_action, only: [:show, :handle]
before_action :verify_authorization, only: [:show, :handle]
before_action :set_query, :set_fields, only: :handle
before_action :set_query, :set_action, :verify_authorization, only: [:show, :handle]
before_action :set_fields, only: :handle

layout :choose_layout

Expand Down Expand Up @@ -62,7 +61,7 @@ def handle
private

def set_query
resource_ids = action_params[:fields][:avo_resource_ids].split(",")
resource_ids = action_params[:fields]&.dig(:avo_resource_ids)&.split(",") || []

@query = decrypted_query || (resource_ids.any? ? @resource.find_record(resource_ids, params: params) : [])
end
Expand All @@ -72,7 +71,7 @@ def set_fields
end

def action_params
@action_params ||= params.permit(:id, :authenticity_token, :resource_name, :action_id, :button, fields: {})
@action_params ||= params.permit(:id, :authenticity_token, :resource_name, :action_id, :button, :arguments, fields: {})
end

def set_action
Expand All @@ -82,7 +81,8 @@ def set_action
user: _current_user,
# 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)) || {}
arguments: BaseAction.decode_arguments(params[:arguments] || params.dig(:fields, :arguments)) || {},
query: @query
)

# Fetch action's fields
Expand Down Expand Up @@ -171,7 +171,7 @@ def get_messages
end

def decrypted_query
return if (encrypted_query = action_params[:fields][:avo_selected_query]).blank?
return if (encrypted_query = action_params[:fields]&.dig(:avo_selected_query)).blank?

Avo::Services::EncryptionService.decrypt(message: encrypted_query, purpose: :select_all, serializer: Marshal)
end
Expand Down
39 changes: 39 additions & 0 deletions app/javascript/js/controllers/item_select_all_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export default class extends Controller {
this.selectAllOverlay(allSelected)
this.resetUnselected()
}

this.updateLinks('resourceIds')
}

selectAll(event) {
Expand All @@ -77,6 +79,43 @@ export default class extends Controller {
this.selectedAllValue = !this.selectedAllValue
this.unselectedMessageTarget.classList.toggle('hidden')
this.selectedMessageTarget.classList.toggle('hidden')

if (this.selectedAllValue) {
this.updateLinks('selectedQuery')
} else {
this.updateLinks('resourceIds')
}
}

updateLinks(param) {
let resourceIds = ''
let selectedQuery = ''

if (param === 'resourceIds') {
resourceIds = JSON.parse(this.element.dataset.selectedResources).join(',')
} else if (param === 'selectedQuery') {
selectedQuery = this.element.dataset.itemSelectAllSelectedAllQueryValue
}

document.querySelectorAll('[data-target="actions-list"] > a').forEach((link) => {
try {
const url = new URL(link.href)

Array.from(url.searchParams.keys())
.filter((key) => key.startsWith('fields['))
.forEach((key) => url.searchParams.delete(key))

if (param === 'resourceIds') {
url.searchParams.set('fields[avo_resource_ids]', resourceIds)
} else if (param === 'selectedQuery') {
url.searchParams.set('fields[avo_selected_query]', selectedQuery)
}

link.href = url.toString()
} catch (error) {
console.error('Error updating link:', link, error)
}
})
}

resetUnselected() {
Expand Down
16 changes: 11 additions & 5 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class BaseAction
attr_reader :icon
attr_reader :appended_turbo_streams
attr_reader :records_to_reload
attr_reader :query

# TODO: find a differnet way to delegate this to the uninitialized Current variable
delegate :context, to: Avo::Current
Expand Down Expand Up @@ -96,14 +97,15 @@ def action_name
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
arguments: @arguments,
query: @query
).handle
end

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)
def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {}, icon: :play, query: nil)
@record = record
@resource = resource
@user = user
Expand All @@ -114,6 +116,7 @@ def initialize(record: nil, resource: nil, user: nil, view: nil, arguments: {},
resource: resource,
record: record
).handle.with_indifferent_access
@query = 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")
Expand All @@ -139,7 +142,8 @@ def get_message
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
arguments: @arguments,
query: @query
).handle
end

Expand All @@ -149,7 +153,8 @@ def cancel_button_label
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
arguments: @arguments,
query: @query
).handle
end

Expand All @@ -159,7 +164,8 @@ def confirm_button_label
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
arguments: @arguments,
query: @query
).handle
end

Expand Down
36 changes: 18 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@
"@stimulus-components/password-visibility": "^3.0.0",
"@tailwindcss/forms": "^0.5.10",
"@tailwindcss/typography": "^0.5.16",
"@tiptap/core": "^2.11.0",
"@tiptap/extension-bold": "^2.11.0",
"@tiptap/extension-bullet-list": "^2.11.0",
"@tiptap/extension-document": "^2.11.0",
"@tiptap/extension-hard-break": "^2.11.0",
"@tiptap/extension-italic": "^2.11.0",
"@tiptap/extension-link": "^2.11.0",
"@tiptap/extension-list-item": "^2.11.0",
"@tiptap/extension-ordered-list": "^2.11.0",
"@tiptap/extension-paragraph": "^2.11.0",
"@tiptap/extension-placeholder": "^2.11.0",
"@tiptap/extension-strike": "^2.11.0",
"@tiptap/extension-text": "^2.11.0",
"@tiptap/extension-underline": "^2.11.0",
"@tiptap/pm": "^2.11.0",
"@yaireo/tagify": "^4.32.2",
"@tiptap/core": "^2.11.2",
"@tiptap/extension-bold": "^2.11.2",
"@tiptap/extension-bullet-list": "^2.11.2",
"@tiptap/extension-document": "^2.11.2",
"@tiptap/extension-hard-break": "^2.11.2",
"@tiptap/extension-italic": "^2.11.2",
"@tiptap/extension-link": "^2.11.2",
"@tiptap/extension-list-item": "^2.11.2",
"@tiptap/extension-ordered-list": "^2.11.2",
"@tiptap/extension-paragraph": "^2.11.2",
"@tiptap/extension-placeholder": "^2.11.2",
"@tiptap/extension-strike": "^2.11.2",
"@tiptap/extension-text": "^2.11.2",
"@tiptap/extension-underline": "^2.11.2",
"@tiptap/pm": "^2.11.2",
"@yaireo/tagify": "^4.33.1",
"add": "^2.0.6",
"autoprefixer": "^10.4.20",
"chart.js": "^3.9.1",
Expand All @@ -67,7 +67,7 @@
"luxon": "^3.5.0",
"mapkick": "^0.2.6",
"mousetrap": "^1.6.5",
"postcss": "^8.4.49",
"postcss": "^8.5.1",
"postcss-flexbugs-fixes": "^5.0.2",
"postcss-import": "^15.1.0",
"postcss-loader": "^7.3.4",
Expand All @@ -88,7 +88,7 @@
},
"devDependencies": {
"@babel/core": "^7.26.0",
"@babel/eslint-parser": "^7.25.9",
"@babel/eslint-parser": "^7.26.5",
"@babel/preset-env": "^7.26.0",
"@prettier/plugin-ruby": "^2.1.0",
"esbuild-visualizer": "^0.3.1",
Expand Down
23 changes: 23 additions & 0 deletions spec/dummy/app/avo/actions/test/query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Avo::Actions::Test::Query < Avo::BaseAction
self.name = -> {
"Test query access #{query&.count}"
}
self.message = -> {
"message #{query.count} selected"
}
self.cancel_button_label = -> {
"cancel_button_label #{query.count} selected"
}
self.confirm_button_label = -> {
"confirm_button_label #{query.count} selected"
}
self.standalone = true

def fields
field :selected, default: "#{query.count} selected def fields"
end

def handle(**args)
succeed "succeed #{query.count} selected"
end
end
1 change: 1 addition & 0 deletions spec/dummy/app/avo/resources/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def actions
divider label: "Other actions"
action Avo::Actions::Sub::DummyAction
action Avo::Actions::DownloadFile, icon: "heroicons/outline/arrow-left"
action Avo::Actions::Test::Query
divider
action Avo::Actions::Test::NoConfirmationPostsRedirect
action Avo::Actions::Test::NoConfirmationRedirect
Expand Down
50 changes: 50 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,56 @@
end
end

describe "query" do
let!(:users) { create_list :user, 6 }

it "access query action show" do
visit avo.resources_users_path(per_page: 3)

open_panel_action(action_name: "Test query access ")

expect(page).to have_text("message 0 selected")
expect(page).to have_field("fields_selected", with: "0 selected def fields")
expect(page).to have_text("cancel_button_label 0 selected")
expect(page).to have_text("confirm_button_label 0 selected")
expect(page).to have_text("Test query access 0")

run_action

expect(page).to have_text("succeed 0 selected")

check_select_all
open_panel_action(action_name: "Test query access ")

expect(page).to have_text("message 3 selected")
expect(page).to have_field("fields_selected", with: "3 selected def fields")
expect(page).to have_text("cancel_button_label 3 selected")
expect(page).to have_text("confirm_button_label 3 selected")
expect(page).to have_text("Test query access 3")

run_action

expect(page).to have_text("succeed 3 selected")

check_select_all
click_on "Select all matching"

open_panel_action(action_name: "Test query access ")

user_count = User.count

expect(page).to have_text("message #{user_count} selected")
expect(page).to have_field("fields_selected", with: "#{user_count} selected def fields")
expect(page).to have_text("cancel_button_label #{user_count} selected")
expect(page).to have_text("confirm_button_label #{user_count} selected")
expect(page).to have_text("Test query access #{user_count}")

run_action

expect(page).to have_text("succeed #{user_count} selected")
end
end

describe "fields" do
context "boolean group fields" do
it "pass through fields params" do
Expand Down
Loading

0 comments on commit ec6f579

Please sign in to comment.