Class: Weft::Redirect

Inherits:
Object
  • Object
show all
Defined in:
lib/weft/redirect.rb

Overview

Value object returned from action callables to trigger navigation instead of re-rendering. The Router detects Redirect returns and sends HX-Redirect (htmx) or 302 (traditional form).

Preferred form accepts a Page subclass:

Weft::Redirect.to(OrderDetailPage, order_id: order.id)

String path fallback:

Weft::Redirect.to("/orders/#{order.id}")

Convenience wrapper:

Weft.redirect(OrderDetailPage, order_id: order.id)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, **attrs) ⇒ Redirect

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Use to (or Weft.redirect) — new is private.



21
22
23
24
# File 'lib/weft/redirect.rb', line 21

def initialize(target, **attrs)
  @target = target
  @attrs = attrs
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



17
18
19
# File 'lib/weft/redirect.rb', line 17

def attrs
  @attrs
end

#targetObject (readonly)

Returns the value of attribute target.



17
18
19
# File 'lib/weft/redirect.rb', line 17

def target
  @target
end

Class Method Details

.to(target, **attrs) ⇒ Object

Primary constructor.



39
40
41
# File 'lib/weft/redirect.rb', line 39

def self.to(target, **attrs)
  new(target, **attrs)
end

Instance Method Details

#urlObject

Resolve the redirect URL. Page targets: interpolate attrs into page_path pattern. String targets: use as-is.



29
30
31
32
33
34
35
36
# File 'lib/weft/redirect.rb', line 29

def url
  case @target
  when String
    @target
  else
    @target.resolve_page_path(@attrs)
  end
end