Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple option for auto-generated timestamps
- Loading branch information
Showing
10 changed files
with
176 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class TimestampExample < ActiveRecord::Base | ||
include AASM | ||
|
||
aasm column: :aasm_state, timestamps: true do | ||
state :opened | ||
state :closed | ||
|
||
event :open do | ||
transitions to: :opened | ||
end | ||
|
||
event :close do | ||
transitions to: :closed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class TimestampExampleMongoid | ||
include Mongoid::Document | ||
include AASM | ||
|
||
field :status, type: String | ||
field :opened_at, type: Time | ||
|
||
aasm column: :status, timestamps: true do | ||
state :opened | ||
state :closed | ||
|
||
event :open do | ||
transitions to: :opened | ||
end | ||
|
||
event :close do | ||
transitions to: :closed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class TimestampsExample | ||
include AASM | ||
|
||
attr_accessor :opened_at | ||
attr_reader :closed_at | ||
|
||
aasm timestamps: true do | ||
state :opened | ||
state :closed | ||
|
||
event :open do | ||
transitions to: :opened | ||
end | ||
|
||
event :close do | ||
transitions to: :closed | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
describe 'timestamps option' do | ||
it 'calls a timestamp setter based on the state name when entering a new state' do | ||
object = TimestampsExample.new | ||
expect { object.open }.to change { object.opened_at }.from(nil).to(instance_of(::Time)) | ||
end | ||
|
||
it 'overwrites any previous timestamp if a state is entered repeatedly' do | ||
object = TimestampsExample.new | ||
object.opened_at = ::Time.new(2000, 1, 1) | ||
expect { object.open }.to change { object.opened_at } | ||
end | ||
|
||
it 'does nothing if there is no setter matching the new state' do | ||
object = TimestampsExample.new | ||
expect { object.close }.not_to change { object.closed_at } | ||
end | ||
|
||
it 'can be turned off and on' do | ||
object = TimestampsExample.new | ||
object.class.aasm.state_machine.config.timestamps = false | ||
expect { object.open }.not_to change { object.opened_at } | ||
object.class.aasm.state_machine.config.timestamps = true | ||
expect { object.open }.to change { object.opened_at } | ||
end | ||
end |