diff --git a/app/controllers/admin/custom_fields/custom_field_projects_controller.rb b/app/controllers/admin/custom_fields/custom_field_projects_controller.rb index f78f78674bf..5f5eb3355ac 100644 --- a/app/controllers/admin/custom_fields/custom_field_projects_controller.rb +++ b/app/controllers/admin/custom_fields/custom_field_projects_controller.rb @@ -67,7 +67,7 @@ class Admin::CustomFields::CustomFieldProjectsController < ApplicationController ) end - respond_to_with_turbo_streams(status: create_service.success? ? :ok : :unprocessable_entity) + respond_to_with_turbo_streams(status: create_service) end def destroy @@ -83,7 +83,7 @@ class Admin::CustomFields::CustomFieldProjectsController < ApplicationController ) end - respond_to_with_turbo_streams(status: delete_service.success? ? :ok : :unprocessable_entity) + respond_to_with_turbo_streams(status: delete_service) end private diff --git a/app/controllers/admin/settings/project_custom_fields_controller.rb b/app/controllers/admin/settings/project_custom_fields_controller.rb index 75c3460e77e..8b88b00eb25 100644 --- a/app/controllers/admin/settings/project_custom_fields_controller.rb +++ b/app/controllers/admin/settings/project_custom_fields_controller.rb @@ -123,7 +123,7 @@ module Admin::Settings ) end - respond_to_with_turbo_streams(status: create_service.success? ? :ok : :unprocessable_entity) + respond_to_with_turbo_streams(status: create_service) end def unlink @@ -139,7 +139,7 @@ module Admin::Settings ) end - respond_to_with_turbo_streams(status: delete_service.success? ? :ok : :unprocessable_entity) + respond_to_with_turbo_streams(status: delete_service) end def move diff --git a/app/controllers/concerns/op_turbo/component_stream.rb b/app/controllers/concerns/op_turbo/component_stream.rb index 0136201e206..a7ff97a9868 100644 --- a/app/controllers/concerns/op_turbo/component_stream.rb +++ b/app/controllers/concerns/op_turbo/component_stream.rb @@ -32,10 +32,25 @@ module OpTurbo module ComponentStream extend ActiveSupport::Concern + # Builds a turbo stream response block, supports different ways of building response statuses. + # It can take a `result` object that will serve as a base for a status, or a `status` symbol + # directly. + # + # @param status [Symbol, ServiceResult, Dry::Monads[:result]] the response status, if a result + # object is provided, it is evaluated based on its state. Defaults to `:ok`. + # @yield [format] Optional block to handle additional response formats + # @yieldparam format [ActionController::MimeResponds::Collector] + # def respond_to_with_turbo_streams(status: turbo_status, &format_block) + resolved_status = if status.respond_to?(:success?) + status.success? ? :ok : :unprocessable_entity + else + status + end + respond_to do |format| format.turbo_stream do - render turbo_stream: turbo_streams, status: + render turbo_stream: turbo_streams, status: resolved_status end yield(format) if format_block diff --git a/modules/backlogs/app/controllers/backlogs/work_packages_controller.rb b/modules/backlogs/app/controllers/backlogs/work_packages_controller.rb index b9aafdfdb9a..8888672a007 100644 --- a/modules/backlogs/app/controllers/backlogs/work_packages_controller.rb +++ b/modules/backlogs/app/controllers/backlogs/work_packages_controller.rb @@ -81,7 +81,7 @@ module Backlogs ) end - respond_with_turbo_streams(status: call.success? ? :ok : :unprocessable_entity) + respond_with_turbo_streams(status: call) end private diff --git a/modules/costs/app/controllers/time_entries_controller.rb b/modules/costs/app/controllers/time_entries_controller.rb index 4b72f293bb6..2985b1efd77 100644 --- a/modules/costs/app/controllers/time_entries_controller.rb +++ b/modules/costs/app/controllers/time_entries_controller.rb @@ -119,7 +119,7 @@ class TimeEntriesController < ApplicationController errors: call.errors.full_messages.join(", "))) end - respond_with_turbo_streams(status: call.success? ? :ok : :bad_request) + respond_with_turbo_streams(status: call) end def destroy # rubocop:disable Metrics/AbcSize @@ -141,7 +141,7 @@ class TimeEntriesController < ApplicationController errors: call.errors.full_messages.join(", "))) end - respond_with_turbo_streams(status: call.success? ? :ok : :bad_request) + respond_with_turbo_streams(status: call) end private diff --git a/modules/overviews/app/controllers/overviews/project_phases_controller.rb b/modules/overviews/app/controllers/overviews/project_phases_controller.rb index 2498292c41c..b1c596410bd 100644 --- a/modules/overviews/app/controllers/overviews/project_phases_controller.rb +++ b/modules/overviews/app/controllers/overviews/project_phases_controller.rb @@ -60,15 +60,14 @@ module ::Overviews .new(user: current_user, model: @project_phase) .call(permitted_params.project_phase) - component, status = - if service_call.success? - [Overviews::ProjectPhases::SidePanelComponent.new(project: @project), :ok] - else - [Overviews::ProjectPhases::EditComponent.new(service_call.result), :unprocessable_entity] - end + component = if service_call.success? + Overviews::ProjectPhases::SidePanelComponent.new(project: @project) + else + Overviews::ProjectPhases::EditComponent.new(service_call.result) + end update_via_turbo_stream(component:) - respond_to_with_turbo_streams(status:) + respond_to_with_turbo_streams(status: service_call) end private diff --git a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb index cc4a459636a..42f00235e6e 100644 --- a/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb +++ b/modules/storages/app/controllers/storages/admin/storages/project_storages_controller.rb @@ -93,7 +93,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll update_via_turbo_stream(component:, status: :bad_request) end - respond_with_turbo_streams(status: create_service.success? ? :ok : :unprocessable_entity) + respond_with_turbo_streams(status: create_service) end def update @@ -108,7 +108,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll update_via_turbo_stream(component:, status: :bad_request) end - respond_with_turbo_streams(status: update_service.success? ? :ok : :unprocessable_entity) + respond_with_turbo_streams(status: update_service) end def destroy_confirmation_dialog @@ -136,7 +136,7 @@ class Storages::Admin::Storages::ProjectStoragesController < ApplicationControll ) end - respond_to_with_turbo_streams(status: delete_service.success? ? :ok : :unprocessable_entity) + respond_to_with_turbo_streams(status: delete_service) end private