fix: timestamp will work with named machine
This commit is contained in:
parent
252ade3a20
commit
ad8fa48c5f
|
@ -268,7 +268,7 @@ module AASM
|
||||||
|
|
||||||
after_all_transitions do
|
after_all_transitions do
|
||||||
if self.class.aasm(:"#{aasm_name}").state_machine.config.timestamps
|
if self.class.aasm(:"#{aasm_name}").state_machine.config.timestamps
|
||||||
ts_setter = "#{aasm.to_state}_at="
|
ts_setter = "#{aasm(aasm_name).to_state}_at="
|
||||||
respond_to?(ts_setter) && send(ts_setter, ::Time.now)
|
respond_to?(ts_setter) && send(ts_setter, ::Time.now)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
class TimestampsWithNamedMachineExample
|
||||||
|
include AASM
|
||||||
|
|
||||||
|
attr_accessor :opened_at
|
||||||
|
|
||||||
|
aasm :my_state, timestamps: true do
|
||||||
|
state :opened
|
||||||
|
|
||||||
|
event :open do
|
||||||
|
transitions to: :opened
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -24,4 +24,9 @@ describe 'timestamps option' do
|
||||||
object.class.aasm.state_machine.config.timestamps = true
|
object.class.aasm.state_machine.config.timestamps = true
|
||||||
expect { object.open }.to change { object.opened_at }
|
expect { object.open }.to change { object.opened_at }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'calls a timestamp setter when using a named state machine' do
|
||||||
|
object = TimestampsWithNamedMachineExample.new
|
||||||
|
expect { object.open }.to change { object.opened_at }.from(nil).to(instance_of(::Time))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue