diff --git a/lib/avo/base_action.rb b/lib/avo/base_action.rb index e6c3cf2b02..53641a6b8b 100644 --- a/lib/avo/base_action.rb +++ b/lib/avo/base_action.rb @@ -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. diff --git a/spec/dummy/app/avo/actions/sub/dummy_action.rb b/spec/dummy/app/avo/actions/sub/dummy_action.rb index 1872cb04b3..c65de2b814 100644 --- a/spec/dummy/app/avo/actions/sub/dummy_action.rb +++ b/spec/dummy/app/avo/actions/sub/dummy_action.rb @@ -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 diff --git a/spec/system/avo/actions_spec.rb b/spec/system/avo/actions_spec.rb index da86f312da..3f5e75ae3a 100644 --- a/spec/system/avo/actions_spec.rb +++ b/spec/system/avo/actions_spec.rb @@ -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 }