mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
just some minor refactoring to improve readability
This commit is contained in:
parent
6f9be86282
commit
b8ea0340e7
4 changed files with 25 additions and 22 deletions
|
@ -10,11 +10,12 @@ module AASM
|
|||
module ClassMethods
|
||||
|
||||
# make sure inheritance (aka subclassing) works with AASM
|
||||
def inherited(klass)
|
||||
AASM::StateMachine[klass] = AASM::StateMachine[self].clone
|
||||
def inherited(base)
|
||||
AASM::StateMachine[base] = AASM::StateMachine[self].clone
|
||||
super
|
||||
end
|
||||
|
||||
# this is the entry point for all state and event definitions
|
||||
def aasm(options={}, &block)
|
||||
@aasm ||= AASM::Base.new(self, options)
|
||||
@aasm.instance_eval(&block) if block # new DSL
|
||||
|
@ -31,6 +32,7 @@ module AASM
|
|||
end
|
||||
end
|
||||
|
||||
# is this better?: aasm.states.name.from_states
|
||||
def aasm_from_states_for_state(state, options={})
|
||||
if options[:transition]
|
||||
aasm.events[options[:transition]].transitions_to_state(state).flatten.map(&:from).flatten
|
||||
|
|
|
@ -3,43 +3,43 @@ module AASM
|
|||
|
||||
def initialize(clazz, options={}, &block)
|
||||
@clazz = clazz
|
||||
sm = AASM::StateMachine[@clazz]
|
||||
sm.config.column = options[:column].to_sym if options[:column]
|
||||
@state_machine = AASM::StateMachine[@clazz]
|
||||
@state_machine.config.column = options[:column].to_sym if options[:column]
|
||||
|
||||
if options.key?(:whiny_transitions)
|
||||
sm.config.whiny_transitions = options[:whiny_transitions]
|
||||
elsif sm.config.whiny_transitions.nil?
|
||||
sm.config.whiny_transitions = true # this is the default, so let's cry
|
||||
@state_machine.config.whiny_transitions = options[:whiny_transitions]
|
||||
elsif @state_machine.config.whiny_transitions.nil?
|
||||
@state_machine.config.whiny_transitions = true # this is the default, so let's cry
|
||||
end
|
||||
|
||||
if options.key?(:skip_validation_on_save)
|
||||
sm.config.skip_validation_on_save = options[:skip_validation_on_save]
|
||||
elsif sm.config.skip_validation_on_save.nil?
|
||||
sm.config.skip_validation_on_save = false # this is the default, so don't store any new state if the model is invalid
|
||||
@state_machine.config.skip_validation_on_save = options[:skip_validation_on_save]
|
||||
elsif @state_machine.config.skip_validation_on_save.nil?
|
||||
@state_machine.config.skip_validation_on_save = false # this is the default, so don't store any new state if the model is invalid
|
||||
end
|
||||
end
|
||||
|
||||
def initial_state
|
||||
AASM::StateMachine[@clazz].initial_state
|
||||
@state_machine.initial_state
|
||||
end
|
||||
|
||||
# define a state
|
||||
def state(name, options={})
|
||||
# @clazz.aasm_state(name, options)
|
||||
sm = AASM::StateMachine[@clazz]
|
||||
sm.create_state(name, @clazz, options)
|
||||
sm.initial_state = name if options[:initial] || !sm.initial_state
|
||||
@state_machine.add_state(name, @clazz, options)
|
||||
@state_machine.initial_state = name if options[:initial] || !@state_machine.initial_state
|
||||
|
||||
@clazz.send(:define_method, "#{name.to_s}?") do
|
||||
aasm_current_state == name
|
||||
end
|
||||
end
|
||||
|
||||
# define an event
|
||||
def event(name, options={}, &block)
|
||||
# @clazz.aasm_event(name, options, &block)
|
||||
sm = AASM::StateMachine[@clazz]
|
||||
|
||||
unless sm.events.has_key?(name)
|
||||
sm.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
||||
unless @state_machine.events.has_key?(name)
|
||||
@state_machine.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
||||
end
|
||||
|
||||
# an addition over standard aasm so that, before firing an event, you can ask
|
||||
|
@ -59,11 +59,11 @@ module AASM
|
|||
end
|
||||
|
||||
def states
|
||||
AASM::StateMachine[@clazz].states
|
||||
@state_machine.states
|
||||
end
|
||||
|
||||
def events
|
||||
AASM::StateMachine[@clazz].events
|
||||
@state_machine.events
|
||||
end
|
||||
|
||||
def states_for_select
|
||||
|
|
|
@ -29,7 +29,7 @@ module AASM
|
|||
@events = @events.dup
|
||||
end
|
||||
|
||||
def create_state(name, clazz, options)
|
||||
def add_state(name, clazz, options)
|
||||
@states << AASM::SupportingClasses::State.new(name, clazz, options) unless @states.include?(name)
|
||||
end
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class Event
|
||||
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, options = {}, &block)
|
||||
|
@ -40,8 +41,8 @@ module AASM
|
|||
@transitions
|
||||
end
|
||||
|
||||
def fire_callbacks(action, record, *args)
|
||||
invoke_callbacks(@options[action], record, args)
|
||||
def fire_callbacks(callback_name, record, *args)
|
||||
invoke_callbacks(@options[callback_name], record, args)
|
||||
end
|
||||
|
||||
def ==(event)
|
||||
|
|
Loading…
Add table
Reference in a new issue