Skip to content

Commit

Permalink
fix: timestamp will work with named machine
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandStuder committed Apr 23, 2021
1 parent 252ade3 commit ade2484
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/aasm/base.rb
Expand Up @@ -268,7 +268,7 @@ def setup_timestamps(aasm_name)

after_all_transitions do
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)
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/models/timestamps_with_named_machine_example.rb
@@ -0,0 +1,19 @@
class TimestampsWithNamedMachineExample
include AASM

attr_accessor :opened_at
attr_reader :closed_at

aasm :my_state, timestamps: true do
state :opened
state :closed

event :open do
transitions to: :opened
end

event :close do
transitions to: :closed
end
end
end
5 changes: 5 additions & 0 deletions spec/unit/timestamps_spec.rb
Expand Up @@ -24,4 +24,9 @@
object.class.aasm.state_machine.config.timestamps = true
expect { object.open }.to change { object.opened_at }
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

0 comments on commit ade2484

Please sign in to comment.