state-machines / state_machines-activemodel

StateMachines ActiveModel Integration
MIT License
33 stars 33 forks source link

Initializing with aliased state attribute doesn't work #27

Closed mlarraz closed 2 years ago

mlarraz commented 9 years ago

If you use alias_attribute on a state machine attribute, you can correctly use the aliased getters and setters. However, initializing a model with a hash containing the aliased attribute fails to set the state.

This is inconsistent with how attributes are supposed to work in ActiveModel.

A simple example:

require 'state_machines-activemodel'

class Foo
  include ActiveModel::Model

  attr_accessor :status

  alias_attribute :state, :status

  state_machine initial: :new
end

a = Foo.new(state: :old)
# a.state   => :old
# a.status  => :old

a.status = :new
# a.status  => :new
# a.state   => :new

b = Foo.new(status: :old)  # Assigns nothing
# b.state   => :new
# b.status  => :new

b.status = :old
# b.status  => :old
# b.state   => :old
joelvh commented 3 years ago

@seuros is there any interest in fixing this issue? I've been looking at the source code, but am still familiarizing myself with where to hone in on the proper fix.