diff --git a/Gemfile b/Gemfile index 95c7aea..e487ce0 100644 --- a/Gemfile +++ b/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 diff --git a/lib/aasm/persistence/mongo_mapper_persistence.rb b/lib/aasm/persistence/mongo_mapper_persistence.rb index 2dc41d7..9199b1f 100644 --- a/lib/aasm/persistence/mongo_mapper_persistence.rb +++ b/lib/aasm/persistence/mongo_mapper_persistence.rb @@ -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 diff --git a/spec/models/mongo_mapper/complex_mongo_mapper_example.rb b/spec/models/mongo_mapper/complex_mongo_mapper_example.rb new file mode 100644 index 0000000..efb1e5b --- /dev/null +++ b/spec/models/mongo_mapper/complex_mongo_mapper_example.rb @@ -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 diff --git a/spec/models/mongo_mapper/no_scope_mongo_mapper.rb b/spec/models/mongo_mapper/no_scope_mongo_mapper.rb index 7d7454a..d79d3f8 100644 --- a/spec/models/mongo_mapper/no_scope_mongo_mapper.rb +++ b/spec/models/mongo_mapper/no_scope_mongo_mapper.rb @@ -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 diff --git a/spec/models/mongo_mapper/simple_mongo_mapper.rb b/spec/models/mongo_mapper/simple_mongo_mapper.rb index 0df4f14..b8bdc56 100644 --- a/spec/models/mongo_mapper/simple_mongo_mapper.rb +++ b/spec/models/mongo_mapper/simple_mongo_mapper.rb @@ -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 diff --git a/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb b/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb index 5d047a2..87bd7b5 100644 --- a/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb +++ b/spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb @@ -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 diff --git a/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb b/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb new file mode 100644 index 0000000..90fc0a1 --- /dev/null +++ b/spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb @@ -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 diff --git a/spec/unit/persistence/mongo_mapper_persistence_spec.rb b/spec/unit/persistence/mongo_mapper_persistence_spec.rb index e33a2d2..89f3057 100644 --- a/spec/unit/persistence/mongo_mapper_persistence_spec.rb +++ b/spec/unit/persistence/mongo_mapper_persistence_spec.rb @@ -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