mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
bang event methods now return false and dont transition on failed save
This commit is contained in:
parent
e517a0b5e4
commit
3eb2ea20f2
3 changed files with 41 additions and 6 deletions
13
lib/aasm.rb
13
lib/aasm.rb
|
@ -97,10 +97,13 @@ module AASM
|
|||
|
||||
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)
|
||||
|
@ -127,13 +130,13 @@ module AASM
|
|||
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)
|
||||
|
|
|
@ -188,8 +188,15 @@ module AASM
|
|||
#
|
||||
# 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
|
||||
|
||||
|
|
|
@ -146,6 +146,31 @@ describe AASM, '- event firing with persistence' do
|
|||
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue