diff --git a/spec/models/mongoid/complex_mongoid_example.rb b/spec/models/mongoid/complex_mongoid_example.rb new file mode 100644 index 0000000..06b4809 --- /dev/null +++ b/spec/models/mongoid/complex_mongoid_example.rb @@ -0,0 +1,37 @@ +class ComplexMongoidExample + include Mongoid::Document + include AASM + + field :left, :type => String + field :right, :type => String + + aasm :left, :column => 'left' do + state :one, :initial => true + state :two + state :three + + event :increment do + transitions :from => :one, :to => :two + transitions :from => :two, :to => :three + end + event :reset do + transitions :from => :three, :to => :one + end + end + + aasm :right, :column => 'right' do + state :alpha, :initial => true + state :beta + state :gamma + + event :level_up do + transitions :from => :alpha, :to => :beta + transitions :from => :beta, :to => :gamma + end + event :level_down do + transitions :from => :gamma, :to => :beta + transitions :from => :beta, :to => :alpha + end + end + +end diff --git a/spec/unit/persistence/mongoid_persistence_multiple_spec.rb b/spec/unit/persistence/mongoid_persistence_multiple_spec.rb index 90934d6..99895e5 100644 --- a/spec/unit/persistence/mongoid_persistence_multiple_spec.rb +++ b/spec/unit/persistence/mongoid_persistence_multiple_spec.rb @@ -69,7 +69,59 @@ describe 'mongoid' do end + describe "complex example" do + it "works" do + record = ComplexMongoidExample.new + expect_aasm_states record, :one, :alpha + + record.save! + expect_aasm_states record, :one, :alpha + record.reload + expect_aasm_states record, :one, :alpha + + record.increment! + expect_aasm_states record, :two, :alpha + record.reload + expect_aasm_states record, :two, :alpha + + record.level_up! + expect_aasm_states record, :two, :beta + record.reload + expect_aasm_states record, :two, :beta + + record.increment! + expect { record.increment! }.to raise_error(AASM::InvalidTransition) + expect_aasm_states record, :three, :beta + record.reload + expect_aasm_states record, :three, :beta + + record.level_up! + expect_aasm_states record, :three, :gamma + record.reload + expect_aasm_states record, :three, :gamma + + record.level_down # without saving + expect_aasm_states record, :three, :beta + record.reload + expect_aasm_states record, :three, :gamma + + record.level_down # without saving + expect_aasm_states record, :three, :beta + record.reset! + expect_aasm_states record, :one, :beta + end + + def expect_aasm_states(record, left_state, right_state) + expect(record.aasm(:left).current_state).to eql left_state.to_sym + expect(record.left).to eql left_state.to_s + expect(record.aasm(:right).current_state).to eql right_state.to_sym + expect(record.right).to eql right_state.to_s + end + end + rescue LoadError - puts "Not running Mongoid specs because mongoid gem is not installed!!!" + puts "--------------------------------------------------------------------------" + puts "Not running Mongoid multiple-specs because mongoid gem is not installed!!!" + puts "--------------------------------------------------------------------------" end end diff --git a/spec/unit/persistence/mongoid_persistence_spec.rb b/spec/unit/persistence/mongoid_persistence_spec.rb index 7831de0..56ffd9c 100644 --- a/spec/unit/persistence/mongoid_persistence_spec.rb +++ b/spec/unit/persistence/mongoid_persistence_spec.rb @@ -72,6 +72,8 @@ describe 'mongoid' do end rescue LoadError + puts "--------------------------------------------------------------------------" puts "Not running Mongoid specs because mongoid gem is not installed!!!" + puts "--------------------------------------------------------------------------" end end