Sha256: 66ffcf70b808cbc04a0635698ece0dd45ff0594ff9dca0ee6853e72d5d737a15

Contents?: true

Size: 544 Bytes

Versions: 1

Compression:

Stored size: 544 Bytes

Contents

module StateMachine

  # obj = Model.new
  # obj.process_to state: 'xxx'
  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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_com-1.0.0 app/models/state_machine.rb