mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
test support for MongoMapper and multiple state machines per class
This commit is contained in:
parent
f638333313
commit
44cf3f3220
8 changed files with 230 additions and 11 deletions
4
Gemfile
4
Gemfile
|
@ -9,7 +9,7 @@ gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.
|
|||
gem 'sequel'
|
||||
# Since mongoid V4 requires incompatible bson V2, cannot have mongoid (V4 or greater)
|
||||
# and mongo_mapper ( or mongo ) in the same application
|
||||
#gem 'mongo_mapper', '~> 0.13'
|
||||
#gem 'bson_ext', :platforms => :ruby
|
||||
# gem 'mongo_mapper', '~> 0.13'
|
||||
# gem 'bson_ext', :platforms => :ruby
|
||||
|
||||
gemspec
|
||||
|
|
|
@ -51,18 +51,18 @@ module AASM
|
|||
# Foo.find(1).aasm.current_state # => :closed
|
||||
#
|
||||
# NOTE: intended to be called from an event
|
||||
def aasm_write_state(state)
|
||||
old_value = read_attribute(self.class.aasm.attribute_name)
|
||||
aasm_write_attribute state
|
||||
def aasm_write_state(state, name=:default)
|
||||
old_value = read_attribute(self.class.aasm(name).attribute_name)
|
||||
write_attribute(self.class.aasm(name).attribute_name, state)
|
||||
|
||||
success = if aasm_skipping_validations
|
||||
value = aasm_raw_attribute_value state
|
||||
self.class.where(self.class.primary_key => self.id).update_all(self.class.aasm.attribute_name => value) == 1
|
||||
success = if aasm_skipping_validations(name)
|
||||
value = aasm_raw_attribute_value(state, name)
|
||||
self.class.where(self.class.primary_key => self.id).update_all(self.class.aasm(name).attribute_name => value) == 1
|
||||
else
|
||||
self.save
|
||||
end
|
||||
unless success
|
||||
write_attribute(self.class.aasm.attribute_name, old_value)
|
||||
write_attribute(self.class.aasm(name).attribute_name, old_value)
|
||||
return false
|
||||
end
|
||||
|
||||
|
@ -82,7 +82,7 @@ module AASM
|
|||
#
|
||||
# NOTE: intended to be called from an event
|
||||
def aasm_write_state_without_persistence(state, name=:default)
|
||||
aasm_write_attribute state
|
||||
aasm_write_attribute(state, name)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
37
spec/models/mongo_mapper/complex_mongo_mapper_example.rb
Normal file
37
spec/models/mongo_mapper/complex_mongo_mapper_example.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
class ComplexMongoMapperExample
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :left, String
|
||||
key :right, 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
|
|
@ -8,3 +8,14 @@ class NoScopeMongoMapper
|
|||
state :ignored_scope
|
||||
end
|
||||
end
|
||||
|
||||
class NoScopeMongoMapperMultiple
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :left, :create_scopes => false, :column => :status do
|
||||
state :ignored_scope
|
||||
end
|
||||
end
|
||||
|
|
|
@ -9,3 +9,15 @@ class SimpleMongoMapper
|
|||
state :next
|
||||
end
|
||||
end
|
||||
|
||||
class SimpleMongoMapperMultiple
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :left, column: :status do
|
||||
state :unknown_scope
|
||||
state :next
|
||||
end
|
||||
end
|
||||
|
|
|
@ -10,3 +10,16 @@ class SimpleNewDslMongoMapper
|
|||
state :next
|
||||
end
|
||||
end
|
||||
|
||||
class SimpleNewDslMongoMapperMultiple
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :left, :column => :status
|
||||
aasm :left do
|
||||
state :unknown_scope
|
||||
state :next
|
||||
end
|
||||
end
|
||||
|
|
144
spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb
Normal file
144
spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb
Normal file
|
@ -0,0 +1,144 @@
|
|||
describe 'mongo_mapper' do
|
||||
begin
|
||||
require 'mongo_mapper'
|
||||
require 'logger'
|
||||
require 'spec_helper'
|
||||
|
||||
before(:all) do
|
||||
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each { |f| require File.expand_path(f) }
|
||||
|
||||
config = {
|
||||
'test' => {
|
||||
'database' => "mongo_mapper_#{Process.pid}"
|
||||
}
|
||||
}
|
||||
|
||||
MongoMapper.setup(config, 'test') #, :logger => Logger.new(STDERR))
|
||||
end
|
||||
|
||||
after do
|
||||
# Clear Out all non-system Mongo collections between tests
|
||||
MongoMapper.database.collections.each do |collection|
|
||||
collection.drop unless collection.capped? || (collection.name =~ /\Asystem/)
|
||||
end
|
||||
end
|
||||
|
||||
describe "named scopes with the old DSL" do
|
||||
|
||||
context "Does not already respond_to? the scope name" do
|
||||
it "should add a scope" do
|
||||
expect(SimpleMongoMapperMultiple).to respond_to(:unknown_scope)
|
||||
expect(SimpleMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
||||
#expect(SimpleMongoMapperMultiple.unknown_scope.is_a?(ActiveRecord::Relation)).to be_truthy
|
||||
end
|
||||
end
|
||||
|
||||
context "Already respond_to? the scope name" do
|
||||
it "should not add a scope" do
|
||||
expect(SimpleMongoMapperMultiple).to respond_to(:next)
|
||||
expect(SimpleMongoMapperMultiple.new.class).to eq(SimpleMongoMapperMultiple)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "named scopes with the new DSL" do
|
||||
|
||||
context "Does not already respond_to? the scope name" do
|
||||
it "should add a scope" do
|
||||
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:unknown_scope)
|
||||
expect(SimpleNewDslMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
||||
end
|
||||
end
|
||||
|
||||
context "Already respond_to? the scope name" do
|
||||
it "should not add a scope" do
|
||||
expect(SimpleNewDslMongoMapperMultiple).to respond_to(:next)
|
||||
expect(SimpleNewDslMongoMapperMultiple.new.class).to eq(SimpleNewDslMongoMapperMultiple)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not create scopes if requested" do
|
||||
expect(NoScopeMongoMapperMultiple).not_to respond_to(:ignored_scope)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "instance methods" do
|
||||
|
||||
let(:simple) {SimpleNewDslMongoMapperMultiple.new}
|
||||
|
||||
it "should call aasm_ensure_initial_state on validation before create" do
|
||||
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).initial_state).to eq(:unknown_scope)
|
||||
expect(SimpleNewDslMongoMapperMultiple.aasm(:left).attribute_name).to eq(:status)
|
||||
expect(simple.status).to eq(nil)
|
||||
simple.valid?
|
||||
expect(simple.status).to eq('unknown_scope')
|
||||
end
|
||||
|
||||
it "should call aasm_ensure_initial_state before create, even if skipping validations" do
|
||||
expect(simple.status).to eq(nil)
|
||||
simple.save(:validate => false)
|
||||
expect(simple.status).to eq('unknown_scope')
|
||||
end
|
||||
end
|
||||
|
||||
describe "complex example" do
|
||||
it "works" do
|
||||
record = ComplexMongoMapperExample.new
|
||||
expect(record.aasm(:left).current_state).to eql :one
|
||||
expect(record.left).to be_nil
|
||||
expect(record.aasm(:right).current_state).to eql :alpha
|
||||
expect(record.right).to be_nil
|
||||
|
||||
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 "--------------------------------------------------------------------------"
|
||||
puts "Not running MongoMapper specs because mongo_mapper gem is not installed!!!"
|
||||
puts "--------------------------------------------------------------------------"
|
||||
end
|
||||
end
|
|
@ -84,6 +84,8 @@ describe 'mongo_mapper' do
|
|||
end
|
||||
|
||||
rescue LoadError
|
||||
puts "Not running MongoMapper specs because mongo_mapper gem is not installed!!!"
|
||||
puts "--------------------------------------------------------------------------"
|
||||
puts "Not running MongoMapper multiple-specs because mongo_mapper gem is not installed!!!"
|
||||
puts "--------------------------------------------------------------------------"
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue