mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
Combine event firing logic for ! (persistent) and non-! (non-persistent) event triggers
This commit is contained in:
parent
fd9c275aa8
commit
2bb30ee84b
1 changed files with 25 additions and 34 deletions
59
lib/aasm.rb
59
lib/aasm.rb
|
@ -39,44 +39,12 @@ module AASM
|
|||
end
|
||||
|
||||
define_method("#{name.to_s}!") do
|
||||
new_state = self.class.aasm_events[name].fire(self)
|
||||
unless new_state.nil?
|
||||
if self.respond_to?(:aasm_event_fired)
|
||||
self.aasm_event_fired(self.aasm_current_state, new_state)
|
||||
end
|
||||
|
||||
self.aasm_current_state_with_persistence = new_state
|
||||
|
||||
self.send(self.class.aasm_events[name].success) if self.class.aasm_events[name].success
|
||||
|
||||
true
|
||||
else
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
self.aasm_event_failed(name)
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
aasm_fire_event(name, true)
|
||||
end
|
||||
|
||||
define_method("#{name.to_s}") do
|
||||
new_state = self.class.aasm_events[name].fire(self)
|
||||
unless new_state.nil?
|
||||
if self.respond_to?(:aasm_event_fired)
|
||||
self.aasm_event_fired(self.aasm_current_state, new_state)
|
||||
end
|
||||
|
||||
self.aasm_current_state = new_state
|
||||
true
|
||||
else
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
self.aasm_event_failed(name)
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
aasm_fire_event(name, false)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
def aasm_states
|
||||
|
@ -128,4 +96,27 @@ module AASM
|
|||
@aasm_current_state = state
|
||||
end
|
||||
|
||||
def aasm_fire_event(name, persist)
|
||||
new_state = self.class.aasm_events[name].fire(self)
|
||||
unless new_state.nil?
|
||||
if self.respond_to?(:aasm_event_fired)
|
||||
self.aasm_event_fired(self.aasm_current_state, new_state)
|
||||
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
|
||||
else
|
||||
self.aasm_current_state = new_state
|
||||
end
|
||||
|
||||
true
|
||||
else
|
||||
if self.respond_to?(:aasm_event_failed)
|
||||
self.aasm_event_failed(name)
|
||||
end
|
||||
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue