Skip to content

Commit

Permalink
Merge 4a7a87b into 123700c
Browse files Browse the repository at this point in the history
  • Loading branch information
reidmorrison committed Feb 14, 2015
2 parents 123700c + 4a7a87b commit c4596ff
Show file tree
Hide file tree
Showing 17 changed files with 425 additions and 10 deletions.
7 changes: 5 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@ rvm:
- 2.1
- 2.2
# - jruby-18mode # JRuby in 1.8 mode
- jruby-19mode # JRuby in 1.9 mode
- jruby-1.7 # JRuby in 1.9 mode
- rbx-2.2.1

services: mongodb

gemfile:
- gemfiles/rails_3.2.gemfile
- gemfiles/rails_4.0.gemfile
- gemfiles/rails_4.0_bson1.gemfile
- gemfiles/rails_4.1.gemfile
- gemfiles/rails_4.1_bson1.gemfile
- gemfiles/rails_4.2.gemfile
- gemfiles/rails_4.2_bson1.gemfile

matrix:
allow_failures:
- rvm: rbx-2.2.1
- rvm: jruby-19mode
- rvm: jruby-1.7
exclude:
- rvm: 1.9.3
gemfile: gemfiles/rails_4.1.gemfile
Expand Down
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ gem 'rubysl', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "~>4.1"
gem 'mongoid'
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
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

gemspec
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ 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) and
[Mongoid](http://mongoid.org/), but it can be used for any Ruby class, no matter what
[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
parent class it has (if any).

## Upgrade from version 3 to 4
Expand Down Expand Up @@ -435,7 +435,7 @@ to ```false```.

### Sequel

AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_ and _Mongoid_.
AASM also supports [Sequel](http://sequel.jeremyevans.net/) besides _ActiveRecord_, _Mongoid_, and _MongoMapper_.

```ruby
class Job < Sequel::Model
Expand Down Expand Up @@ -466,6 +466,23 @@ 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
```

### Automatic Scopes

AASM will automatically create scope methods for each state in the model.
Expand Down
4 changes: 3 additions & 1 deletion gemfiles/rails_3.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "3.2.21"
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'mongoid', '~>3.1' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'sequel'
gem 'mongo_mapper', '~>0.13'
gem 'bson_ext', :platforms => :ruby

gemspec :path => "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.0.13"
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'sequel'

gemspec :path => "../"
14 changes: 14 additions & 0 deletions gemfiles/rails_4.0_bson1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"

gem "sqlite3", :platforms => :ruby
gem "coveralls"
gem 'rubysl', :platforms => :rbx
gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.0.13"
gem 'sequel'
gem 'mongo_mapper', '~>0.13'
gem 'bson_ext', :platforms => :ruby

gemspec :path => "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.1.9"
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'sequel'

gemspec :path => "../"
14 changes: 14 additions & 0 deletions gemfiles/rails_4.1_bson1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"

gem "sqlite3", :platforms => :ruby
gem "coveralls"
gem 'rubysl', :platforms => :rbx
gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.1.9"
gem 'sequel'
gem 'mongo_mapper', '~> 0.13'
gem 'bson_ext', :platforms => :ruby

gemspec :path => "../"
2 changes: 1 addition & 1 deletion gemfiles/rails_4.2.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.2.0"
gem 'mongoid' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'mongoid', '~>4.0' if Gem::Version.create(RUBY_VERSION.dup) >= Gem::Version.create('1.9.3')
gem 'sequel'

gemspec :path => "../"
14 changes: 14 additions & 0 deletions gemfiles/rails_4.2_bson1.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
source "https://rubygems.org"

gem "sqlite3", :platforms => :ruby
gem "coveralls"
gem 'rubysl', :platforms => :rbx
gem 'rubinius-developer_tools', :platforms => :rbx
gem "jruby-openssl", :platforms => :jruby
gem "activerecord-jdbcsqlite3-adapter", :platforms => :jruby
gem "rails", "4.2.0"
gem 'sequel'
gem 'mongo_mapper'
gem 'bson_ext', :platforms => :ruby

gemspec :path => "../"
2 changes: 2 additions & 0 deletions lib/aasm/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ def load_persistence(base)
include_persistence base, :active_record
elsif hierarchy.include?("Mongoid::Document")
include_persistence base, :mongoid
elsif hierarchy.include?("MongoMapper::Document")
include_persistence base, :mongo_mapper
elsif hierarchy.include?("Sequel::Model")
include_persistence base, :sequel
else
Expand Down
3 changes: 3 additions & 0 deletions lib/aasm/persistence/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ def state_with_scope(name, *args)
elsif @klass.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
scope_options = lambda { @klass.send(:where, {@klass.aasm.attribute_name.to_sym => name.to_s}) }
@klass.send(:scope, name, scope_options)
elsif @klass.ancestors.map {|klass| klass.to_s}.include?("MongoMapper::Document")
conditions = { @klass.aasm.attribute_name.to_sym => name.to_s }
@klass.scope(name, lambda { @klass.where(conditions) })
end
end
end
Expand Down
174 changes: 174 additions & 0 deletions lib/aasm/persistence/mongo_mapper_persistence.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
require_relative 'base'

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.extend AASM::Persistence::MongoMapperPersistence::ClassMethods
base.send(:include, AASM::Persistence::MongoMapperPersistence::InstanceMethods)

base.before_create :aasm_ensure_initial_state

# ensure state is in the list of states
base.validate :aasm_validate_states
end

module ClassMethods

def find_in_state(number, state, *args)
with_state_scope(state).find!(number, *args)
end

def count_in_state(state, *args)
with_state_scope(state).count(*args)
end

def calculate_in_state(state, *args)
with_state_scope(state).calculate(*args)
end

protected
def with_state_scope(state)
where(aasm.attribute_name.to_sym => state.to_s)
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)
old_value = read_attribute(self.class.aasm.attribute_name)
aasm_write_attribute 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
else
self.save
end
unless success
write_attribute(self.class.aasm.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)
aasm_write_attribute state
end

private
def aasm_enum
case AASM::StateMachine[self.class].config.enum
when false then nil
when true then aasm_guess_enum_method
when nil then aasm_guess_enum_method if aasm_column_looks_like_enum
else AASM::StateMachine[self.class].config.enum
end
end

def aasm_column_looks_like_enum
self.class.keys[self.class.aasm.attribute_name.to_s].type == Integer
end

def aasm_guess_enum_method
self.class.aasm.attribute_name.to_s.pluralize.to_sym
end

def aasm_skipping_validations
AASM::StateMachine[self.class].config.skip_validation_on_save
end

def aasm_write_attribute(state)
write_attribute self.class.aasm.attribute_name, aasm_raw_attribute_value(state)
end

def aasm_raw_attribute_value(state)
if aasm_enum
self.class.send(aasm_enum)[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
return send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) if send(self.class.aasm.attribute_name).blank?
end

def aasm_validate_states
send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) if send(self.class.aasm.attribute_name).blank?
unless AASM::StateMachine[self.class].config.skip_validation_on_save
if aasm.current_state && !aasm.states.include?(aasm.current_state)
self.errors.add(AASM::StateMachine[self.class].config.column , "is invalid")
end
end
end
end # InstanceMethods

end
end # Persistence
end # AASM
10 changes: 10 additions & 0 deletions spec/models/mongo_mapper/no_scope_mongo_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class NoScopeMongoMapper
include MongoMapper::Document
include AASM

key :status, String

aasm :create_scopes => false, :column => :status do
state :ignored_scope
end
end
11 changes: 11 additions & 0 deletions spec/models/mongo_mapper/simple_mongo_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class SimpleMongoMapper
include MongoMapper::Document
include AASM

key :status, String

aasm column: :status do
state :unknown_scope
state :next
end
end
12 changes: 12 additions & 0 deletions spec/models/mongo_mapper/simple_new_dsl_mongo_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class SimpleNewDslMongoMapper
include MongoMapper::Document
include AASM

key :status, String

aasm :column => :status
aasm do
state :unknown_scope
state :next
end
end

0 comments on commit c4596ff

Please sign in to comment.