Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Rothstein
committed
Feb 4, 2010
1 parent
5e45012
commit bfb47be
Showing
7 changed files
with
88 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require File.join(File.dirname(__FILE__), 'user') | ||
class TwoStateRule < User | ||
state :new do | ||
transitions { self.state = :has_name if name.present? } | ||
end | ||
|
||
state :has_name do | ||
transitions { self.state = :new if name.blank? } | ||
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,3 @@ | ||
class User < ActiveRecord::Base | ||
include SimplyStated | ||
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,9 @@ | ||
ActiveRecord::Schema.define :version => 0 do | ||
create_table :users, :force => true do |t| | ||
t.column :name, :string | ||
t.column :email_confirmed, :boolean, :default => false, :null => false | ||
t.column :approved, :boolean, :default => false, :null => false | ||
t.column :state, :string, :default => "new" | ||
t.column :type, :string | ||
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 |
---|---|---|
@@ -1,6 +1,66 @@ | ||
require File.instance_eval { expand_path join(dirname(__FILE__), 'test_helper') } | ||
require 'active_record' | ||
|
||
class TestSimplyStated < Test::Unit::TestCase | ||
should 'write the tests' | ||
context 'with a user' do | ||
setup do | ||
@user = User.new | ||
end | ||
|
||
should 'be in state new' do | ||
assert_state :new, @user | ||
end | ||
|
||
should 'not accept an invalid state' do | ||
exception = assert_raises(RuntimeError) {@user.state = :foo} | ||
assert_message "invalid state foo", exception | ||
end | ||
|
||
context 'with a known state' do | ||
setup { User.state :has_name } | ||
|
||
should 'accept symbol wibble as a state' do | ||
assert_nothing_raised {@user.state = :has_name} | ||
end | ||
|
||
should 'accept string wibble as a state' do | ||
assert_nothing_raised {@user.state = "has_name"} | ||
end | ||
end | ||
|
||
context 'with simple transition rules between new and has_name' do | ||
setup { @user = TwoStateRule.new } | ||
|
||
should 'list all the states' do | ||
assert_equal Set.new([:new, :has_name]), TwoStateRule.states | ||
end | ||
|
||
context 'when saved, the user' do | ||
setup {@user.save!} | ||
should 'stay in state new' do | ||
assert_state :new, @user | ||
end | ||
end | ||
|
||
context 'when the user has a name, they' do | ||
setup {@user.update_attributes! :name => "lorrie"} | ||
should 'transition to state has_name' do | ||
assert_state :has_name, @user | ||
end | ||
end | ||
|
||
context 'if the user is somehow put into the has_name state but has no name' do | ||
setup { @user.update_attributes! :state => :has_name } | ||
|
||
should 'stay in new state' do | ||
assert_state :new, @user | ||
end | ||
end | ||
end | ||
|
||
should 'test validations' | ||
should 'test validation inheritence' | ||
|
||
should 'test in_state named scope' | ||
should 'test not_in_state named scope' | ||
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