Skip to content

Commit

Permalink
no need for WriteStateWithoutPersistence mixin anymore (to keep addin…
Browse files Browse the repository at this point in the history
…g persistence simple)
  • Loading branch information
alto committed Apr 24, 2013
1 parent 95ef7fc commit bbafd22
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 81 deletions.
19 changes: 17 additions & 2 deletions API
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
Overwrite method to read the current state

Overwrite method to read the current state. Used to provide another storage mechanism,
different from the standard Rails read_attribute method.

class MyClass
include AASM
Expand All @@ -8,12 +10,25 @@ Overwrite method to read the current state
end
end

Overwrite method to write the current state

Overwrite method to write the current state (and actually persist it). Used to provide
another storage mechanism, different from the standard Rails write_attribute method.

class MyClass
include AASM

def aasm_write_state
# store and persist the current state manually
end
end


Overwrite method to write the current state (without persisting it).

class MyClass
include AASM

def aasm_write_state_without_persistence
# store the current state manually
end
end
5 changes: 5 additions & 0 deletions lib/aasm/aasm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ def aasm_write_state(new_state)
true
end

# may be overwritten by persistence mixins
def aasm_write_state_without_persistence(new_state)
true
end

# deprecated
def aasm_current_state
# warn "#aasm_current_state is deprecated and will be removed in version 3.2.0; please use #aasm.state instead!"
Expand Down
4 changes: 1 addition & 3 deletions lib/aasm/instance_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ def current_state
end

def current_state=(state)
if @instance.respond_to?(:aasm_write_state_without_persistence) || @instance.private_methods.include?('aasm_write_state_without_persistence')
@instance.aasm_write_state_without_persistence(state)
end
@instance.aasm_write_state_without_persistence(state)
@current_state = state
end

Expand Down
38 changes: 16 additions & 22 deletions lib/aasm/persistence/active_record_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ module ActiveRecordPersistence
# * extends the model with ClassMethods
# * includes InstanceMethods
#
# Unless the corresponding methods are already defined, it includes
# * WriteStateWithoutPersistence
#
# Adds
#
# before_validation :aasm_ensure_initial_state, :on => :create
Expand All @@ -33,7 +30,6 @@ def self.included(base)
base.send(:include, AASM::Persistence::Base)
base.extend AASM::Persistence::ActiveRecordPersistence::ClassMethods
base.send(:include, AASM::Persistence::ActiveRecordPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)

if ActiveRecord::VERSION::MAJOR >= 3
base.before_validation(:aasm_ensure_initial_state, :on => :create)
Expand Down Expand Up @@ -98,6 +94,22 @@ def aasm_write_state(state)
true
end

# Writes <tt>state</tt> to the state column, but does not persist it to the database
#
# foo = Foo.find(1)
# foo.aasm_current_state # => :opened
# foo.close
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :opened
# foo.save
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state)
write_attribute(self.class.aasm_column, state.to_s)
end

private

# Ensures that if the aasm_state column is nil and the record is new
Expand Down Expand Up @@ -126,24 +138,6 @@ def aasm_fire_event(name, options, *args)
end
end # InstanceMethods

module WriteStateWithoutPersistence
# Writes <tt>state</tt> to the state column, but does not persist it to the database
#
# foo = Foo.find(1)
# foo.aasm_current_state # => :opened
# foo.close
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :opened
# foo.save
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state)
write_attribute(self.class.aasm_column, state.to_s)
end
end

end
end
end
38 changes: 16 additions & 22 deletions lib/aasm/persistence/mongoid_persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ module MongoidPersistence
# * extends the model with ClassMethods
# * includes InstanceMethods
#
# Unless the corresponding methods are already defined, it includes
# * WriteStateWithoutPersistence
#
# Adds
#
# before_validation :aasm_ensure_initial_state
Expand All @@ -35,7 +32,6 @@ def self.included(base)
base.send(:include, AASM::Persistence::Base)
base.extend AASM::Persistence::MongoidPersistence::ClassMethods
base.send(:include, AASM::Persistence::MongoidPersistence::InstanceMethods)
base.send(:include, AASM::Persistence::MongoidPersistence::WriteStateWithoutPersistence) unless base.method_defined?(:aasm_write_state_without_persistence)

# Mongoid's Validatable gem dependency goes not have a before_validation_on_xxx hook yet.
# base.before_validation_on_create :aasm_ensure_initial_state
Expand Down Expand Up @@ -88,6 +84,22 @@ def aasm_write_state(state)
true
end

# Writes <tt>state</tt> to the state column, but does not persist it to the database
#
# foo = Foo.find(1)
# foo.aasm_current_state # => :opened
# foo.close
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :opened
# foo.save
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state)
write_attribute(self.class.aasm_column, state.to_s)
end

private

# Ensures that if the aasm_state column is nil and the record is new
Expand All @@ -110,24 +122,6 @@ def aasm_ensure_initial_state
end
end # InstanceMethods

module WriteStateWithoutPersistence
# Writes <tt>state</tt> to the state column, but does not persist it to the database
#
# foo = Foo.find(1)
# foo.aasm_current_state # => :opened
# foo.close
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :opened
# foo.save
# foo.aasm_current_state # => :closed
# Foo.find(1).aasm_current_state # => :closed
#
# NOTE: intended to be called from an event
def aasm_write_state_without_persistence(state)
write_attribute(self.class.aasm_column, state.to_s)
end
end

module NamedScopeMethods
def aasm_state_with_named_scope name, options = {}
aasm_state_without_named_scope name, options
Expand Down
16 changes: 12 additions & 4 deletions spec/models/active_record/api.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class DefaultState
attr_accessor :transient_store
attr_accessor :transient_store, :persisted_store
include AASM
aasm do
state :alpha, :initial => true
Expand All @@ -12,7 +12,7 @@ class DefaultState
end

class ProvidedState
attr_accessor :transient_store
attr_accessor :transient_store, :persisted_store
include AASM
aasm do
state :alpha, :initial => true
Expand All @@ -28,12 +28,16 @@ def aasm_read_state
end

def aasm_write_state(new_state)
@persisted_store = new_state
end

def aasm_write_state_without_persistence(new_state)
@transient_store = new_state
end
end

class PersistedState < ActiveRecord::Base
attr_accessor :transient_store
attr_accessor :transient_store, :persisted_store
include AASM
aasm do
state :alpha, :initial => true
Expand All @@ -46,7 +50,7 @@ class PersistedState < ActiveRecord::Base
end

class ProvidedAndPersistedState < ActiveRecord::Base
attr_accessor :transient_store
attr_accessor :transient_store, :persisted_store
include AASM
aasm do
state :alpha, :initial => true
Expand All @@ -62,6 +66,10 @@ def aasm_read_state
end

def aasm_write_state(new_state)
@persisted_store = new_state
end

def aasm_write_state_without_persistence(new_state)
@transient_store = new_state
end
end
34 changes: 30 additions & 4 deletions spec/unit/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,54 @@
end
end

describe "writing the current state" do
describe "writing and persisting the current state" do
it "uses the AASM default" do
o = DefaultState.new
o.release!
o.transient_store.should be_nil
o.persisted_store.should be_nil
end

it "uses the provided method" do
o = ProvidedState.new
o.release!
o.transient_store.should eql :beta
o.persisted_store.should eql :beta
end

it "uses the persistence storage" do
o = PersistedState.new
o.release!
o.transient_store.should be_nil
o.persisted_store.should be_nil
end

it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release!
o.persisted_store.should eql :beta
end
end

describe "writing the current state without persisting it" do
it "uses the AASM default" do
o = DefaultState.new
o.release
o.transient_store.should be_nil
end

it "uses the provided method" do
o = ProvidedState.new
o.release
o.transient_store.should eql :beta
end

it "uses the persistence storage" do
o = PersistedState.new
o.release
o.transient_store.should be_nil
end

it "uses the provided method even if persisted" do
o = ProvidedAndPersistedState.new
o.release
o.transient_store.should eql :beta
end
end
24 changes: 0 additions & 24 deletions spec/unit/persistence/active_record_persistence_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,6 @@
end
end

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::WriteStateWithoutPersistence)
end
end

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::WriteStateWithoutPersistence)
end
end

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_not be_include(AASM::Persistence::ActiveRecordPersistence::WriteStateWithoutPersistence)
end
end

describe "instance methods" do
let(:gate) {Gate.new}

Expand Down

0 comments on commit bbafd22

Please sign in to comment.