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

continued to refactor instance methods for AASM objects

This commit is contained in:
Thorsten Böttger 2013-02-22 19:15:03 +13:00
parent f66f2a1e76
commit 233d99b906
6 changed files with 42 additions and 30 deletions

View file

@ -87,16 +87,10 @@ module AASM
aasm.current_state
end
# deprecated
def aasm_enter_initial_state
state_name = aasm.determine_state_name(self.class.aasm_initial_state)
state = aasm.state_object_for_name(state_name)
state.fire_callbacks(:before_enter, self)
state.fire_callbacks(:enter, self)
aasm.current_state = state_name
state.fire_callbacks(:after_enter, self)
state_name
# warn "#aasm_enter_initial_state is deprecated and will be removed in version 3.2.0; please use #aasm.enter_initial_state instead!"
aasm.enter_initial_state
end
# deprecated
@ -105,10 +99,10 @@ module AASM
aasm.events(aasm.current_state)
end
# filters the results of events_for_current_state so that only those that
# are really currently possible (given transition guards) are shown.
# deprecated
def aasm_permissible_events_for_current_state
aasm.events(aasm.current_state).select{ |e| self.send(("may_" + e.to_s + "?").to_sym) }
# warn "#aasm_permissible_events_for_current_state is deprecated and will be removed in version 3.2.0; please use #aasm.permissible_events instead!"
aasm.permissible_events
end
# deprecated
@ -125,16 +119,6 @@ module AASM
private
def aasm_set_current_state_with_persistence(state)
save_success = true
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
save_success = aasm_write_state(state)
end
aasm.current_state = state if save_success
save_success
end
def aasm_fire_event(name, options, *args)
persist = options[:persist]
@ -159,7 +143,7 @@ private
persist_successful = true
if persist
persist_successful = aasm_set_current_state_with_persistence(new_state_name)
persist_successful = aasm.set_current_state_with_persistence(new_state_name)
event.fire_callbacks(:success, self) if persist_successful
else
aasm.current_state = new_state_name

View file

@ -6,7 +6,7 @@ module AASM
end
def current_state
@current_state ||= persistable? ? @instance.aasm_read_state : @instance.aasm_enter_initial_state
@current_state ||= persistable? ? @instance.aasm_read_state : enter_initial_state
end
def current_state=(state)
@ -16,6 +16,18 @@ module AASM
@current_state = state
end
def enter_initial_state
state_name = determine_state_name(@instance.class.aasm_initial_state)
state_object = state_object_for_name(state_name)
state_object.fire_callbacks(:before_enter, @instance)
state_object.fire_callbacks(:enter, @instance)
self.current_state = state_name
state_object.fire_callbacks(:after_enter, @instance)
state_name
end
def human_state
AASM::Localizer.new.human_state_name(@instance.class, current_state)
end
@ -25,6 +37,13 @@ module AASM
events.map {|e| e.name}
end
# filters the results of events_for_current_state so that only those that
# are really currently possible (given transition guards) are shown.
# TODO: what about events.permissible ?
def permissible_events
events.select{ |e| @instance.send(("may_" + e.to_s + "?").to_sym) }
end
def state_object_for_name(name)
obj = @instance.class.aasm.states.find {|s| s == name}
raise AASM::UndefinedState, "State :#{name} doesn't exist" if obj.nil?
@ -47,6 +66,15 @@ module AASM
event.may_fire?(@instance, *args)
end
def set_current_state_with_persistence(state)
save_success = true
if @instance.respond_to?(:aasm_write_state) || @instance.private_methods.include?('aasm_write_state')
save_success = @instance.aasm_write_state(state)
end
self.current_state = state if save_success
save_success
end
private
def persistable?

View file

@ -112,7 +112,7 @@ module AASM
# foo.aasm_state # => nil
#
def aasm_ensure_initial_state
aasm_enter_initial_state if send(self.class.aasm_column).blank?
aasm.enter_initial_state if send(self.class.aasm_column).blank?
end
def aasm_fire_event(name, options, *args)

View file

@ -106,7 +106,7 @@ module AASM
# foo.aasm_state # => nil
#
def aasm_ensure_initial_state
send("#{self.class.aasm_column}=", self.aasm_enter_initial_state.to_s) if send(self.class.aasm_column).blank?
send("#{self.class.aasm_column}=", aasm.enter_initial_state.to_s) if send(self.class.aasm_column).blank?
end
end
@ -161,4 +161,4 @@ module AASM
end
end
end
end
end

View file

@ -46,7 +46,7 @@ class AuthMachine
def initialize
# the AR backend uses a before_validate_on_create :aasm_ensure_initial_state
# lets do something similar here for testing purposes.
aasm_enter_initial_state
aasm.enter_initial_state
end
def make_activation_code

View file

@ -85,7 +85,7 @@ describe 'event callbacks' do
end
it 'should not call it for failing bang fire' do
@foo.stub!(:aasm_set_current_state_with_persistence).and_return(false)
@foo.aasm.stub!(:set_current_state_with_persistence).and_return(false)
@foo.should_not_receive(:aasm_event_fired)
@foo.close!
end
@ -108,7 +108,7 @@ describe 'event callbacks' do
end
it 'should not call it if persist fails for bang fire' do
@foo.stub!(:aasm_set_current_state_with_persistence).and_return(false)
@foo.aasm.stub!(:set_current_state_with_persistence).and_return(false)
@foo.should_receive(:aasm_event_failed)
@foo.close!
end