mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Drop Support for Mongo Mapper (#439)
* Drop support for Mongo Mapper. * Refactor Appraisal
This commit is contained in:
parent
77cd065eeb
commit
ceb05f746b
23 changed files with 29 additions and 717 deletions
24
.travis.yml
24
.travis.yml
|
@ -14,13 +14,10 @@ services:
|
|||
- redis-server
|
||||
|
||||
gemfile:
|
||||
- gemfiles/rails_3.2_stable.gemfile
|
||||
- gemfiles/rails_3.2.gemfile
|
||||
- gemfiles/rails_4.0.gemfile
|
||||
- gemfiles/rails_4.0_mongo_mapper.gemfile
|
||||
- gemfiles/rails_4.2.gemfile
|
||||
- gemfiles/rails_4.2_mongoid_4.gemfile
|
||||
- gemfiles/rails_4.2_mongoid_5.gemfile
|
||||
- gemfiles/rails_4.2_mongo_mapper.gemfile
|
||||
- gemfiles/rails_5.0.gemfile
|
||||
|
||||
before_script:
|
||||
|
@ -30,28 +27,21 @@ before_script:
|
|||
- mongod --version
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- rvm: jruby-9.0.5.0
|
||||
gemfile: gemfiles/rails_5.0.gemfile
|
||||
- rvm: rbx-2.5.8
|
||||
gemfile: gemfiles/rails_5.0.gemfile
|
||||
exclude:
|
||||
- rvm: 1.9.3
|
||||
gemfile: gemfiles/rails_4.2_mongoid_4.gemfile
|
||||
- rvm: 1.9.3
|
||||
gemfile: gemfiles/rails_4.2_mongoid_5.gemfile
|
||||
- rvm: 1.9.3
|
||||
gemfile: gemfiles/rails_5.0.gemfile
|
||||
- rvm: 2.2.5
|
||||
gemfile: gemfiles/rails_3.2_stable.gemfile
|
||||
- rvm: 2.3.1
|
||||
gemfile: gemfiles/rails_3.2_stable.gemfile
|
||||
- rvm: jruby-1.7
|
||||
gemfile: gemfiles/rails_4.2_mongoid_4.gemfile
|
||||
- rvm: 2.2.6
|
||||
gemfile: gemfiles/rails_3.2.gemfile
|
||||
- rvm: 2.3.3
|
||||
gemfile: gemfiles/rails_3.2.gemfile
|
||||
- rvm: jruby-1.7
|
||||
gemfile: gemfiles/rails_4.2_mongoid_5.gemfile
|
||||
- rvm: jruby-1.7
|
||||
gemfile: gemfiles/rails_5.0.gemfile
|
||||
- rvm: jruby-9.0.5.0
|
||||
gemfile: gemfiles/rails_5.0.gemfile
|
||||
|
||||
notifications:
|
||||
slack:
|
||||
|
|
85
Appraisals
85
Appraisals
|
@ -1,23 +1,12 @@
|
|||
appraise 'rails_3.2_stable' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'rails', :github => 'rails/rails', :branch => '3-2-stable'
|
||||
appraise 'rails_3.2' do
|
||||
gem 'rails', '~>3.2.22'
|
||||
gem 'mongoid', '~>3.1'
|
||||
gem 'sequel'
|
||||
gem 'mongo_mapper', '~>0.13'
|
||||
gem 'bson_ext', :platforms => :ruby
|
||||
gem 'test-unit', '~> 3.0'
|
||||
end
|
||||
|
||||
appraise 'rails_4.0' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.0.13'
|
||||
gem 'mongoid', '~>4.0'
|
||||
|
@ -27,87 +16,25 @@ appraise 'rails_4.0' do
|
|||
gem 'redis-objects'
|
||||
end
|
||||
|
||||
appraise 'rails_4.0_mongo_mapper' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.0.13'
|
||||
gem 'sequel'
|
||||
gem 'mongo_mapper', '~>0.13.1'
|
||||
gem 'bson_ext', :platforms => :ruby
|
||||
gem 'dynamoid', '~> 1', :platforms => :ruby
|
||||
gem 'aws-sdk', '~>2', :platforms => :ruby
|
||||
end
|
||||
|
||||
appraise 'rails_4.2' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'nokogiri', '1.6.8.1', :platforms => [:ruby_19]
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.2.5'
|
||||
gem 'sequel'
|
||||
gem 'dynamoid', '~> 1', :platforms => :ruby
|
||||
gem 'aws-sdk', '~>2', :platforms => :ruby
|
||||
end
|
||||
|
||||
appraise 'rails_4.2_mongoid_4' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.2.5'
|
||||
gem 'mongoid', '~>4.0', :platforms => [:ruby_22, :ruby_23, :jruby]
|
||||
gem 'mongoid', '~>4.0'
|
||||
gem 'sequel'
|
||||
gem 'dynamoid', '~> 1', :platforms => :ruby
|
||||
gem 'aws-sdk', '~>2', :platforms => :ruby
|
||||
end
|
||||
|
||||
appraise 'rails_4.2_mongoid_5' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.2.5'
|
||||
gem 'mongoid', '~>5.0', :platforms => [:ruby_22, :ruby_23, :jruby]
|
||||
gem 'sequel'
|
||||
gem 'dynamoid', '~> 1', :platforms => :ruby
|
||||
gem 'aws-sdk', '~>2', :platforms => :ruby
|
||||
end
|
||||
|
||||
appraise 'rails_4.2_mongo_mapper' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'mime-types', '~> 2', :platforms => [:ruby_19, :jruby]
|
||||
gem 'rails', '4.2.5'
|
||||
gem 'sequel'
|
||||
gem 'mongo_mapper'
|
||||
gem 'bson_ext', :platforms => :ruby
|
||||
gem 'dynamoid', '~> 1', :platforms => :ruby
|
||||
gem 'aws-sdk', '~>2', :platforms => :ruby
|
||||
gem 'redis-objects'
|
||||
gem 'mongoid', '~>5.0'
|
||||
end
|
||||
|
||||
appraise 'rails_5.0' do
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'rubysl', :platforms => :rbx
|
||||
gem 'rubinius-developer_tools', :platforms => :rbx
|
||||
gem 'jruby-openssl', :platforms => :jruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'rails', '5.0.0'
|
||||
# mongoid is not yet Rails 5 compatible
|
||||
# gem 'mongoid', '~>4.0', :platforms => [:ruby_22, :ruby_23, :jruby]
|
||||
gem 'mongoid', '~>6.0'
|
||||
gem 'sequel'
|
||||
|
||||
# dynamoid is not yet Rails 5 compatible
|
||||
|
|
5
Gemfile
5
Gemfile
|
@ -2,5 +2,6 @@ source "https://rubygems.org"
|
|||
|
||||
gemspec
|
||||
|
||||
gem "rails", "4.2.7.1"
|
||||
gem "sqlite3"
|
||||
gem 'sqlite3', :platforms => :ruby
|
||||
gem 'activerecord-jdbcsqlite3-adapter', :platforms => :jruby
|
||||
gem 'rails', '5.0.0'
|
||||
|
|
21
README.md
21
README.md
|
@ -10,7 +10,7 @@ This package contains AASM, a library for adding finite state machines to Ruby c
|
|||
AASM started as the *acts_as_state_machine* plugin but has evolved into a more generic library
|
||||
that no longer targets only ActiveRecord models. It currently provides adapters for
|
||||
[ActiveRecord](http://api.rubyonrails.org/classes/ActiveRecord/Base.html),
|
||||
[Mongoid](http://mongoid.org/), and [Mongomapper](http://mongomapper.com/) but it can be used for any Ruby class, no matter what
|
||||
and [Mongoid](http://mongoid.org/) but it can be used for any Ruby class, no matter what
|
||||
parent class it has (if any).
|
||||
|
||||
## Upgrade from version 3 to 4
|
||||
|
@ -677,7 +677,7 @@ to ```false```.
|
|||
|
||||
### Sequel
|
||||
|
||||
AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_, _Mongoid_, and _MongoMapper_.
|
||||
AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_, and _Mongoid_.
|
||||
|
||||
```ruby
|
||||
class Job < Sequel::Model
|
||||
|
@ -713,23 +713,6 @@ class Job
|
|||
end
|
||||
```
|
||||
|
||||
### MongoMapper
|
||||
|
||||
AASM also supports persistence to Mongodb if you're using MongoMapper. Make sure
|
||||
to include MongoMapper::Document before you include AASM.
|
||||
|
||||
```ruby
|
||||
class Job
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :aasm_state, Symbol
|
||||
aasm do
|
||||
...
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
### Redis
|
||||
|
||||
AASM also supports persistence in Redis.
|
||||
|
|
|
@ -18,8 +18,8 @@ Or for specific one:
|
|||
|
||||
Or for one particular test file
|
||||
|
||||
appraisal rails_4.2_mongo_mapper rspec spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb
|
||||
appraisal rails_4.2_mongoid_5 rspec spec/unit/persistence/mongoid_persistence_multiple_spec.rb
|
||||
|
||||
Or down to one test case
|
||||
|
||||
appraisal rails_4.2_mongo_mapper rspec spec/unit/persistence/mongo_mapper_persistence_multiple_spec.rb:92
|
||||
appraisal rails_4.2_mongoid_5 rspec spec/unit/persistence/mongoid_persistence_multiple_spec.rb:92
|
||||
|
|
|
@ -3,14 +3,10 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "rails", :github => "rails/rails", :branch => "3-2-stable"
|
||||
gem "rails", "~>3.2.22"
|
||||
gem "mongoid", "~>3.1"
|
||||
gem "sequel"
|
||||
gem "mongo_mapper", "~>0.13"
|
||||
gem "bson_ext", :platforms => :ruby
|
||||
gem "test-unit", "~> 3.0"
|
||||
|
|
@ -3,12 +3,9 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.0.13"
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "mongoid", "~>4.0"
|
||||
gem "sequel"
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.0.13"
|
||||
gem "sequel"
|
||||
gem "mongo_mapper", "~>0.13.1"
|
||||
gem "bson_ext", :platforms => :ruby
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
|
||||
gemspec :path => "../"
|
|
@ -3,17 +3,13 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "nokogiri", "1.6.8.1", :platforms => [:ruby_19]
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.2.5"
|
||||
gem "nokogiri", "1.6.8.1", :platforms => [:ruby_19]
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "mongoid", "~>4.0"
|
||||
gem "sequel"
|
||||
if ENV['CI'] = 'true'
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
end
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
|
||||
gemspec :path => "../"
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "nokogiri", "1.6.8.1", :platforms => [:ruby_19]
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.2.5"
|
||||
gem "sequel"
|
||||
gem "mongo_mapper"
|
||||
gem "bson_ext", :platforms => :ruby
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
gem "redis-objects"
|
||||
|
||||
gemspec :path => "../"
|
|
@ -1,17 +0,0 @@
|
|||
# This file was generated by Appraisal
|
||||
|
||||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.2.5"
|
||||
gem "mongoid", "~>4.0", :platforms => [:ruby_22, :ruby_23, :jruby]
|
||||
gem "sequel"
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
|
||||
gemspec :path => "../"
|
|
@ -3,15 +3,9 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "rails", "4.2.5"
|
||||
gem "mongoid", "~>5.0", :platforms => [:ruby_22, :ruby_23, :jruby]
|
||||
gem "sequel"
|
||||
gem "dynamoid", "~> 1", :platforms => :ruby
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
gem "mime-types", "~> 2", :platforms => [:ruby_19, :jruby]
|
||||
gem "mongoid", "~>5.0"
|
||||
|
||||
gemspec :path => "../"
|
||||
|
|
|
@ -3,11 +3,9 @@
|
|||
source "https://rubygems.org"
|
||||
|
||||
gem "sqlite3", :platforms => :ruby
|
||||
gem "rubysl", :platforms => :rbx
|
||||
gem "rubinius-developer_tools", :platforms => :rbx
|
||||
gem "jruby-openssl", :platforms => :jruby
|
||||
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
|
||||
gem "rails", "5.0.0"
|
||||
gem "mongoid", "~>6.0"
|
||||
gem "sequel"
|
||||
gem "aws-sdk", "~>2", :platforms => :ruby
|
||||
|
||||
|
|
|
@ -12,9 +12,6 @@ module AASM
|
|||
elsif hierarchy.include?("Mongoid::Document")
|
||||
require_persistence :mongoid
|
||||
include_persistence base, :mongoid
|
||||
elsif hierarchy.include?("MongoMapper::Document")
|
||||
require_persistence :mongo_mapper
|
||||
include_persistence base, :mongo_mapper
|
||||
elsif hierarchy.include?("Sequel::Model")
|
||||
require_persistence :sequel
|
||||
include_persistence base, :sequel
|
||||
|
|
|
@ -1,163 +0,0 @@
|
|||
module AASM
|
||||
module Persistence
|
||||
module MongoMapperPersistence
|
||||
# This method:
|
||||
#
|
||||
# * extends the model with ClassMethods
|
||||
# * includes InstanceMethods
|
||||
#
|
||||
# Adds
|
||||
#
|
||||
# before_validation :aasm_ensure_initial_state, :on => :create
|
||||
#
|
||||
# As a result, it doesn't matter when you define your methods - the following 2 are equivalent
|
||||
#
|
||||
# class Foo
|
||||
# include MongoMapper::Document
|
||||
# def aasm_write_state(state)
|
||||
# "bar"
|
||||
# end
|
||||
# include AASM
|
||||
# end
|
||||
#
|
||||
# class Foo < ActiveRecord::Base
|
||||
# include MongoMapper::Document
|
||||
# include AASM
|
||||
# def aasm_write_state(state)
|
||||
# "bar"
|
||||
# end
|
||||
# end
|
||||
#
|
||||
def self.included(base)
|
||||
base.send(:include, AASM::Persistence::Base)
|
||||
base.send(:include, AASM::Persistence::MongoMapperPersistence::InstanceMethods)
|
||||
base.extend AASM::Persistence::MongoMapperPersistence::ClassMethods
|
||||
|
||||
base.before_create :aasm_ensure_initial_state
|
||||
|
||||
# ensure state is in the list of states
|
||||
base.validate :aasm_validate_states
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def aasm_create_scope(state_machine_name, scope_name)
|
||||
conditions = { aasm(state_machine_name).attribute_name.to_sym => scope_name.to_s }
|
||||
scope(scope_name, lambda { where(conditions) })
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
||||
# Writes <tt>state</tt> to the state column and persists it to the database
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
# foo.aasm.current_state # => :opened
|
||||
# foo.close!
|
||||
# foo.aasm.current_state # => :closed
|
||||
# Foo.find(1).aasm.current_state # => :closed
|
||||
#
|
||||
# NOTE: intended to be called from an event
|
||||
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(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(name).attribute_name, old_value)
|
||||
return false
|
||||
end
|
||||
|
||||
true
|
||||
end
|
||||
|
||||
# Writes <tt>state</tt> to the state column, but does not persist it to the database
|
||||
#
|
||||
# foo = Foo.find(1)
|
||||
# foo.aasm.current_state # => :opened
|
||||
# foo.close
|
||||
# foo.aasm.current_state # => :closed
|
||||
# Foo.find(1).aasm.current_state # => :opened
|
||||
# foo.save
|
||||
# foo.aasm.current_state # => :closed
|
||||
# Foo.find(1).aasm.current_state # => :closed
|
||||
#
|
||||
# NOTE: intended to be called from an event
|
||||
def aasm_write_state_without_persistence(state, name=:default)
|
||||
aasm_write_attribute(state, name)
|
||||
end
|
||||
|
||||
private
|
||||
def aasm_enum(name=:default)
|
||||
case AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum
|
||||
when false then nil
|
||||
when true then aasm_guess_enum_method(name)
|
||||
when nil then aasm_guess_enum_method(name) if aasm_column_looks_like_enum(name)
|
||||
else AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum
|
||||
end
|
||||
end
|
||||
|
||||
def aasm_column_looks_like_enum(name)
|
||||
self.class.keys[self.class.aasm(name).attribute_name.to_s].type == Integer
|
||||
end
|
||||
|
||||
def aasm_guess_enum_method(name)
|
||||
self.class.aasm(name).attribute_name.to_s.pluralize.to_sym
|
||||
end
|
||||
|
||||
def aasm_skipping_validations(state_machine_name)
|
||||
AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save
|
||||
end
|
||||
|
||||
def aasm_write_attribute(state, name=:default)
|
||||
write_attribute self.class.aasm(name).attribute_name, aasm_raw_attribute_value(state, name)
|
||||
end
|
||||
|
||||
def aasm_raw_attribute_value(state, name=:default)
|
||||
if aasm_enum(name)
|
||||
self.class.send(aasm_enum(name))[state]
|
||||
else
|
||||
state.to_s
|
||||
end
|
||||
end
|
||||
|
||||
# Ensures that if the aasm_state column is nil and the record is new
|
||||
# that the initial state gets populated before validation on create
|
||||
#
|
||||
# foo = Foo.new
|
||||
# foo.aasm_state # => nil
|
||||
# foo.valid?
|
||||
# foo.aasm_state # => "open" (where :open is the initial state)
|
||||
#
|
||||
#
|
||||
# foo = Foo.find(:first)
|
||||
# foo.aasm_state # => 1
|
||||
# foo.aasm_state = nil
|
||||
# foo.valid?
|
||||
# foo.aasm_state # => nil
|
||||
#
|
||||
def aasm_ensure_initial_state
|
||||
AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
|
||||
send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s) if send(self.class.aasm(state_machine_name).attribute_name).blank?
|
||||
end
|
||||
end
|
||||
|
||||
def aasm_validate_states
|
||||
AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name|
|
||||
send("#{self.class.aasm(state_machine_name).attribute_name}=", aasm(state_machine_name).enter_initial_state.to_s) if send(self.class.aasm(state_machine_name).attribute_name).blank?
|
||||
unless AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.skip_validation_on_save
|
||||
if aasm(state_machine_name).current_state && !aasm(state_machine_name).states.include?(aasm(state_machine_name).current_state)
|
||||
self.errors.add(AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.column , "is invalid")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end # InstanceMethods
|
||||
|
||||
end
|
||||
end # Persistence
|
||||
end # AASM
|
|
@ -13,7 +13,6 @@ exclude_files = [
|
|||
'aasm/rspec.*',
|
||||
'aasm/persistence/active_record_persistence.rb',
|
||||
'aasm/persistence/dynamoid_persistence.rb',
|
||||
'aasm/persistence/mongo_mapper_persistence.rb',
|
||||
'aasm/persistence/mongoid_persistence.rb',
|
||||
'aasm/persistence/sequel_persistence.rb',
|
||||
'aasm/persistence/redis_persistence.rb'
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
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
|
|
@ -1,21 +0,0 @@
|
|||
class NoScopeMongoMapper
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :create_scopes => false, :column => :status do
|
||||
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
|
|
@ -1,23 +0,0 @@
|
|||
class SimpleMongoMapper
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm column: :status do
|
||||
state :unknown_scope, :another_unknown_scope
|
||||
state :next
|
||||
end
|
||||
end
|
||||
|
||||
class SimpleMongoMapperMultiple
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :left, column: :status do
|
||||
state :unknown_scope, :another_unknown_scope
|
||||
state :next
|
||||
end
|
||||
end
|
|
@ -1,25 +0,0 @@
|
|||
class SimpleNewDslMongoMapper
|
||||
include MongoMapper::Document
|
||||
include AASM
|
||||
|
||||
key :status, String
|
||||
|
||||
aasm :column => :status
|
||||
aasm do
|
||||
state :unknown_scope
|
||||
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
|
|
@ -1,7 +0,0 @@
|
|||
# encoding: utf-8
|
||||
begin
|
||||
require 'mongo_mapper'
|
||||
puts "mongo_mapper gem found, running MongoMapper specs \e[32m#{'✔'}\e[0m"
|
||||
rescue LoadError
|
||||
puts "mongo_mapper gem not found, not running MongoMapper specs \e[31m#{'✖'}\e[0m"
|
||||
end
|
|
@ -1,144 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
if defined?(MongoMapper)
|
||||
describe 'mongo_mapper' do
|
||||
|
||||
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
||||
require File.expand_path(f)
|
||||
end
|
||||
|
||||
before(:all) do
|
||||
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 for each state" do
|
||||
expect(SimpleMongoMapperMultiple).to respond_to(:unknown_scope)
|
||||
expect(SimpleMongoMapperMultiple).to respond_to(:another_unknown_scope)
|
||||
|
||||
expect(SimpleMongoMapperMultiple.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
||||
expect(SimpleMongoMapperMultiple.another_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
|
||||
|
||||
end
|
||||
end
|
|
@ -1,91 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
if defined?(MongoMapper)
|
||||
describe 'mongo_mapper' do
|
||||
|
||||
Dir[File.dirname(__FILE__) + "/../../models/mongo_mapper/*.rb"].sort.each do |f|
|
||||
require File.expand_path(f)
|
||||
end
|
||||
|
||||
before(:all) do
|
||||
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 for each state" do
|
||||
expect(SimpleMongoMapper).to respond_to(:unknown_scope)
|
||||
expect(SimpleMongoMapper).to respond_to(:another_unknown_scope)
|
||||
|
||||
expect(SimpleMongoMapper.unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
||||
expect(SimpleMongoMapper.another_unknown_scope.class).to eq(MongoMapper::Plugins::Querying::DecoratedPluckyQuery)
|
||||
#expect(SimpleMongoMapper.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(SimpleMongoMapper).to respond_to(:next)
|
||||
expect(SimpleMongoMapper.new.class).to eq(SimpleMongoMapper)
|
||||
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(SimpleNewDslMongoMapper).to respond_to(:unknown_scope)
|
||||
expect(SimpleNewDslMongoMapper.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(SimpleNewDslMongoMapper).to respond_to(:next)
|
||||
expect(SimpleNewDslMongoMapper.new.class).to eq(SimpleNewDslMongoMapper)
|
||||
end
|
||||
end
|
||||
|
||||
it "does not create scopes if requested" do
|
||||
expect(NoScopeMongoMapper).not_to respond_to(:ignored_scope)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe "instance methods" do
|
||||
|
||||
let(:simple) {SimpleNewDslMongoMapper.new}
|
||||
|
||||
it "should call aasm_ensure_initial_state on validation before create" do
|
||||
expect(SimpleNewDslMongoMapper.aasm.initial_state).to eq(:unknown_scope)
|
||||
expect(SimpleNewDslMongoMapper.aasm.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
|
||||
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue