diff --git a/lib/aasm/persistence.rb b/lib/aasm/persistence.rb
index 3efe50e..945fd6a 100644
--- a/lib/aasm/persistence.rb
+++ b/lib/aasm/persistence.rb
@@ -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)
diff --git a/lib/aasm/persistence/active_record_persistence.rb b/lib/aasm/persistence/active_record_persistence.rb
index a6746d1..1b58bd0 100644
--- a/lib/aasm/persistence/active_record_persistence.rb
+++ b/lib/aasm/persistence/active_record_persistence.rb
@@ -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 aasm_current_state
- #
- # 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
diff --git a/lib/aasm/persistence/mongoid_persistence.rb b/lib/aasm/persistence/mongoid_persistence.rb
index 9b3ed4a..1365644 100644
--- a/lib/aasm/persistence/mongoid_persistence.rb
+++ b/lib/aasm/persistence/mongoid_persistence.rb
@@ -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 aasm_current_state
- #
- # 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
diff --git a/lib/aasm/persistence/read_state.rb b/lib/aasm/persistence/read_state.rb
new file mode 100644
index 0000000..5df77b0
--- /dev/null
+++ b/lib/aasm/persistence/read_state.rb
@@ -0,0 +1,40 @@
+module AASM
+ module Persistence
+ module ReadState
+
+ # Returns the value of the aasm_column - called from aasm_current_state
+ #
+ # 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
diff --git a/spec/unit/persistence/active_record_persistence_spec.rb b/spec/unit/persistence/active_record_persistence_spec.rb
index acd3436..2d640f2 100644
--- a/spec/unit/persistence/active_record_persistence_spec.rb
+++ b/spec/unit/persistence/active_record_persistence_spec.rb
@@ -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