Add to AASM fire(event) and fire!(event) methods
This commit is contained in:
parent
c5b7efaab2
commit
5f05e38241
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue