From 96e5ce157509904a567500e94f08370c79fe78ed Mon Sep 17 00:00:00 2001 From: J Smith Date: Wed, 27 Nov 2024 01:09:39 -0400 Subject: [PATCH] fix: Rename Turbo Stream helpers to avoid clashes in user apps (#3462) --- app/controllers/avo/actions_controller.rb | 12 ++--- .../avo/associations_controller.rb | 2 +- app/controllers/avo/base_controller.rb | 4 +- .../avo/turbo_stream_actions_helper.rb | 6 +-- .../avo/turbo_stream_actions_helper_spec.rb | 48 +++++++++++++++++++ 5 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 spec/helpers/avo/turbo_stream_actions_helper_spec.rb diff --git a/app/controllers/avo/actions_controller.rb b/app/controllers/avo/actions_controller.rb index 3aa65667bd..a8ebd870c9 100644 --- a/app/controllers/avo/actions_controller.rb +++ b/app/controllers/avo/actions_controller.rb @@ -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]) @@ -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 diff --git a/app/controllers/avo/associations_controller.rb b/app/controllers/avo/associations_controller.rb index 5542f51a9b..f6be30b544 100644 --- a/app/controllers/avo/associations_controller.rb +++ b/app/controllers/avo/associations_controller.rb @@ -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 diff --git a/app/controllers/avo/base_controller.rb b/app/controllers/avo/base_controller.rb index a19bdece03..49f8b5a6ce 100644 --- a/app/controllers/avo/base_controller.rb +++ b/app/controllers/avo/base_controller.rb @@ -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 @@ -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 diff --git a/app/helpers/avo/turbo_stream_actions_helper.rb b/app/helpers/avo/turbo_stream_actions_helper.rb index 7dcd337909..e922d65492 100644 --- a/app/helpers/avo/turbo_stream_actions_helper.rb +++ b/app/helpers/avo/turbo_stream_actions_helper.rb @@ -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}) diff --git a/spec/helpers/avo/turbo_stream_actions_helper_spec.rb b/spec/helpers/avo/turbo_stream_actions_helper_spec.rb new file mode 100644 index 0000000000..cb29f665b4 --- /dev/null +++ b/spec/helpers/avo/turbo_stream_actions_helper_spec.rb @@ -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("") } + 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