Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 3f37bc767449686c81587f82424058f5d12607f4 @bowsersenior committed Oct 9, 2010
@@ -0,0 +1,2 @@
+---
+BUNDLE_DISABLE_SHARED_GEMS: "1"
@@ -0,0 +1,7 @@
+tmp
+nbproject
+pkg
+doc/
+html/
+*.swp
+.yardoc
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
@@ -0,0 +1,72 @@
+PATH
+ remote: .
+ specs:
+ workflow_on_mongoid (0.1)
+ mongoid (~> 2.0.0.beta.19)
+ workflow (~> 0.7)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ activemodel (3.0.0)
+ activesupport (= 3.0.0)
+ builder (~> 2.1.2)
+ i18n (~> 0.4.1)
+ activerecord (3.0.0)
+ activemodel (= 3.0.0)
+ activesupport (= 3.0.0)
+ arel (~> 1.0.0)
+ tzinfo (~> 0.3.23)
+ activesupport (3.0.0)
+ arel (1.0.1)
+ activesupport (~> 3.0.0)
+ bson (1.0.4)
+ builder (2.1.2)
+ columnize (0.3.1)
+ diff-lcs (1.1.2)
+ i18n (0.4.1)
+ linecache (0.43)
+ mocha (0.9.8)
+ rake
+ mongo (1.0.7)
+ bson (>= 1.0.4)
+ mongoid (2.0.0.beta.18)
+ activemodel (~> 3.0.0)
+ bson (= 1.0.4)
+ mongo (= 1.0.7)
+ tzinfo (~> 0.3.22)
+ will_paginate (~> 3.0.pre)
+ rake (0.8.7)
+ rspec (2.0.0.rc)
+ rspec-core (= 2.0.0.rc)
+ rspec-expectations (= 2.0.0.rc)
+ rspec-mocks (= 2.0.0.rc)
+ rspec-core (2.0.0.rc)
+ rspec-expectations (2.0.0.rc)
+ diff-lcs (>= 1.1.2)
+ rspec-mocks (2.0.0.rc)
+ rspec-core (= 2.0.0.rc)
+ rspec-expectations (= 2.0.0.rc)
+ ruby-debug (0.10.3)
+ columnize (>= 0.1)
+ ruby-debug-base (~> 0.10.3.0)
+ ruby-debug-base (0.10.3)
+ linecache (>= 0.3)
+ sqlite3-ruby (1.3.1)
+ tzinfo (0.3.23)
+ will_paginate (3.0.pre2)
+ workflow (0.7.0)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ activerecord
+ mocha
+ mongoid (~> 2.0.0.beta.19)
+ rake (= 0.8.7)
+ rspec (~> 2.0.0.rc)
+ ruby-debug
+ sqlite3-ruby
+ workflow (~> 0.7)
+ workflow_on_mongoid!
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Mani Tadayon
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,2 @@
+workflow_on_mongoid lets you use the [Workflow](http://github.com/geekq/workflow) gem with your Mongoid documents to add state machine functionality.
+-----------------
@@ -0,0 +1,22 @@
+require 'bundler'
+Bundler.setup(:default, :development)
+
+require "rake"
+require "rake/rdoctask"
+require 'rake/testtask'
+
+# require "rspec"
+# require "rspec/core/rake_task"
+
+$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
+require "workflow_on_mongoid/version"
+
+desc 'Default: run all tests.'
+task :default => :test
+
+desc "Test the workflow_on_mongoid plugin."
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.test_files = Dir["test/*_test.rb"]
+ t.verbose = true
+end
@@ -0,0 +1 @@
+require 'workflow_on_mongoid/workflow'
@@ -0,0 +1,4 @@
+# encoding: utf-8
+module WorkflowOnMongoid #:nodoc
+ VERSION = "0.1"
+end
@@ -0,0 +1,33 @@
+module Workflow
+ module MongoidInstanceMethods
+ def load_workflow_state
+ send(self.class.workflow_column)
+ end
+
+ def persist_workflow_state(new_value)
+ self.update_attributes!(self.class.workflow_column => new_value)
+ end
+
+ private
+ def write_initial_state
+ update_attributes(self.class.workflow_column => current_state.to_s) if load_workflow_state.blank?
+ end
+ end
+
+ def self.included(klass)
+ klass.send :include, WorkflowInstanceMethods
+ klass.extend WorkflowClassMethods
+ if Object.const_defined?(:ActiveRecord) && klass < ActiveRecord::Base
+ klass.send :include, ActiveRecordInstanceMethods
+ klass.before_validation :write_initial_state
+ elsif Object.const_defined?(:Remodel) && klass < Remodel::Entity
+ klass.send :include, RemodelInstanceMethods
+ elsif Object.const_defined?(:Mongoid) && klass < Mongoid::Document
+ klass.class_eval do
+ field klass.workflow_column
+ include MongoidInstanceMethods
+ klass.before_validation :write_initial_state
+ end
+ end
+ end
+end
@@ -0,0 +1,49 @@
+# require File.join(File.dirname(__FILE__), 'test_helper')
+
+require 'test_helper'
+
+require 'couchtiny'
+require 'couchtiny/document'
+require 'workflow'
+
+class User < CouchTiny::Document
+ include Workflow
+ workflow do
+ state :submitted do
+ event :activate_via_link, :transitions_to => :proved_email
+ end
+ state :proved_email
+ end
+
+ def load_workflow_state
+ self[:workflow_state]
+ end
+
+ def persist_workflow_state(new_value)
+ self[:workflow_state] = new_value
+ save!
+ end
+end
+
+
+class CouchtinyExample < Test::Unit::TestCase
+
+ def setup
+ db = CouchTiny::Database.url("http://127.0.0.1:5984/test-workflow")
+ db.delete_database! rescue nil
+ db.create_database!
+ User.use_database db
+ end
+
+ test 'CouchDB persistence' do
+ user = User.new :email => 'manya@example.com'
+ user.save!
+ assert user.submitted?
+ user.activate_via_link!
+ assert user.proved_email?
+
+ reloaded_user = User.get user.id
+ puts reloaded_user.inspect
+ assert reloaded_user.proved_email?, 'Reloaded user should have the desired workflow state'
+ end
+end
Oops, something went wrong.

0 comments on commit 3f37bc7

Please sign in to comment.