Module: StateMachine

Defined in:
app/models/state_machine.rb

Instance Method Summary collapse

Instance Method Details

#process_to(options = {}) ⇒ Object

obj = Model.new obj.process_to state: ‘xxx’



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/models/state_machine.rb', line 5

def process_to(options = {})
  if options.size > 1
    raise 'Only support one column'
  end

  options.each do |k, v|
    states = k.to_s.pluralize
    states = self.class.send(states).keys

    i = states.find_index self.send(k)
    n = states[i+1]

    if n == v.to_s
      update!(k => states[v])
    else
      errors.add :state, 'Next state is wrong'
      raise ActiveRecord::Rollback, 'Next state is wrong'
    end
  end
end