Skip to content

Commit

Permalink
continued to refactor instance methods for AASM objects
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Feb 22, 2013
1 parent f66f2a1 commit 233d99b
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 30 deletions.
30 changes: 7 additions & 23 deletions lib/aasm/aasm.rb
Expand Up @@ -87,16 +87,10 @@ def aasm_current_state
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
Expand All @@ -105,10 +99,10 @@ def aasm_events_for_current_state
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
Expand All @@ -125,16 +119,6 @@ def aasm_human_state

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]

Expand All @@ -159,7 +143,7 @@ def aasm_fire_event(name, options, *args)

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
Expand Down
30 changes: 29 additions & 1 deletion lib/aasm/instance_base.rb
Expand Up @@ -6,7 +6,7 @@ def initialize(instance)
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)
Expand All @@ -16,6 +16,18 @@ def current_state=(state)
@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
Expand All @@ -25,6 +37,13 @@ def events(state=current_state)
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?
Expand All @@ -47,6 +66,15 @@ def may_fire_event?(name, *args)
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?
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/persistence/active_record_persistence.rb
Expand Up @@ -112,7 +112,7 @@ def aasm_current_state
# 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)
Expand Down
4 changes: 2 additions & 2 deletions lib/aasm/persistence/mongoid_persistence.rb
Expand Up @@ -106,7 +106,7 @@ def aasm_current_state
# 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
Expand Down Expand Up @@ -161,4 +161,4 @@ def aasm_state_with_named_scope name, options = {}
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/models/auth_machine.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/callbacks_spec.rb
Expand Up @@ -85,7 +85,7 @@ def @foo.aasm_event_fired(event, from, to); end
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
Expand All @@ -108,7 +108,7 @@ def @foo.aasm_event_failed(event, from); end
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
Expand Down

0 comments on commit 233d99b

Please sign in to comment.