1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

Added autocreation of state scopes for Mongoid, including RSpec tests. The interface mirrors the ActiveRecord support for the same.

Summary of changes
- Added the following methods to persistence/mongoid_persistence.rb
  - find_in_state
  - count_in_state
  - with_state_scope (protected in AR, would argue it should be made public there.
  - I did not port calculate_in_state, due to AR vs. Mongoid API differences; I made with_state_scope public instead. (I think it should be public for AR as well.)
- Moved AASM::Base patch which aliases scopes in persistence/active_record_persistence.rb to persistence/base.rb, and added a Mongoid detection clause to the if statement (in addition to AR). Also deleted commented code from the original method.
- Added 'mongoid' as dev dependency to gemspec
- Created two test files (mongoid_persistance_spec.rb and mongoid_models.rb) covering all my changes.
This commit is contained in:
Johnny Shields 2013-01-07 07:29:35 +09:00
parent af00b990f5
commit 13e7090d40
6 changed files with 188 additions and 30 deletions

View file

@ -14,6 +14,7 @@ Gem::Specification.new do |s|
s.licenses = ["MIT"]
s.add_development_dependency 'activerecord'
s.add_development_dependency 'mongoid'
s.add_development_dependency 'rake'
s.add_development_dependency 'sdoc'
s.add_development_dependency 'rspec', '~> 2.0'

View file

@ -172,19 +172,3 @@ module AASM
end
end
end
class AASM::Base
def state_with_scope(name, *args)
state_without_scope(name, *args)
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base") && !@clazz.respond_to?(name)
# puts "setting scope #{@clazz.name}.#{name}"
scope_options = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}}
scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope
@clazz.send(scope_method, name, scope_options)
# else
# puts "not setting scope #{@clazz.name}.#{name}"
end
end
alias_method :state_without_scope, :state
alias_method :state, :state_with_scope
end

View file

@ -43,4 +43,23 @@ module AASM
end # Base
end # Persistence
class Base
def state_with_scope(name, *args)
state_without_scope(name, *args)
unless @clazz.respond_to?(name)
if @clazz.ancestors.map {|klass| klass.to_s}.include?("ActiveRecord::Base")
scope_options = {:conditions => { "#{@clazz.table_name}.#{@clazz.aasm_column}" => name.to_s}}
scope_method = ActiveRecord::VERSION::MAJOR >= 3 ? :scope : :named_scope
@clazz.send(scope_method, name, scope_options)
elsif @clazz.ancestors.map {|klass| klass.to_s}.include?("Mongoid::Document")
scope_options = -> { @clazz.send(:where, {@clazz.aasm_column.to_sym => name.to_s}) }
@clazz.send(:scope, name, scope_options)
end
end
end
alias_method :state_without_scope, :state
alias_method :state, :state_with_scope
end # Base
end # AASM

View file

@ -41,25 +41,31 @@ module AASM
base.send(:include, AASM::Persistence::MongoidPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
base.send(:include, AASM::Persistence::MongoidPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
# if base.respond_to?(:named_scope)
# base.extend(AASM::Persistence::MongoidPersistence::NamedScopeMethods)
#
# base.class_eval do
# class << self
# unless method_defined?(:aasm_state_without_named_scope)
# alias_method :aasm_state_without_named_scope, :aasm_state
# alias_method :aasm_state, :aasm_state_with_named_scope
# end
# end
# end
# end
# Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
# base.before_validation_on_create :aasm_ensure_initial_state
base.before_validation :aasm_ensure_initial_state
end
module ClassMethods
def find_in_state(number, state, *args)
with_state_scope state do
find(number, *args)
end
end
def count_in_state(state, *args)
with_state_scope state do
count(*args)
end
end
def with_state_scope(state)
with_scope where(aasm_column.to_sym => state.to_s) do
yield if block_given?
end
end
end
module InstanceMethods
@ -155,4 +161,4 @@ module AASM
end
end
end
end
end

View file

@ -0,0 +1,23 @@
class SimpleMongoid
include Mongoid::Document
include AASM
field :status, type: String
aasm_column :status
aasm_state :unknown_scope
aasm_state :new
end
class SimpleNewDslMongoid
include Mongoid::Document
include AASM
field :status, type: String
aasm :column => :status
aasm do
state :unknown_scope
state :new
end
end

View file

@ -0,0 +1,125 @@
require 'mongoid'
require 'logger'
require 'spec_helper'
require File.dirname(__FILE__) + '/../../models/mongoid/mongoid_models'
# if you want to see the statements while running the spec enable the following line
# Mongoid.logger = Logger.new(STDERR)
describe Mongoid do
before(:all) do
DATABASE_NAME = "mongoid_#{Process.pid}"
Mongoid.configure do |config|
config.connect_to DATABASE_NAME
end
end
after do
Mongoid.purge!
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
SimpleMongoid.should respond_to(:unknown_scope)
SimpleMongoid.unknown_scope.class.should == Mongoid::Criteria
end
end
context "Already respond_to? the scope name" do
it "should not add a scope" do
SimpleMongoid.should respond_to(:new)
SimpleMongoid.new.class.should == SimpleMongoid
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
SimpleNewDslMongoid.should respond_to(:unknown_scope)
SimpleNewDslMongoid.unknown_scope.class.should == Mongoid::Criteria
end
end
context "Already respond_to? the scope name" do
it "should not add a scope" do
SimpleNewDslMongoid.should respond_to(:new)
SimpleNewDslMongoid.new.class.should == SimpleNewDslMongoid
end
end
end
describe "#find_in_state" do
let!(:model) { SimpleNewDslMongoid.create!(status: :unknown_scope) }
let!(:model_id) { model._id }
it "should respond to method" do
SimpleNewDslMongoid.should respond_to(:find_in_state)
end
it "should find the model when given the correct scope and model id" do
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').class.should == SimpleNewDslMongoid
SimpleNewDslMongoid.find_in_state(model_id, 'unknown_scope').should == model
end
it "should raise DocumentNotFound error when given incorrect scope" do
expect {SimpleNewDslMongoid.find_in_state(model_id, 'new')}.to raise_error Mongoid::Errors::DocumentNotFound
end
it "should raise DocumentNotFound error when given incorrect model id" do
expect {SimpleNewDslMongoid.find_in_state('bad_id', 'unknown_scope')}.to raise_error Mongoid::Errors::DocumentNotFound
end
end
describe "#count_in_state" do
before do
3.times { SimpleNewDslMongoid.create!(status: :unknown_scope) }
end
it "should respond to method" do
SimpleNewDslMongoid.should respond_to(:count_in_state)
end
it "should return n for a scope with n records persisted" do
SimpleNewDslMongoid.count_in_state('unknown_scope').class.should == Fixnum
SimpleNewDslMongoid.count_in_state('unknown_scope').should == 3
end
it "should return zero for a scope without records persisted" do
SimpleNewDslMongoid.count_in_state('new').class.should == Fixnum
SimpleNewDslMongoid.count_in_state('new').should == 0
end
end
describe "#with_state_scope" do
before do
3.times { SimpleNewDslMongoid.create!(status: :unknown_scope) }
2.times { SimpleNewDslMongoid.create!(status: :new) }
end
it "should respond to method" do
SimpleNewDslMongoid.should respond_to(:with_state_scope)
end
it "should correctly process block" do
SimpleNewDslMongoid.with_state_scope('unknown_scope') do
SimpleNewDslMongoid.count
end.should == 3
SimpleNewDslMongoid.with_state_scope('new') do
SimpleNewDslMongoid.count
end.should == 2
end
end
end