Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simple support for ActiveModel's StateMachine for ActiveRecord
- Loading branch information
Showing
9 changed files
with
122 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ def perform(obj) | |
true | ||
end | ||
end | ||
|
||
def execute(obj, *args) | ||
case @on_transition | ||
when Symbol, String | ||
|
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,24 @@ | ||
module ActiveRecord | ||
module StateMachine #:nodoc: | ||
extend ActiveSupport::Concern | ||
include ActiveModel::StateMachine | ||
|
||
included do | ||
before_validation :set_initial_state | ||
validates_presence_of :state | ||
end | ||
|
||
protected | ||
def write_state(state_machine, state) | ||
update_attributes! :state => state.to_s | ||
end | ||
|
||
def read_state(state_machine) | ||
self.state.to_sym | ||
end | ||
|
||
def set_initial_state | ||
self.state ||= self.class.state_machine.initial_state.to_s | ||
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,42 @@ | ||
require 'cases/helper' | ||
require 'models/traffic_light' | ||
|
||
class StateMachineTest < ActiveRecord::TestCase | ||
def setup | ||
@light = TrafficLight.create! | ||
end | ||
|
||
test "states initial state" do | ||
assert @light.off? | ||
assert_equal :off, @light.current_state | ||
end | ||
|
||
test "transition to a valid state" do | ||
@light.reset | ||
assert @light.red? | ||
assert_equal :red, @light.current_state | ||
|
||
@light.green_on | ||
assert @light.green? | ||
assert_equal :green, @light.current_state | ||
end | ||
|
||
test "transition does not persist state" do | ||
@light.reset | ||
assert_equal :red, @light.current_state | ||
@light.reload | ||
assert_equal "off", @light.state | ||
end | ||
|
||
test "transition does persists state" do | ||
@light.reset! | ||
assert_equal :red, @light.current_state | ||
@light.reload | ||
assert_equal "red", @light.state | ||
end | ||
|
||
test "transition to an invalid state" do | ||
assert_raise(ActiveModel::StateMachine::InvalidTransition) { @light.yellow_on } | ||
assert_equal :off, @light.current_state | ||
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,27 @@ | ||
class TrafficLight < ActiveRecord::Base | ||
include ActiveRecord::StateMachine | ||
|
||
state_machine do | ||
state :off | ||
|
||
state :red | ||
state :green | ||
state :yellow | ||
|
||
event :red_on do | ||
transitions :to => :red, :from => [:yellow] | ||
end | ||
|
||
event :green_on do | ||
transitions :to => :green, :from => [:red] | ||
end | ||
|
||
event :yellow_on do | ||
transitions :to => :yellow, :from => [:green] | ||
end | ||
|
||
event :reset do | ||
transitions :to => :red, :from => [:off] | ||
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