dry up read state (for persistence)

This commit is contained in:
Thorsten Böttger 2012-11-05 21:48:44 +13:00
parent 4142db83d6
commit f2eeb2e4dc
5 changed files with 47 additions and 77 deletions

View File

@ -6,6 +6,7 @@ module AASM
# Use a fancier auto-loading thingy, perhaps. When there are more persistence engines.
hierarchy = base.ancestors.map {|klass| klass.to_s}
require File.join(File.dirname(__FILE__), 'persistence', 'read_state')
if hierarchy.include?("ActiveRecord::Base")
require File.join(File.dirname(__FILE__), 'persistence', 'active_record_persistence')
base.send(:include, AASM::Persistence::ActiveRecordPersistence)

View File

@ -34,7 +34,7 @@ module AASM
def self.included(base)
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
base.send(:include, AASM::Persistence::ReadState) unless base.method_defined?(:aasm_read_state)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteState) unless base.method_defined?(:aasm_write_state)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)
@ -203,41 +203,6 @@ module AASM
end
end
module ReadState
# Returns the value of the aasm_column - called from <tt>aasm_current_state</tt>
#
# If it's a new record, and the aasm state column is blank it returns the initial state:
#
# class Foo < ActiveRecord::Base
# include AASM
# aasm_column :status
# aasm_state :opened
# aasm_state :closed
# end
#
# foo = Foo.new
# foo.current_state # => :opened
# foo.close
# foo.current_state # => :closed
#
# foo = Foo.find(1)
# foo.current_state # => :opened
# foo.aasm_state = nil
# foo.current_state # => nil
#
# NOTE: intended to be called from an event
#
# This allows for nil aasm states - be sure to add validation to your model
def aasm_read_state
if new_record?
send(self.class.aasm_column).blank? ? aasm_determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
else
send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
end
end
end
end
end
end

View File

@ -36,7 +36,7 @@ module AASM
def self.included(base)
base.extend AASM::Persistence::MongoidPersistence::ClassMethods
base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::MongoidPersistence::ReadState) unless base.method_defined?(:aasm_read_state)
base.send(:include, AASM::Persistence::ReadState) unless base.method_defined?(:aasm_read_state)
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)
@ -200,42 +200,6 @@ module AASM
end
end
module ReadState
# Returns the value of the aasm_column - called from <tt>aasm_current_state</tt>
#
# If it's a new record, and the aasm state column is blank it returns the initial state:
#
# class Foo
# include Mongoid::Document
# include AASM
# aasm_column :status
# aasm_state :opened
# aasm_state :closed
# end
#
# foo = Foo.new
# foo.current_state # => :opened
# foo.close
# foo.current_state # => :closed
#
# foo = Foo.find(1)
# foo.current_state # => :opened
# foo.aasm_state = nil
# foo.current_state # => nil
#
# NOTE: intended to be called from an event
#
# This allows for nil aasm states - be sure to add validation to your model
def aasm_read_state
if new_record?
send(self.class.aasm_column).blank? ? aasm_determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
else
send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
end
end
end
module NamedScopeMethods
def aasm_state_with_named_scope name, options = {}
aasm_state_without_named_scope name, options

View File

@ -0,0 +1,40 @@
module AASM
module Persistence
module ReadState
# Returns the value of the aasm_column - called from <tt>aasm_current_state</tt>
#
# If it's a new record, and the aasm state column is blank it returns the initial state
# (example provided here for ActiveRecord, but it's true for Mongoid as well):
#
# class Foo < ActiveRecord::Base
# include AASM
# aasm_column :status
# aasm_state :opened
# aasm_state :closed
# end
#
# foo = Foo.new
# foo.current_state # => :opened
# foo.close
# foo.current_state # => :closed
#
# foo = Foo.find(1)
# foo.current_state # => :opened
# foo.aasm_state = nil
# foo.current_state # => nil
#
# NOTE: intended to be called from an event
#
# This allows for nil aasm states - be sure to add validation to your model
def aasm_read_state
if new_record?
send(self.class.aasm_column).blank? ? aasm_determine_state_name(self.class.aasm_initial_state) : send(self.class.aasm_column).to_sym
else
send(self.class.aasm_column).nil? ? nil : send(self.class.aasm_column).to_sym
end
end
end # ReadState
end # Persistence
end # AASM

View File

@ -19,7 +19,7 @@ describe "class methods for classes without own read or write state" do
let(:klass) {Gate}
it_should_behave_like "aasm model"
it "should include all persistence mixins" do
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
@ -29,7 +29,7 @@ describe "class methods for classes with own read state" do
let(:klass) {Reader}
it_should_behave_like "aasm model"
it "should include all persistence mixins but read state" do
klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
klass.included_modules.should_not be_include(AASM::Persistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
@ -39,7 +39,7 @@ describe "class methods for classes with own write state" do
let(:klass) {Writer}
it_should_behave_like "aasm model"
it "should include include all persistence mixins but write state" do
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ReadState)
klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
@ -49,7 +49,7 @@ describe "class methods for classes without persistence" do
let(:klass) {Transient}
it_should_behave_like "aasm model"
it "should include all mixins but persistence" do
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ReadState)
klass.included_modules.should be_include(AASM::Persistence::ActiveRecordPersistence::WriteState)
klass.included_modules.should_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end