module CanHasState::Machine
Private Instance Methods
can_has_deferred_state_triggers()
click to toggle source
# File lib/can_has_state/machine.rb, line 68 def can_has_deferred_state_triggers @triggers_called ||= {} state_machines.each do |(column, sm)| # clear record of called triggers @triggers_called[column] = nil if respond_to?("#{column}_before_last_save") # rails 5.1+ from, to = send("#{column}_before_last_save"), send(column) else from, to = send("#{column}_was"), send(column) end next if from == to sm.trigger(self, from, to, :deferred) end end
can_has_initial_states()
click to toggle source
# File lib/can_has_state/machine.rb, line 41 def can_has_initial_states state_machines.each do |(column, sm)| if send(column).blank? send("#{column}=", sm.initial_state) end end end
can_has_state_errors()
click to toggle source
# File lib/can_has_state/machine.rb, line 84 def can_has_state_errors err = [] state_machines.each do |(column, sm)| from, to = send("#{column}_was"), send(column) next if from == to if !sm.known?(to) err << [column, :invalid_state] elsif !sm.allow?(self, to) #state_machine_allow?(column, to) err << [column, sm.message(to), {from: from, to: to}] end end err end
can_has_state_triggers()
click to toggle source
# File lib/can_has_state/machine.rb, line 49 def can_has_state_triggers # skip triggers if any state machine isn't valid return if can_has_state_errors.any? @triggers_called ||= {} state_machines.each do |(column, sm)| from, to = send("#{column}_was"), send(column) next if from == to # skip triggers if they've already been called for this from/to transition next if @triggers_called[column] == [from, to] sm.trigger(self, from, to) # record that triggers were called @triggers_called[column] = [from, to] end end
can_has_valid_state_machines()
click to toggle source
# File lib/can_has_state/machine.rb, line 98 def can_has_valid_state_machines can_has_state_errors.each do |(column, msg, opts)| errors.add column, msg, **(opts||{}) end end
state_machine_allow?(column, to)
click to toggle source
# File lib/can_has_state/machine.rb, line 104 def state_machine_allow?(column, to) sm = state_machines.detect{|(col, _)| col == column} # |(col, stm)| raise("Unknown state machine #{column}") unless sm sm[1].allow?(self, to) end