Skip to content

Commit

Permalink
bang event methods now return false and dont transition on failed save
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Distad committed Oct 3, 2008
1 parent e517a0b commit 3eb2ea2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/aasm.rb
Expand Up @@ -97,10 +97,13 @@ def aasm_events_for_state(state)

private
def aasm_current_state_with_persistence=(state)
save_success = true
if self.respond_to?(:aasm_write_state) || self.private_methods.include?('aasm_write_state')
aasm_write_state(state)
save_success = aasm_write_state(state)
end
self.aasm_current_state = state
self.aasm_current_state = state if save_success

save_success
end

def aasm_current_state=(state)
Expand All @@ -127,13 +130,13 @@ def aasm_fire_event(name, persist, *args)
end

if persist
self.aasm_current_state_with_persistence = new_state
self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
persist_successful = (self.aasm_current_state_with_persistence = new_state)
self.send(self.class.aasm_events[name].success) if persist_successful && self.class.aasm_events[name].success
else
self.aasm_current_state = new_state
end

true
!persist && persist_successful
else
if self.respond_to?(:aasm_event_failed)
self.aasm_event_failed(name)
Expand Down
9 changes: 8 additions & 1 deletion lib/persistence/active_record_persistence.rb
Expand Up @@ -188,8 +188,15 @@ module WriteState
#
# NOTE: intended to be called from an event
def aasm_write_state(state)
old_value = read_attribute(self.class.aasm_column)
write_attribute(self.class.aasm_column, state.to_s)
self.save!

unless self.save
write_attribute(self.class.aasm_column, old_value)
return false
end

true
end
end

Expand Down
25 changes: 25 additions & 0 deletions spec/unit/aasm_spec.rb
Expand Up @@ -146,6 +146,31 @@ def foo.aasm_write_state

foo.close!
end

it 'should return false if aasm_write_state is defined and returns false' do
foo = Foo.new

def foo.aasm_write_state
false
end

foo.should_receive(:aasm_write_state)

foo.close!.should be_false
end

it "should not update the aasm_current_state if the write fails" do
foo = Foo.new

def foo.aasm_write_state
false
end

foo.should_receive(:aasm_write_state)

foo.close!
foo.aasm_current_state.should == :open
end
end

describe AASM, '- event firing without persistence' do
Expand Down

0 comments on commit 3eb2ea2

Please sign in to comment.