Module: Controllers::WarehouseStreamActions

Extended by:
ActiveSupport::Concern
Includes:
FileDownloadPresenter
Included in:
Crm::WarehousesController, DeliveriesController, OrdersController
Defined in:
app/concerns/controllers/warehouse_stream_actions.rb

Overview

Makes warehouse-dashboard actions "rock solid" by guaranteeing every request
triggered from the warehouse tabs returns a turbo_stream that refreshes the
active tab in place — never a redirect that would trigger a full Turbo Drive
navigation back to /warehouses/:id and the fragment-driven blank-tab race
that comes with it.

Two integration points are covered:

  1. Workflowable already calls render_workflow_success_stream /
    render_workflow_error_stream on the turbo_stream format. We override
    both to render the warehouse stream.

  2. Plenty of warehouse buttons aren't workflow_action links — they are
    plain controller actions (cancel_pickup, shipped, release, …) that end
    with redirect_to. We override redirect_to so any turbo_stream
    request that redirects to a /warehouses/:id path becomes an in-place
    tab refresh instead of a full page reload.

Detection prefers two signals so it never hijacks unrelated requests:

  1. params[:warehouse_context] — the explicit opt-in used by
    workflow_action links built inside the warehouse view.
  2. Turbo-Frame: tab-content-warehouses request header — set by Turbo
    when a link / form submission originates from inside the warehouse
    tab content frame (e.g. plain link_to ..., data: { turbo_method: :post } like "Release CR Hold").

Only when one of those is true do we treat a redirect to a /warehouses/:id
path as a tab refresh. A form on a standalone page (e.g. the
/deliveries/:id/picked edit page) that happens to redirect back to the
warehouse is left alone so Turbo Drive performs a real navigation.

Constant Summary collapse

WAREHOUSE_TAB_CONTENT_FRAME =
'tab-content-warehouses'

Instance Method Summary collapse

Methods included from FileDownloadPresenter

#file_download_entry

Instance Method Details

#redirect_to(options = {}, response_options = {}) ⇒ Object

Override redirect_to so any request originating from inside the warehouse
tab content frame never causes a full Turbo Drive navigation — instead we
render a turbo_stream that refreshes the active tab in place (when the
redirect target is back to /warehouses/:id) or a turbo_stream.redirect
that triggers a clean Turbo.visit (when the target is elsewhere, e.g. a
job progress page).

IMPORTANT: detection must NOT require an Accept header containing
turbo-stream. Turbo Frame links/forms send Accept: text/html plus a
Turbo-Frame: header — they are NOT turbo-stream-capable by Accept,
but Turbo will happily execute a response of Content-Type
text/vnd.turbo-stream.html regardless of the Accept header. Falling
through to a bare 302 here is the "blank tab" failure mode: Turbo
follows the redirect, fetches the full warehouse HTML page, can't slot
it into the frame, and renders nothing.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/concerns/controllers/warehouse_stream_actions.rb', line 55

def redirect_to(options = {}, response_options = {})
  if warehouse_frame_request?
    # Swallow follow-up redirect_to calls within the same warehouse-frame
    # action instead of letting them bubble to Rails as DoubleRenderError
    # (AppSignal #4729). The first render_warehouse_stream call has already
    # produced the response we want.
    if performed?
      Rails.logger.warn(
        "[WarehouseStreamActions] ignoring redirect_to after response already performed " \
        "(controller=#{self.class.name} action=#{action_name} target=#{warehouse_redirect_target(options).inspect})"
      )
      return
    end

    target = warehouse_redirect_target(options)
    if warehouse_path?(target)
      render_warehouse_stream(target_url: target)
    else
      render turbo_stream: turbo_stream.redirect(target || @return_path || '/')
    end
    return
  end

  super
end