Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: callable confirm and cancel labels on actions #3458

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ def get_message
).handle
end

def cancel_button_label
Avo::ExecutionContext.new(
target: self.class.cancel_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end

def confirm_button_label
Avo::ExecutionContext.new(
target: self.class.confirm_button_label,
resource: @resource,
record: @record,
view: @view,
arguments: @arguments
).handle
end

def handle_action(**args)
processed_fields = if args[:fields].present?
# Fetching the field definitions and not the actual fields (get_fields) because they will break if the user uses a `visible` block and adds a condition using the `params` variable. The params are different in the show method and the handle method.
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/avo/actions/sub/dummy_action.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class Avo::Actions::Sub::DummyAction < Avo::BaseAction
self.name = -> { "Dummy action#{arguments[:test_action_name]}" }
self.cancel_button_label = -> { arguments[:cancel_button_label] || I18n.t("avo.cancel") }
self.confirm_button_label = -> { arguments[:confirm_button_label] || I18n.t("avo.run") }
self.standalone = true
# self.turbo = false
self.visible = -> do
Expand Down
14 changes: 14 additions & 0 deletions spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,20 @@
end
end

describe "callable labels" do
it "pick label from arguments on run and cancel" do
encoded_arguments = Avo::BaseAction.encode_arguments({
cancel_button_label: "Cancel dummy action",
confirm_button_label: "Confirm dummy action"
})

visit "#{avo.resources_users_path}/actions?action_id=Avo::Actions::Sub::DummyAction&arguments=#{encoded_arguments}"

expect(page).to have_text "Cancel dummy action"
expect(page).to have_text "Confirm dummy action"
end
end

# let!(:roles) { { admin: false, manager: false, writer: false } }
# let!(:user) { create :user, active: true, roles: roles }

Expand Down
Loading