Skip to content

Add mongoid support #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ html/
*.rbc
.bundle
.config
.ruby*
.yardoc
Gemfile.lock
InstalledFiles
Expand All @@ -18,3 +19,5 @@ spec/reports
test/tmp
test/version_tmp
tmp

gemfiles/*.lock
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ matrix:

- rvm: 2.1.0
gemfile: gemfiles/Gemfile.rails-3.x

services:
- mongodb
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-3.x
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ group :development do
gem "rdoc", ">= 3.12"
gem "bundler", ">= 1.0.0"
gem "activerecord", "~>3.2"
gem "mongoid"
gem "sqlite3"
gem "mocha"
gem "rake"
Expand Down
1 change: 1 addition & 0 deletions gemfiles/Gemfile.rails-edge
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ source "http://rubygems.org"
group :development do
gem "rdoc", ">= 3.12"
gem "bundler", ">= 1.0.0"
gem "mongoid", "~> 4.0.0.beta"
gem "activerecord"
gem "sqlite3"
gem "mocha"
Expand Down
18 changes: 10 additions & 8 deletions lib/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'workflow/specification'
require 'workflow/adapters/active_record'
require 'workflow/adapters/remodel'
require 'workflow/adapters/mongoid'

# See also README.markdown for documentation
module Workflow
Expand Down Expand Up @@ -262,16 +263,17 @@ def self.included(klass)

klass.extend ClassMethods

if Object.const_defined?(:ActiveRecord)
if klass < ActiveRecord::Base
klass.send :include, Adapter::ActiveRecord::InstanceMethods
klass.send :extend, Adapter::ActiveRecord::Scopes
if Object.const_defined?(:ActiveRecord) && klass < ActiveRecord::Base
klass.send :include, Adapter::ActiveRecord::InstanceMethods
klass.send :extend, Adapter::ActiveRecord::Scopes
klass.before_validation :write_initial_state
elsif Object.const_defined?(:Remodel) && klass < Adapter::Remodel::Entity
klass.send :include, Remodel::InstanceMethods
elsif Object.const_defined?(:Mongoid) && klass < Mongoid::Document
klass.class_eval do
klass.send :include, Adapter::Mongoid::InstanceMethods
klass.before_validation :write_initial_state
end
elsif Object.const_defined?(:Remodel)
if klass < Adapter::Remodel::Entity
klass.send :include, Remodel::InstanceMethods
end
end
end
end
22 changes: 22 additions & 0 deletions lib/workflow/adapters/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Workflow
module Adapter
module Mongoid
module InstanceMethods
def load_workflow_state
send(self.class.workflow_column)
end

def persist_workflow_state(new_value)
self.update_attribute(self.class.workflow_column, new_value)
end

private

def write_initial_state
send("#{self.class.workflow_column}=", current_state.to_s) if load_workflow_state.blank?
end

end
end
end
end
Loading