Skip to content

Commit 33c3f69

Browse files
committed
Working on state-machines#26, added some tests to more clearly establish the baseline, as well as to show that we aren't behaving as expected when considering "ActiveRecord::Base#save returns nil on a rolled-back transaction"
1 parent c450a55 commit 33c3f69

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

lib/state_machines/integrations/active_record.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ def save(*)
493493
end
494494
495495
def save!(*)
496-
self.class.state_machine(#{name.inspect}).send(:around_save, self) { super } || raise(ActiveRecord::RecordInvalid.new(self))
496+
result = self.class.state_machine(#{name.inspect}).send(:around_save, self) { super }
497+
result || raise(ActiveRecord::RecordInvalid.new(self))
497498
end
498499
499500
def changed_for_autosave?

test/machine_with_event_attributes_on_save_test.rb

+63-3
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,69 @@ def test_should_allow_additional_transitions_to_previous_state_in_after_transiti
176176
assert_equal 'first_gear', @record.state
177177
end
178178

179-
def test_should_return_false_on_before_transition_rollback
180-
@machine.before_transition { raise ActiveRecord::Rollback }
179+
def test_should_yield_one_model!
180+
assert_equal true, @record.save!
181+
assert_equal 1, @model.count
182+
end
181183

182-
assert_equal false, @record.save
184+
# explicit tests of #save and #save! to ensure expected behavior
185+
def test_should_yield_two_models_with_before
186+
@machine.before_transition { @model.create! }
187+
assert_equal true, @record.save
188+
assert_equal 2, @model.count
183189
end
190+
191+
def test_should_yield_two_models_with_before!
192+
@machine.before_transition { @model.create! }
193+
assert_equal true, @record.save!
194+
assert_equal 2, @model.count
195+
end
196+
197+
def test_should_raise_on_around_transition_rollback!
198+
@machine.before_transition { @model.create! }
199+
@machine.around_transition { @model.create!; raise ActiveRecord::Rollback }
200+
201+
raised = false
202+
begin
203+
@record.save!
204+
rescue Exception
205+
raised = true
206+
end
207+
208+
assert_equal true, raised
209+
assert_equal 0, @model.count
210+
end
211+
212+
def test_should_return_nil_on_around_transition_rollback
213+
@machine.before_transition { @model.create! }
214+
@machine.around_transition { @model.create!; raise ActiveRecord::Rollback }
215+
assert_equal nil, @record.save
216+
assert_equal 0, @model.count
217+
end
218+
219+
def test_should_return_nil_on_before_transition_rollback
220+
@machine.before_transition { raise ActiveRecord::Rollback }
221+
assert_equal nil, @record.save
222+
assert_equal 0, @model.count
223+
end
224+
225+
#
226+
# @rosskevin - This fails and I'm not sure why, it was existing behavior.
227+
# see: https://github.com/state-machines/state_machines-activerecord/pull/26#issuecomment-112911886
228+
#
229+
# def test_should_yield_three_models_with_before_and_around_save
230+
# @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
231+
# @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
232+
#
233+
# assert_equal true, @record.save
234+
# assert_equal 3, @model.count
235+
# end
236+
#
237+
# def test_should_yield_three_models_with_before_and_around_save!
238+
# @machine.before_transition { @model.create!; puts "before ran, now #{@model.count}" }
239+
# @machine.around_transition { @model.create!; puts "around ran, now #{@model.count}" }
240+
#
241+
# assert_equal true, @record.save!
242+
# assert_equal 3, @model.count
243+
# end
184244
end

0 commit comments

Comments
 (0)