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 =

Warehouse tab content frame id prefix. The rendered pane id is
instance-scoped (tab-content-warehouses-<store_id>, see
ApplicationHelper#tab_frame_id), so header detection matches by prefix
and stream targets echo the inbound header / derive from the store.

'tab-content-warehouses'

Instance Method Summary collapse

Methods included from FileDownloadPresenter

#file_download_entry

Instance Method Details

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

Note:

Detection must NOT require an Accept header containing

This method returns an undefined value.

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).

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.

Parameters:

  • options (Hash, String, Symbol) (defaults to: {})

    redirect target (a URL string/symbol
    or a Hash of url_for parts such as :controller/:action)

  • response_options (Hash) (defaults to: {})

    response options forwarded to Rails'
    redirect_to

Options Hash (response_options):

  • status (Symbol, Integer)

    HTTP status for the redirect

  • allow_other_host (Boolean)

    allow redirecting to a
    host other than the request host



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/concerns/controllers/warehouse_stream_actions.rb', line 68

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