Class: TransitionButtons::StateMachineAdapters::StateMachines

Inherits:
Object
  • Object
show all
Defined in:
lib/transition_buttons/state_machine_adapters/state_machines.rb

Overview

Adapter for the state_machines gem.

state_machines prefixes its generated helpers with the machine's attribute name, so a machine declared as state_machine :review_state exposes review_state_events, fire_review_state_event, review_state_name, and Klass.human_review_state_event_name. We build those names from machine.

Instance Method Summary collapse

Instance Method Details

#available_events(record, machine:) ⇒ Object

Events permitted by the machine's guards from the current state. state_machines evaluates if/unless guards here by default, so this is the domain-legal set, before authorization.



13
14
15
# File 'lib/transition_buttons/state_machine_adapters/state_machines.rb', line 13

def available_events(record, machine:)
  record.public_send("#{machine}_events")
end

#current_state(record, machine:) ⇒ Object



17
18
19
# File 'lib/transition_buttons/state_machine_adapters/state_machines.rb', line 17

def current_state(record, machine:)
  record.public_send("#{machine}_name")
end

#event_label(record, event, machine:) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/transition_buttons/state_machine_adapters/state_machines.rb', line 27

def event_label(record, event, machine:)
  humanizer = "human_#{machine}_event_name"
  klass = record.class
  if klass.respond_to?(humanizer)
    klass.public_send(humanizer, event)
  else
    event.to_s.humanize
  end
end

#fire(record, event, machine:) ⇒ Object

Returns true/false (state_machines does not raise when an event is disallowed), letting the caller translate a false into an HTTP 422.



23
24
25
# File 'lib/transition_buttons/state_machine_adapters/state_machines.rb', line 23

def fire(record, event, machine:)
  record.public_send("fire_#{machine}_event", event)
end