Module: Weft::DSL::Actions::ClassMethods

Defined in:
lib/weft/dsl/actions.rb

Instance Method Summary collapse

Instance Method Details

#action_for(name) ⇒ Object

Look up an action by name (for Context interception).



60
61
62
# File 'lib/weft/dsl/actions.rb', line 60

def action_for(name)
  actions.values.find { |a| a.name == name }
end

#actionsObject

All declared actions (own + inherited), keyed by [name, method].



51
52
53
54
55
56
57
# File 'lib/weft/dsl/actions.rb', line 51

def actions
  if superclass.respond_to?(:actions)
    superclass.actions.merge(own_actions)
  else
    own_actions.dup
  end
end

#dismisses(name = nil, method: :delete) ⇒ Object

Sugar for performs with swap: :delete. Removes the component from the DOM on success. The callable (if given) runs for side effects; the return value is rendered but htmx ignores the response body.

dismisses :close                          # no side effects
dismisses :remove do |attrs|              # with side effects
Item.find(attrs.item_id).archive!
end


35
36
37
# File 'lib/weft/dsl/actions.rb', line 35

def dismisses(name = nil, method: :delete, &)
  performs(name, method: method, swap: :delete, &)
end

#performs(name = nil, method: :post, swap: :outer_html, target: nil, &block) ⇒ Object

Declare a user-initiated action on this component.

performs :advance do |attrs|
order = Order.find(attrs.order_id)
Oms::PrepareOrder.call(order)
end

performs method: :delete, swap: :delete do |attrs| ... end


21
22
23
24
25
# File 'lib/weft/dsl/actions.rb', line 21

def performs(name = nil, method: :post, swap: :outer_html, target: nil, &block)
  action = Weft::Action.new(name: name, method: method, swap: swap,
                            target: target, renders: self, callable: block)
  own_actions[[name, method]] = action
end

#transfers(name = nil, to:, method: :post, swap: :outer_html, target: nil, &block) ⇒ Object

Declare a transfer — an action that renders a different component.

transfers :edit, to: EditableOrderHeader do |attrs|
{ mode: "full" }
end


44
45
46
47
48
# File 'lib/weft/dsl/actions.rb', line 44

def transfers(name = nil, to:, method: :post, swap: :outer_html, target: nil, &block)
  action = Weft::Action.new(name: name, method: method, swap: swap,
                            target: target, renders: to, callable: block)
  own_actions[[name, method]] = action
end