Skip to content

Commit

Permalink
feature: allow actions to render turbo streams (#2796)
Browse files Browse the repository at this point in the history
* feature: allow actions to render turbo streams

* lint

* fix & test

* rename & drop turbo_stream keyword

* lint

* fix tests

* lint
  • Loading branch information
Paul-Bob authored Jul 12, 2024
1 parent 99b9020 commit b396c12
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
22 changes: 15 additions & 7 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ def respond

respond_to do |format|
format.turbo_stream do
case @response[:type]
turbo_response = case @response[:type]
when :keep_modal_open
# Only render the flash messages if the action keeps the modal open
render turbo_stream: turbo_stream.flash_alerts
turbo_stream.flash_alerts
when :download
# Trigger download, removes modal and flash the messages
render turbo_stream: [
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_action_modal,
turbo_stream.flash_alerts
Expand All @@ -104,25 +104,33 @@ def respond
frame_id = Avo::ACTIONS_TURBO_FRAME_ID
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])

render turbo_stream: turbo_stream.turbo_frame_set_src(frame_id, src)
turbo_stream.turbo_frame_set_src(frame_id, src)
when :redirect
render turbo_stream: turbo_stream.redirect_to(
turbo_stream.redirect_to(
Avo::ExecutionContext.new(target: @response[:path]).handle,
turbo_frame: @response[:redirect_args][:turbo_frame],
**@response[:redirect_args].except(:turbo_frame)
)
when :close_modal
# Close the modal and flash the messages
render turbo_stream: [
[
turbo_stream.close_action_modal,
turbo_stream.flash_alerts
]
else
# Reload the page
back_path = request.referer || params[:referrer].presence || resources_path(resource: @resource)

render turbo_stream: turbo_stream.redirect_to(back_path)
turbo_stream.redirect_to(back_path)
end

responses = if @action.appended_turbo_streams.present?
Array(turbo_response) + Array(instance_exec(&@action.appended_turbo_streams))
else
Array(turbo_response)
end

render turbo_stream: responses
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions lib/avo/base_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class BaseAction
attr_accessor :user
attr_reader :arguments
attr_reader :icon
attr_reader :appended_turbo_streams

# TODO: find a differnet way to delegate this to the uninitialized Current variable
delegate :context, to: Avo::Current
Expand Down Expand Up @@ -291,6 +292,10 @@ def authorized?
).handle
end

def append_to_response(turbo_stream)
@appended_turbo_streams = turbo_stream
end

private

def add_message(body, type = :info)
Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/avo/actions/test/close_modal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ def handle(**args)
TestBuddy.hi "Hello from Avo::Actions::Test::CloseModal handle method"
succeed "Modal closed!!"
close_modal

append_to_response -> {
turbo_stream.set_title("Cool title")
}
end
end
4 changes: 3 additions & 1 deletion spec/system/avo/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@

fill_in "user_first_name", with: "First name should persist after action."

expect(page).to have_title("Create new user — Avocadelicious")

click_on "Actions"
click_on "Close modal"
Expand All @@ -218,7 +219,8 @@
click_on "Run"
expect(page).not_to have_selector(modal)
expect(page).to have_text "Modal closed!!"
expect(page).to have_field('user_first_name', with: 'First name should persist after action.')
expect(page).to have_title("Cool title")
expect(page).to have_field("user_first_name", with: "First name should persist after action.")
end
end

Expand Down

0 comments on commit b396c12

Please sign in to comment.