Skip to content

Commit

Permalink
just some minor refactoring to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
alto committed Feb 22, 2013
1 parent 6f9be86 commit b8ea034
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
6 changes: 4 additions & 2 deletions lib/aasm/aasm.rb
Expand Up @@ -10,11 +10,12 @@ def self.included(base) #:nodoc:
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
Expand All @@ -31,6 +32,7 @@ def aasm_initial_state(set_state=nil)
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
Expand Down
34 changes: 17 additions & 17 deletions lib/aasm/base.rb
Expand Up @@ -3,43 +3,43 @@ class Base

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
Expand All @@ -59,11 +59,11 @@ def event(name, options={}, &block)
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
Expand Down
2 changes: 1 addition & 1 deletion lib/aasm/state_machine.rb
Expand Up @@ -29,7 +29,7 @@ def initialize_copy(orig)
@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

Expand Down
5 changes: 3 additions & 2 deletions lib/aasm/supporting_classes/event.rb
@@ -1,6 +1,7 @@
module AASM
module SupportingClasses
class Event

attr_reader :name, :options

def initialize(name, options = {}, &block)
Expand Down Expand Up @@ -40,8 +41,8 @@ def all_transitions
@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)
Expand Down

0 comments on commit b8ea034

Please sign in to comment.