Skip to content
Browse files

Merge pull request #5 from fourstack/master

Get ssm running gems without AR
  • Loading branch information...
2 parents 740ff80 + 1bbd5a5 commit f6dd4e69b8388911d967765fa338fe2a9cf866e1 @p8 p8 committed
Showing with 225 additions and 211 deletions.
  1. +2 −1 .travis.yml
  2. +8 −0 gemfiles/Gemfile.basic
  3. +5 −4 lib/simple_state_machine.rb
  4. +203 −202 spec/active_record_spec.rb
  5. +7 −4 spec/spec_helper.rb
View
3 .travis.yml
@@ -5,9 +5,10 @@ rvm:
- 1.9.3
#- jruby-18mode # JRuby in 1.8 mode
#- jruby-19mode # JRuby in 1.9 mode
- - rbx-2.0
+ - rbx
script: bundle exec rspec spec
gemfile:
- gemfiles/Gemfile.rails-2.3.x
+ - gemfiles/Gemfile.basic
- Gemfile
View
8 gemfiles/Gemfile.basic
@@ -0,0 +1,8 @@
+# A sample Gemfile
+source "http://rubygems.org"
+
+group :test do
+ #gem "rake"
+ gem "ZenTest"
+ gem "rspec"
+end
View
9 lib/simple_state_machine.rb
@@ -5,9 +5,10 @@
require 'simple_state_machine/state_machine_definition'
require 'simple_state_machine/transition'
require 'simple_state_machine/decorator/default'
-# if defined?(ActiveRecord)
+if defined?(ActiveRecord)
require 'simple_state_machine/active_record'
require 'simple_state_machine/decorator/active_record'
-# end
-require "simple_state_machine/railtie" if defined?(Rails::Railtie)
-
+end
+if defined?(Rails::Railtie)
+ require "simple_state_machine/railtie"
+end
View
405 spec/active_record_spec.rb
@@ -1,255 +1,256 @@
require 'spec_helper'
require 'logger'
-ActiveRecord::Base.logger = Logger.new "test.log"
-ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
- :database => ":memory:")
-
-def setup_db
- ActiveRecord::Schema.define(:version => 1) do
- create_table :users do |t|
- t.column :id, :integer
- t.column :name, :string
- t.column :state, :string
- t.column :activation_code, :string
- t.column :created_at, :datetime
- t.column :updated_at, :datetime
- end
- create_table :tickets do |t|
- t.column :id, :integer
- t.column :ssm_state, :string
+if defined? ActiveRecord
+ ActiveRecord::Base.logger = Logger.new "test.log"
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3",
+ :database => ":memory:")
+
+ def setup_db
+ ActiveRecord::Schema.define(:version => 1) do
+ create_table :users do |t|
+ t.column :id, :integer
+ t.column :name, :string
+ t.column :state, :string
+ t.column :activation_code, :string
+ t.column :created_at, :datetime
+ t.column :updated_at, :datetime
+ end
+ create_table :tickets do |t|
+ t.column :id, :integer
+ t.column :ssm_state, :string
+ end
end
end
-end
-
-def teardown_db
- ActiveRecord::Base.connection.tables.each do |table|
- ActiveRecord::Base.connection.drop_table(table)
- end
-end
-
-class Ticket < ActiveRecord::Base
- extend SimpleStateMachine::ActiveRecord
- state_machine_definition.state_method = :ssm_state
-
- after_initialize :after_initialize
- def after_initialize
- self.ssm_state ||= 'open'
+ def teardown_db
+ ActiveRecord::Base.connection.tables.each do |table|
+ ActiveRecord::Base.connection.drop_table(table)
+ end
end
- event :close, :open => :closed
-end
+ class Ticket < ActiveRecord::Base
+ extend SimpleStateMachine::ActiveRecord
-describe ActiveRecord do
+ state_machine_definition.state_method = :ssm_state
- before do
- setup_db
- end
-
- after do
- teardown_db
- end
+ after_initialize :after_initialize
+ def after_initialize
+ self.ssm_state ||= 'open'
+ end
- it "has a default state" do
- User.new.should be_new
+ event :close, :open => :closed
end
- # TODO needs nesting/grouping, seems to have some duplication
+ describe ActiveRecord do
- describe "event_and_save" do
- it "persists transitions" do
- user = User.create!(:name => 'name')
- user.invite_and_save.should == true
- User.find(user.id).should be_invited
- User.find(user.id).activation_code.should_not be_nil
+ before do
+ setup_db
end
- it "persist transitions even when state is attr_protected" do
- user_class = Class.new(User)
- user_class.instance_eval { attr_protected :state }
- user = user_class.create!(:name => 'name', :state => 'x')
- user.should be_new
- user.invite_and_save
- user.reload.should be_invited
+ after do
+ teardown_db
end
- it "persists transitions when using send and a symbol" do
- user = User.create!(:name => 'name')
- user.send(:invite_and_save).should == true
- User.find(user.id).should be_invited
- User.find(user.id).activation_code.should_not be_nil
+ it "has a default state" do
+ User.new.should be_new
end
- it "raises an error if an invalid state_transition is called" do
- user = User.create!(:name => 'name')
- expect {
- user.confirm_invitation_and_save 'abc'
- }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
- "You cannot 'confirm_invitation' when state is 'new'")
- end
+ # TODO needs nesting/grouping, seems to have some duplication
- it "returns false and keeps state if record is invalid" do
- user = User.new
- user.should be_new
- user.should_not be_valid
- user.invite_and_save.should == false
- user.should be_new
- end
+ describe "event_and_save" do
+ it "persists transitions" do
+ user = User.create!(:name => 'name')
+ user.invite_and_save.should == true
+ User.find(user.id).should be_invited
+ User.find(user.id).activation_code.should_not be_nil
+ end
- it "returns false, keeps state and keeps errors if event adds errors" do
- user = User.create!(:name => 'name')
- user.invite_and_save!
- user.should be_invited
- user.confirm_invitation_and_save('x').should == false
- user.should be_invited
- Array(user.errors[:activation_code]).should == ['is invalid']
- end
+ it "persist transitions even when state is attr_protected" do
+ user_class = Class.new(User)
+ user_class.instance_eval { attr_protected :state }
+ user = user_class.create!(:name => 'name', :state => 'x')
+ user.should be_new
+ user.invite_and_save
+ user.reload.should be_invited
+ end
- it "rollsback if an exception is raised" do
- user_class = Class.new(User)
- user_class.instance_eval do
- define_method :invite_without_managed_state do
- User.create!(:name => 'name2') #this shouldn't be persisted
- User.create! #this should raise an error
- end
+ it "persists transitions when using send and a symbol" do
+ user = User.create!(:name => 'name')
+ user.send(:invite_and_save).should == true
+ User.find(user.id).should be_invited
+ User.find(user.id).activation_code.should_not be_nil
end
- user_class.count.should == 0
- user = user_class.create!(:name => 'name')
- expect {
- user.transaction { user.invite_and_save }
- }.to raise_error(ActiveRecord::RecordInvalid,
- "Validation failed: Name can't be blank")
- user_class.count.should == 1
- user_class.first.name.should == 'name'
- user_class.first.should be_new
- end
- end
- describe "event_and_save!" do
+ it "raises an error if an invalid state_transition is called" do
+ user = User.create!(:name => 'name')
+ expect {
+ user.confirm_invitation_and_save 'abc'
+ }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
+ "You cannot 'confirm_invitation' when state is 'new'")
+ end
- it "persists transitions" do
- user = User.create!(:name => 'name')
- user.invite_and_save!.should == true
- User.find(user.id).should be_invited
- User.find(user.id).activation_code.should_not be_nil
- end
+ it "returns false and keeps state if record is invalid" do
+ user = User.new
+ user.should be_new
+ user.should_not be_valid
+ user.invite_and_save.should == false
+ user.should be_new
+ end
- it "persist transitions even when state is attr_protected" do
- user_class = Class.new(User)
- user_class.instance_eval { attr_protected :state }
- user = user_class.create!(:name => 'name', :state => 'x')
- user.should be_new
- user.invite_and_save!
- user.reload.should be_invited
- end
+ it "returns false, keeps state and keeps errors if event adds errors" do
+ user = User.create!(:name => 'name')
+ user.invite_and_save!
+ user.should be_invited
+ user.confirm_invitation_and_save('x').should == false
+ user.should be_invited
+ Array(user.errors[:activation_code]).should == ['is invalid']
+ end
- it "raises an error if an invalid state_transition is called" do
- user = User.create!(:name => 'name')
- expect {
- user.confirm_invitation_and_save! 'abc'
- }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
- "You cannot 'confirm_invitation' when state is 'new'")
+ it "rollsback if an exception is raised" do
+ user_class = Class.new(User)
+ user_class.instance_eval do
+ define_method :invite_without_managed_state do
+ User.create!(:name => 'name2') #this shouldn't be persisted
+ User.create! #this should raise an error
+ end
+ end
+ user_class.count.should == 0
+ user = user_class.create!(:name => 'name')
+ expect {
+ user.transaction { user.invite_and_save }
+ }.to raise_error(ActiveRecord::RecordInvalid,
+ "Validation failed: Name can't be blank")
+ user_class.count.should == 1
+ user_class.first.name.should == 'name'
+ user_class.first.should be_new
+ end
end
- it "raises a RecordInvalid and keeps state if record is invalid" do
- user = User.new
- user.should be_new
- user.should_not be_valid
- expect {
+ describe "event_and_save!" do
+
+ it "persists transitions" do
+ user = User.create!(:name => 'name')
+ user.invite_and_save!.should == true
+ User.find(user.id).should be_invited
+ User.find(user.id).activation_code.should_not be_nil
+ end
+
+ it "persist transitions even when state is attr_protected" do
+ user_class = Class.new(User)
+ user_class.instance_eval { attr_protected :state }
+ user = user_class.create!(:name => 'name', :state => 'x')
+ user.should be_new
user.invite_and_save!
- }.to raise_error(ActiveRecord::RecordInvalid,
- "Validation failed: Name can't be blank")
- user.should be_new
- end
+ user.reload.should be_invited
+ end
- it "raises a RecordInvalid and keeps state if event adds errors" do
- user = User.create!(:name => 'name')
- user.invite_and_save!
- user.should be_invited
- expect {
- user.confirm_invitation_and_save!('x')
- }.to raise_error(ActiveRecord::RecordInvalid,
- "Validation failed: Activation code is invalid")
- user.should be_invited
- end
+ it "raises an error if an invalid state_transition is called" do
+ user = User.create!(:name => 'name')
+ expect {
+ user.confirm_invitation_and_save! 'abc'
+ }.to raise_error(SimpleStateMachine::IllegalStateTransitionError,
+ "You cannot 'confirm_invitation' when state is 'new'")
+ end
- it "rollsback if an exception is raised" do
- user_class = Class.new(User)
- user_class.instance_eval do
- define_method :invite_without_managed_state do
- User.create!(:name => 'name2') #this shouldn't be persisted
- User.create! #this should raise an error
+ it "raises a RecordInvalid and keeps state if record is invalid" do
+ user = User.new
+ user.should be_new
+ user.should_not be_valid
+ expect {
+ user.invite_and_save!
+ }.to raise_error(ActiveRecord::RecordInvalid,
+ "Validation failed: Name can't be blank")
+ user.should be_new
+ end
+
+ it "raises a RecordInvalid and keeps state if event adds errors" do
+ user = User.create!(:name => 'name')
+ user.invite_and_save!
+ user.should be_invited
+ expect {
+ user.confirm_invitation_and_save!('x')
+ }.to raise_error(ActiveRecord::RecordInvalid,
+ "Validation failed: Activation code is invalid")
+ user.should be_invited
+ end
+
+ it "rollsback if an exception is raised" do
+ user_class = Class.new(User)
+ user_class.instance_eval do
+ define_method :invite_without_managed_state do
+ User.create!(:name => 'name2') #this shouldn't be persisted
+ User.create! #this should raise an error
+ end
end
+ user_class.count.should == 0
+ user = user_class.create!(:name => 'name')
+ expect {
+ user.transaction { user.invite_and_save! }
+ }.to raise_error(ActiveRecord::RecordInvalid,
+ "Validation failed: Name can't be blank")
+ user_class.count.should == 1
+ user_class.first.name.should == 'name'
+ user_class.first.should be_new
end
- user_class.count.should == 0
- user = user_class.create!(:name => 'name')
- expect {
- user.transaction { user.invite_and_save! }
- }.to raise_error(ActiveRecord::RecordInvalid,
- "Validation failed: Name can't be blank")
- user_class.count.should == 1
- user_class.first.name.should == 'name'
- user_class.first.should be_new
+
end
- end
+ describe "event" do
+ it "persists transitions" do
+ user = User.create!(:name => 'name')
+ user.invite.should == true
+ User.find(user.id).should be_invited
+ User.find(user.id).activation_code.should_not be_nil
+ end
- describe "event" do
- it "persists transitions" do
- user = User.create!(:name => 'name')
- user.invite.should == true
- User.find(user.id).should be_invited
- User.find(user.id).activation_code.should_not be_nil
- end
+ it "returns false, keeps state and keeps errors if event adds errors" do
+ user = User.create!(:name => 'name')
+ user.invite_and_save!
+ user.should be_invited
+ user.confirm_invitation('x').should == false
+ user.should be_invited
+ Array(user.errors[:activation_code]).should == ['is invalid']
+ end
- it "returns false, keeps state and keeps errors if event adds errors" do
- user = User.create!(:name => 'name')
- user.invite_and_save!
- user.should be_invited
- user.confirm_invitation('x').should == false
- user.should be_invited
- Array(user.errors[:activation_code]).should == ['is invalid']
end
- end
+ describe "event!" do
- describe "event!" do
+ it "persists transitions" do
+ user = User.create!(:name => 'name')
+ user.invite!.should == true
+ User.find(user.id).should be_invited
+ User.find(user.id).activation_code.should_not be_nil
+ end
- it "persists transitions" do
- user = User.create!(:name => 'name')
- user.invite!.should == true
- User.find(user.id).should be_invited
- User.find(user.id).activation_code.should_not be_nil
- end
+ it "raises a RecordInvalid and keeps state if record is invalid" do
+ user = User.new
+ user.should be_new
+ user.should_not be_valid
+ expect { user.invite! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
+ user.should be_new
+ end
- it "raises a RecordInvalid and keeps state if record is invalid" do
- user = User.new
- user.should be_new
- user.should_not be_valid
- expect { user.invite! }.to raise_error(ActiveRecord::RecordInvalid, "Validation failed: Name can't be blank")
- user.should be_new
end
- end
+ describe 'custom state method' do
- describe 'custom state method' do
+ it "persists transitions" do
+ ticket = Ticket.create!
+ ticket.ssm_state.should == 'open'
+ ticket.close!.should == true
+ ticket.ssm_state.should == 'closed'
+ end
- it "persists transitions" do
- ticket = Ticket.create!
- ticket.ssm_state.should == 'open'
- ticket.close!.should == true
- ticket.ssm_state.should == 'closed'
- end
+ it "persists transitions with !" do
+ ticket = Ticket.create!
+ ticket.ssm_state.should == 'open'
+ ticket.close!
+ ticket.ssm_state.should == 'closed'
+ end
- it "persists transitions with !" do
- ticket = Ticket.create!
- ticket.ssm_state.should == 'open'
- ticket.close!
- ticket.ssm_state.should == 'closed'
end
end
-
end
-
View
11 spec/spec_helper.rb
@@ -1,8 +1,11 @@
require "rubygems"
require "bundler"
-Bundler.require
-#Bundler.setup(:test)#, :activerecord)
-require 'active_record'
+Bundler.require :test
+begin
+ require 'active_record'
+rescue LoadError
+ puts "Skipping ActiveRecord specs"
+end
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
$LOAD_PATH << File.join(ROOT, 'lib')
@@ -13,5 +16,5 @@
require File.join(ROOT, 'examples', 'conversation.rb')
require File.join(ROOT, 'examples', 'lamp.rb')
require File.join(ROOT, 'examples', 'traffic_light.rb')
-require File.join(ROOT, 'examples', 'user.rb')
+require File.join(ROOT, 'examples', 'user.rb') if defined? ActiveRecord

0 comments on commit f6dd4e6

Please sign in to comment.
Something went wrong with that request. Please try again.