Module: Trane::RoutingExtension

Defined in:
lib/trane/routing_extension.rb

Overview

Prepended globally onto ActionDispatch::Routing::Mapper by the Engine initializer trane.prepend_routing_extension. Available in all routes.draw blocks. Routes without contract: pass through unchanged in O(1).

Uses *path_or_actions, **kwargs to mirror Rails' own Mapper::Resources#match signature and handle every internal calling convention (normal path, hash- shorthand "up" => "ctrl#action", zero-positional-arg match calls, etc.).

Constant Summary collapse

CONTRACT_KEYS =

Single source of truth for the keys accepted inside contract: { ... }. Anything else is a typo and must fail loud at route-draw time rather than being silently ignored or deferred to request time.

%i[operation].freeze

Instance Method Summary collapse

Instance Method Details

#match(*path_or_actions, **kwargs) ⇒ Object

Intercepts route declarations that carry contract: { operation: name }. Strips the key, injects the operation name into defaults[:_trane_operation] (used at request time to resolve the contract), and sets as: name so the operation is discoverable as a named-route prefix in bin/rails routes.

Routes without contract: are forwarded to super unchanged.

Rails 7.x / 8.0 call match(*args, options_hash) via map_method — a plain Hash as the last positional argument. Rails 8.1+ uses proper keyword arguments. Both conventions must be handled here.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/trane/routing_extension.rb', line 28

def match(*path_or_actions, **kwargs)
  if kwargs.key?(:contract)
    # Rails 8.1+ keyword-argument path
    kwargs = kwargs.dup
    contract = kwargs.delete(:contract)
    _inject_trane_contract!(nil, kwargs, contract)
  elsif path_or_actions.last.is_a?(Hash) && path_or_actions.last.key?(:contract)
    # Rails 7.x / 8.0 positional-hash path (map_method calls match(*args, opts))
    path_or_actions = path_or_actions.dup
    options = path_or_actions.last.dup
    path_or_actions[-1] = options
    contract = options.delete(:contract)
    _inject_trane_contract!(options, nil, contract)
  end

  super(*path_or_actions, **kwargs)
end