Skip to content

Commit

Permalink
fix: Rename Turbo Stream helpers to avoid clashes in user apps (#3462) (
Browse files Browse the repository at this point in the history
  • Loading branch information
dark-panda authored Dec 2, 2024
1 parent 126c7d6 commit 75b7cd2
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 12 deletions.
12 changes: 6 additions & 6 deletions app/controllers/avo/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,13 @@ def respond
turbo_response = case @response[:type]
when :keep_modal_open
# Only render the flash messages if the action keeps the modal open
turbo_stream.flash_alerts
turbo_stream.avo_flash_alerts
when :download
# Trigger download, removes modal and flash the messages
[
turbo_stream.download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.close_modal,
turbo_stream.flash_alerts
turbo_stream.avo_download(content: Base64.encode64(@response[:path]), filename: @response[:filename]),
turbo_stream.avo_close_modal,
turbo_stream.avo_flash_alerts
]
when :navigate_to_action
src, _ = @response[:action].link_arguments(resource: @action.resource, **@response[:navigate_to_action_args])
Expand All @@ -125,8 +125,8 @@ def respond
when :close_modal
# Close the modal and flash the messages
[
turbo_stream.close_modal,
turbo_stream.flash_alerts
turbo_stream.avo_close_modal,
turbo_stream.avo_flash_alerts
]
else
# Reload the page
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/avo/associations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def reload_frame_turbo_streams
turbo_streams = super

# We want to close the modal if the user wants to add just one record
turbo_streams << turbo_stream.close_modal if params[:button] != "attach_another"
turbo_streams << turbo_stream.avo_close_modal if params[:button] != "attach_another"

turbo_streams
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/avo/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ def destroy_fail_action
flash[:error] = destroy_fail_message

respond_to do |format|
format.turbo_stream { render turbo_stream: turbo_stream.flash_alerts }
format.turbo_stream { render turbo_stream: turbo_stream.avo_flash_alerts }
end
end

Expand Down Expand Up @@ -660,7 +660,7 @@ def sanitized_sort_direction
def reload_frame_turbo_streams
[
turbo_stream.turbo_frame_reload(params[:turbo_frame]),
turbo_stream.flash_alerts
turbo_stream.avo_flash_alerts
]
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/helpers/avo/turbo_stream_actions_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module Avo
module TurboStreamActionsHelper
def download(content:, filename:)
def avo_download(content:, filename:)
turbo_stream_action_tag :download, content: content, filename: filename
end

def flash_alerts
def avo_flash_alerts
turbo_stream_action_tag :append,
target: "alerts",
template: @view_context.render(Avo::FlashAlertsComponent.new(flashes: @view_context.flash.discard))
end

def close_modal
def avo_close_modal
turbo_stream_action_tag :replace,
target: Avo::MODAL_FRAME_ID,
template: @view_context.turbo_frame_tag(Avo::MODAL_FRAME_ID, data: {turbo_temporary: 1})
Expand Down
48 changes: 48 additions & 0 deletions spec/helpers/avo/turbo_stream_actions_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "rails_helper"

RSpec.describe Avo::TurboStreamActionsHelper, type: :helper do
let(:turbo_stream) do
Turbo::Streams::TagBuilder.new(self)
end

before do
@view_context = helper
end

describe "#avo_download" do
subject do
helper.avo_download(content:, filename:)
end

let(:content) { "file content" }
let(:filename) { "file.txt" }

it { is_expected.to have_css("turbo-stream[action=\"download\"]") }
it { is_expected.to have_css("turbo-stream[content=\"file content\"]") }
it { is_expected.to have_css("turbo-stream[filename=\"file.txt\"]") }
end

describe "#avo_flash_alerts" do
subject do
helper.avo_flash_alerts
end

before do
allow(helper).to receive(:render).and_return("flash alerts")
allow(helper).to receive(:flash).and_return(double(discard: {}))
end

it { is_expected.to have_css("turbo-stream[action=\"append\"]") }
it { is_expected.to have_css("turbo-stream[target=\"alerts\"]") }
it { is_expected.to include("<template>flash alerts</template>") }
end

describe "#avo_close_modal" do
subject do
helper.avo_close_modal
end

it { is_expected.to have_css("turbo-stream[action=\"replace\"]") }
it { is_expected.to have_css("turbo-stream[target=\"#{Avo::MODAL_FRAME_ID}\"]") }
end
end

0 comments on commit 75b7cd2

Please sign in to comment.