Skip to content

Commit

Permalink
enhancement: callable confirm and cancel labels on actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Bob committed Nov 23, 2024
1 parent c60e854 commit 2b6586d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
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

0 comments on commit 2b6586d

Please sign in to comment.