Add to AASM fire(event) and fire!(event) methods

This commit is contained in:
Vladislav Moskovets 2017-08-08 20:20:58 +03:00 committed by Anil Kumar Maurya
parent c5b7efaab2
commit 5f05e38241
4 changed files with 41 additions and 3 deletions

View File

@ -326,7 +326,7 @@ class Cleaner
end
transitions :from => :idle, :to => :idle
end
event :clean_if_dirty do
transitions :from => :idle, :to => :cleaning, :guard => :if_dirty?
end
@ -335,7 +335,7 @@ class Cleaner
def cleaning_needed?
false
end
def if_dirty?(status)
status == :dirty
end
@ -689,6 +689,10 @@ You can tell AASM to auto-save the object or leave it unsaved
job = Job.new
job.run # not saved
job.run! # saved
# or
job.fire(:run) # not saved
job.fire!(:run) # saved
```
Saving includes running all validations on the `Job` class. If

View File

@ -103,6 +103,14 @@ module AASM
end
end
def fire(event_name, *args, &block)
@instance.send(:aasm_fire_event, @name, event_name, {persist: false}, *args, &block)
end
def fire!(event_name, *args, &block)
@instance.send(:aasm_fire_event, @name, event_name, {persist: true}, *args, &block)
end
def set_current_state_with_persistence(state)
save_success = @instance.aasm_write_state(state, @name)
self.current_state = state if save_success

View File

@ -77,6 +77,16 @@ describe 'when being unsuspended' do
expect(auth.aasm.may_fire_event?(:activate)).to be true
end
it "should be able to fire event by name" do
expect(auth.aasm.fire(:activate)).to be true
expect(auth.aasm.current_state).to eq(:active)
end
it "should be able to fire! event by name" do
expect(auth.aasm.fire!(:activate)).to be true
expect(auth.aasm.current_state).to eq(:active)
end
it "should not be able to fire unknown events" do
expect(auth.aasm.may_fire_event?(:unknown)).to be false
end

View File

@ -91,9 +91,25 @@ describe 'when being unsuspended' do
expect(auth.aasm(:right).may_fire_event?(:right_activate)).to be true
end
it "should not be able to fire unknown events" do
it 'should not be able to fire unknown events' do
expect(auth.aasm(:left).may_fire_event?(:unknown)).to be false
expect(auth.aasm(:right).may_fire_event?(:unknown)).to be false
end
it 'should be able to fire event by name' do
expect(auth.aasm(:left).fire(:left_activate)).to be true
expect(auth.aasm(:left).current_state).to eq(:active)
expect(auth.aasm(:right).fire(:right_activate)).to be true
expect(auth.aasm(:right).current_state).to eq(:active)
end
it 'should be able to fire! event by name' do
expect(auth.aasm(:left).fire!(:left_activate)).to be true
expect(auth.aasm(:left).current_state).to eq(:active)
expect(auth.aasm(:right).fire!(:right_activate)).to be true
expect(auth.aasm(:right).current_state).to eq(:active)
end
end