Class: Weft::Action
- Inherits:
-
Object
- Object
- Weft::Action
- Defined in:
- lib/weft/action.rb
Overview
Metadata for a single component action (declared via performs or transfers).
Knows how to derive its route path and generate htmx attributes.
Every Action knows what it renders — the component class rendered in the
response. For performs, that's the declaring class (re-render self).
For transfers, it's the to: class (render something else).
The Router always renders action.renders — no branching.
target is the other kind of target — the CSS selector for where the
response lands in the DOM (hx-target). Nil means the component's own element.
Constant Summary collapse
- SWAP_VALUES =
{ # Semantic names (preferred) replace: "outerHTML", fill: "innerHTML", before: "beforebegin", after: "afterend", append: "beforeend", prepend: "afterbegin", remove: "delete", # htmx-native names (also accepted) outer_html: "outerHTML", inner_html: "innerHTML", before_begin: "beforebegin", after_begin: "afterbegin", before_end: "beforeend", after_end: "afterend", delete: "delete", none: "none" }.freeze
- TRIGGER_VALUES =
{ click: "click", hover: "mouseenter once", visible: "revealed", input: "input changed delay:300ms" }.freeze
Instance Attribute Summary collapse
-
#callable ⇒ Object
readonly
Returns the value of attribute callable.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#renders ⇒ Object
readonly
Returns the value of attribute renders.
-
#swap ⇒ Object
readonly
Returns the value of attribute swap.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Class Method Summary collapse
-
.resolve_swap(value) ⇒ Object
Resolve a swap symbol or string to its htmx value.
-
.resolve_trigger(value) ⇒ Object
Resolve a trigger symbol or string to its htmx value.
Instance Method Summary collapse
-
#initialize(name:, renders:, method: :post, swap: :outer_html, target: nil, callable: nil) ⇒ Action
constructor
rubocop:disable Metrics/ParameterLists.
- #nameless? ⇒ Boolean
-
#route_path(component_path) ⇒ Object
The URL path for this action, given the component's base path.
-
#to_htmx_attrs(component) ⇒ Object
Generate htmx attributes for an element that triggers this action.
Constructor Details
#initialize(name:, renders:, method: :post, swap: :outer_html, target: nil, callable: nil) ⇒ Action
rubocop:disable Metrics/ParameterLists
19 20 21 22 23 24 25 26 |
# File 'lib/weft/action.rb', line 19 def initialize(name:, renders:, method: :post, swap: :outer_html, target: nil, callable: nil) # rubocop:disable Metrics/ParameterLists @name = name @method = method @swap = swap @target = target @renders = renders @callable = callable end |
Instance Attribute Details
#callable ⇒ Object (readonly)
Returns the value of attribute callable.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def callable @callable end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def method @method end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def name @name end |
#renders ⇒ Object (readonly)
Returns the value of attribute renders.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def renders @renders end |
#swap ⇒ Object (readonly)
Returns the value of attribute swap.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def swap @swap end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
17 18 19 |
# File 'lib/weft/action.rb', line 17 def target @target end |
Class Method Details
.resolve_swap(value) ⇒ Object
Resolve a swap symbol or string to its htmx value.
74 75 76 |
# File 'lib/weft/action.rb', line 74 def self.resolve_swap(value) SWAP_VALUES.fetch(value, value.to_s) end |
.resolve_trigger(value) ⇒ Object
Resolve a trigger symbol or string to its htmx value.
79 80 81 |
# File 'lib/weft/action.rb', line 79 def self.resolve_trigger(value) TRIGGER_VALUES.fetch(value, value.to_s) end |
Instance Method Details
#nameless? ⇒ Boolean
55 |
# File 'lib/weft/action.rb', line 55 def nameless? = @name.nil? |
#route_path(component_path) ⇒ Object
The URL path for this action, given the component's base path.
58 59 60 |
# File 'lib/weft/action.rb', line 58 def route_path(component_path) nameless? ? component_path : "#{component_path}/#{name}" end |
#to_htmx_attrs(component) ⇒ Object
Generate htmx attributes for an element that triggers this action.
63 64 65 66 67 68 69 70 71 |
# File 'lib/weft/action.rb', line 63 def to_htmx_attrs(component) path = route_path(component.class.resolved_component_path) { "hx-#{method}" => path, "hx-target" => target || "##{component.weft_id}", "hx-swap" => self.class.resolve_swap(swap), "hx-vals" => component.attrs.to_h.to_json } end |