Module: Weft::DSL::Triggers::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#trigger_declarationsObject

All declared trigger entries (own + inherited), ancestry first.



34
35
36
37
38
39
40
# File 'lib/weft/dsl/triggers.rb', line 34

def trigger_declarations
  if superclass.respond_to?(:trigger_declarations)
    superclass.trigger_declarations + own_triggers
  else
    own_triggers.dup
  end
end

#trigger_events(action_name = nil) ⇒ Object

The event names that fire for one action. Unfiltered declarations fire on every action, including the nameless one; a subclass can therefore widen an inherited filter by redeclaring the event unfiltered, and duplicates collapse.



46
47
48
49
50
# File 'lib/weft/dsl/triggers.rb', line 46

def trigger_events(action_name = nil)
  trigger_declarations.
    select { |trigger| trigger[:on].nil? || trigger[:on].include?(action_name) }.
    map { |trigger| trigger[:event] }.uniq
end

#triggers(event_name, on: nil) ⇒ Object

Declare an event this component announces on its action responses. Sets the HX-Trigger response header.

triggers "order-updated"                     # every action
triggers "order-updated", on: :advance       # that action only
triggers "moved", on: %i[advance retreat]

on: maps the event to the actions it belongs to. Without it an event is welded to every action the component has, which is rarely what a component with more than one action means.

There is deliberately no when: counterpart: HX-Trigger announces what a callable did, so a render-context filter has nothing to say about it.



29
30
31
# File 'lib/weft/dsl/triggers.rb', line 29

def triggers(event_name, on: nil)
  own_triggers << { event: event_name.to_s, on: on.nil? ? nil : Array(on) }
end