mirror of
https://github.com/aasm/aasm
synced 2023-03-27 23:22:41 -04:00
refactored SupportClasses away
This commit is contained in:
parent
98b54a0141
commit
d88fa9f2bc
17 changed files with 353 additions and 361 deletions
|
|
@ -4,10 +4,10 @@ require 'ostruct'
|
|||
require File.join(File.dirname(__FILE__), 'aasm', 'version')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'errors')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'base')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'supporting_classes', 'transition')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'supporting_classes', 'event')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'supporting_classes', 'state')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'supporting_classes', 'localizer')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'transition')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'event')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'state')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'localizer')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'state_machine')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'persistence')
|
||||
require File.join(File.dirname(__FILE__), 'aasm', 'aasm')
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ module AASM
|
|||
|
||||
# aasm.event(:event_name).human?
|
||||
def aasm_human_event_name(event) # event_name?
|
||||
AASM::SupportingClasses::Localizer.new.human_event_name(self, event)
|
||||
AASM::Localizer.new.human_event_name(self, event)
|
||||
end
|
||||
end # ClassMethods
|
||||
|
||||
|
|
@ -113,7 +113,7 @@ module AASM
|
|||
end
|
||||
|
||||
def aasm_human_state
|
||||
AASM::SupportingClasses::Localizer.new.human_state_name(self.class, aasm_current_state)
|
||||
AASM::Localizer.new.human_state_name(self.class, aasm_current_state)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ module AASM
|
|||
# @clazz.aasm_event(name, options, &block)
|
||||
|
||||
unless @state_machine.events.has_key?(name)
|
||||
@state_machine.events[name] = AASM::SupportingClasses::Event.new(name, options, &block)
|
||||
@state_machine.events[name] = AASM::Event.new(name, options, &block)
|
||||
end
|
||||
|
||||
# an addition over standard aasm so that, before firing an event, you can ask
|
||||
|
|
|
|||
125
lib/aasm/event.rb
Normal file
125
lib/aasm/event.rb
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
module AASM
|
||||
class Event
|
||||
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, options = {}, &block)
|
||||
@name = name
|
||||
@transitions = []
|
||||
update(options, &block)
|
||||
end
|
||||
|
||||
# a neutered version of fire - it doesn't actually fire the event, it just
|
||||
# executes the transition guards to determine if a transition is even
|
||||
# an option given current conditions.
|
||||
def may_fire?(obj, to_state=nil, *args)
|
||||
_fire(obj, true, to_state, *args) # true indicates test firing
|
||||
end
|
||||
|
||||
def fire(obj, to_state=nil, *args)
|
||||
_fire(obj, false, to_state, *args) # false indicates this is not a test (fire!)
|
||||
end
|
||||
|
||||
def transitions_from_state?(state)
|
||||
transitions_from_state(state).any?
|
||||
end
|
||||
|
||||
def transitions_from_state(state)
|
||||
@transitions.select { |t| t.from == state }
|
||||
end
|
||||
|
||||
def transitions_to_state?(state)
|
||||
transitions_to_state(state).any?
|
||||
end
|
||||
|
||||
def transitions_to_state(state)
|
||||
@transitions.select { |t| t.to == state }
|
||||
end
|
||||
|
||||
def all_transitions
|
||||
@transitions
|
||||
end
|
||||
|
||||
def fire_callbacks(callback_name, record, *args)
|
||||
invoke_callbacks(@options[callback_name], record, args)
|
||||
end
|
||||
|
||||
def ==(event)
|
||||
if event.is_a? Symbol
|
||||
name == event
|
||||
else
|
||||
name == event.name
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update(options = {}, &block)
|
||||
@options = options
|
||||
if block then
|
||||
instance_eval(&block)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
# Execute if test? == false, otherwise return true/false depending on whether it would fire
|
||||
def _fire(obj, test, to_state=nil, *args)
|
||||
if @transitions.map(&:from).any?
|
||||
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
|
||||
return nil if transitions.size == 0
|
||||
else
|
||||
transitions = @transitions
|
||||
end
|
||||
|
||||
result = test ? false : nil
|
||||
transitions.each do |transition|
|
||||
next if to_state and !Array(transition.to).include?(to_state)
|
||||
if transition.perform(obj, *args)
|
||||
if test
|
||||
result = true
|
||||
else
|
||||
result = to_state || Array(transition.to).first
|
||||
transition.execute(obj, *args)
|
||||
end
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def invoke_callbacks(code, record, args)
|
||||
case code
|
||||
when Symbol, String
|
||||
record.send(code, *args)
|
||||
true
|
||||
when Proc
|
||||
record.instance_exec(*args, &code)
|
||||
true
|
||||
when Array
|
||||
code.each {|a| invoke_callbacks(a, record, args)}
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
## DSL interface
|
||||
def transitions(trans_opts)
|
||||
# Create a separate transition for each from state to the given state
|
||||
Array(trans_opts[:from]).each do |s|
|
||||
@transitions << AASM::Transition.new(trans_opts.merge({:from => s.to_sym}))
|
||||
end
|
||||
# Create a transition if to is specified without from (transitions from ANY state)
|
||||
@transitions << AASM::Transition.new(trans_opts) if @transitions.empty? && trans_opts[:to]
|
||||
end
|
||||
|
||||
[:after, :before, :error, :success].each do |callback_name|
|
||||
define_method callback_name do |*args, &block|
|
||||
options[callback_name] = Array(options[callback_name])
|
||||
options[callback_name] << block if block
|
||||
options[callback_name] += Array(args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end # AASM
|
||||
54
lib/aasm/localizer.rb
Normal file
54
lib/aasm/localizer.rb
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
module AASM
|
||||
class Localizer
|
||||
def human_event_name(klass, event)
|
||||
checklist = ancestors_list(klass).inject([]) do |list, ancestor|
|
||||
list << :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}"
|
||||
list
|
||||
end
|
||||
translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
|
||||
end
|
||||
|
||||
def human_state_name(klass, state)
|
||||
checklist = ancestors_list(klass).inject([]) do |list, ancestor|
|
||||
list << item_for(klass, state, ancestor)
|
||||
list << item_for(klass, state, ancestor, :old_style => true)
|
||||
list
|
||||
end
|
||||
translate_queue(checklist) || I18n.translate(checklist.shift, :default => state.to_s.humanize)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def item_for(klass, state, ancestor, options={})
|
||||
separator = options[:old_style] ? '.' : '/'
|
||||
:"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}#{separator}#{state}"
|
||||
end
|
||||
|
||||
def translate_queue(checklist)
|
||||
(0...(checklist.size-1)).each do |i|
|
||||
begin
|
||||
return I18n.translate(checklist.shift, :raise => true)
|
||||
rescue I18n::MissingTranslationData
|
||||
# that's okay
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# added for rails 2.x compatibility
|
||||
def i18n_scope(klass)
|
||||
klass.respond_to?(:i18n_scope) ? klass.i18n_scope : :activerecord
|
||||
end
|
||||
|
||||
# added for rails < 3.0.3 compatibility
|
||||
def i18n_klass(klass)
|
||||
klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore
|
||||
end
|
||||
|
||||
def ancestors_list(klass)
|
||||
klass.ancestors.select do |ancestor|
|
||||
ancestor.respond_to?(:model_name) unless ancestor == ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
end # AASM
|
||||
78
lib/aasm/state.rb
Normal file
78
lib/aasm/state.rb
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
module AASM
|
||||
class State
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, clazz, options={})
|
||||
@name = name
|
||||
@clazz = clazz
|
||||
update(options)
|
||||
end
|
||||
|
||||
def ==(state)
|
||||
if state.is_a? Symbol
|
||||
name == state
|
||||
else
|
||||
name == state.name
|
||||
end
|
||||
end
|
||||
|
||||
def <=>(state)
|
||||
if state.is_a? Symbol
|
||||
name <=> state
|
||||
else
|
||||
name <=> state.name
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
name.to_s
|
||||
end
|
||||
|
||||
def fire_callbacks(action, record)
|
||||
action = @options[action]
|
||||
catch :halt_aasm_chain do
|
||||
action.is_a?(Array) ?
|
||||
action.each {|a| _fire_callbacks(a, record)} :
|
||||
_fire_callbacks(action, record)
|
||||
end
|
||||
end
|
||||
|
||||
def display_name
|
||||
@display_name ||= begin
|
||||
if Module.const_defined?(:I18n)
|
||||
localized_name
|
||||
else
|
||||
name.to_s.gsub(/_/, ' ').capitalize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def localized_name
|
||||
AASM::Localizer.new.human_state_name(@clazz, self)
|
||||
end
|
||||
|
||||
def for_select
|
||||
[display_name, name.to_s]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update(options = {})
|
||||
if options.key?(:display) then
|
||||
@display_name = options.delete(:display)
|
||||
end
|
||||
@options = options
|
||||
self
|
||||
end
|
||||
|
||||
def _fire_callbacks(action, record)
|
||||
case action
|
||||
when Symbol, String
|
||||
record.send(action)
|
||||
when Proc
|
||||
action.call(record)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # AASM
|
||||
|
|
@ -30,7 +30,7 @@ module AASM
|
|||
end
|
||||
|
||||
def add_state(name, clazz, options)
|
||||
@states << AASM::SupportingClasses::State.new(name, clazz, options) unless @states.include?(name)
|
||||
@states << AASM::State.new(name, clazz, options) unless @states.include?(name)
|
||||
end
|
||||
|
||||
end # StateMachine
|
||||
|
|
|
|||
|
|
@ -1,127 +0,0 @@
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class Event
|
||||
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, options = {}, &block)
|
||||
@name = name
|
||||
@transitions = []
|
||||
update(options, &block)
|
||||
end
|
||||
|
||||
# a neutered version of fire - it doesn't actually fire the event, it just
|
||||
# executes the transition guards to determine if a transition is even
|
||||
# an option given current conditions.
|
||||
def may_fire?(obj, to_state=nil, *args)
|
||||
_fire(obj, true, to_state, *args) # true indicates test firing
|
||||
end
|
||||
|
||||
def fire(obj, to_state=nil, *args)
|
||||
_fire(obj, false, to_state, *args) # false indicates this is not a test (fire!)
|
||||
end
|
||||
|
||||
def transitions_from_state?(state)
|
||||
transitions_from_state(state).any?
|
||||
end
|
||||
|
||||
def transitions_from_state(state)
|
||||
@transitions.select { |t| t.from == state }
|
||||
end
|
||||
|
||||
def transitions_to_state?(state)
|
||||
transitions_to_state(state).any?
|
||||
end
|
||||
|
||||
def transitions_to_state(state)
|
||||
@transitions.select { |t| t.to == state }
|
||||
end
|
||||
|
||||
def all_transitions
|
||||
@transitions
|
||||
end
|
||||
|
||||
def fire_callbacks(callback_name, record, *args)
|
||||
invoke_callbacks(@options[callback_name], record, args)
|
||||
end
|
||||
|
||||
def ==(event)
|
||||
if event.is_a? Symbol
|
||||
name == event
|
||||
else
|
||||
name == event.name
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update(options = {}, &block)
|
||||
@options = options
|
||||
if block then
|
||||
instance_eval(&block)
|
||||
end
|
||||
self
|
||||
end
|
||||
|
||||
# Execute if test? == false, otherwise return true/false depending on whether it would fire
|
||||
def _fire(obj, test, to_state=nil, *args)
|
||||
if @transitions.map(&:from).any?
|
||||
transitions = @transitions.select { |t| t.from == obj.aasm_current_state }
|
||||
return nil if transitions.size == 0
|
||||
else
|
||||
transitions = @transitions
|
||||
end
|
||||
|
||||
result = test ? false : nil
|
||||
transitions.each do |transition|
|
||||
next if to_state and !Array(transition.to).include?(to_state)
|
||||
if transition.perform(obj, *args)
|
||||
if test
|
||||
result = true
|
||||
else
|
||||
result = to_state || Array(transition.to).first
|
||||
transition.execute(obj, *args)
|
||||
end
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
def invoke_callbacks(code, record, args)
|
||||
case code
|
||||
when Symbol, String
|
||||
record.send(code, *args)
|
||||
true
|
||||
when Proc
|
||||
record.instance_exec(*args, &code)
|
||||
true
|
||||
when Array
|
||||
code.each {|a| invoke_callbacks(a, record, args)}
|
||||
true
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
## DSL interface
|
||||
def transitions(trans_opts)
|
||||
# Create a separate transition for each from state to the given state
|
||||
Array(trans_opts[:from]).each do |s|
|
||||
@transitions << AASM::SupportingClasses::Transition.new(trans_opts.merge({:from => s.to_sym}))
|
||||
end
|
||||
# Create a transition if to is specified without from (transitions from ANY state)
|
||||
@transitions << AASM::SupportingClasses::Transition.new(trans_opts) if @transitions.empty? && trans_opts[:to]
|
||||
end
|
||||
|
||||
[:after, :before, :error, :success].each do |callback_name|
|
||||
define_method callback_name do |*args, &block|
|
||||
options[callback_name] = Array(options[callback_name])
|
||||
options[callback_name] << block if block
|
||||
options[callback_name] += Array(args)
|
||||
end
|
||||
end
|
||||
end
|
||||
end # SupportingClasses
|
||||
end # AASM
|
||||
|
|
@ -1,56 +0,0 @@
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class Localizer
|
||||
def human_event_name(klass, event)
|
||||
checklist = ancestors_list(klass).inject([]) do |list, ancestor|
|
||||
list << :"#{i18n_scope(klass)}.events.#{i18n_klass(ancestor)}.#{event}"
|
||||
list
|
||||
end
|
||||
translate_queue(checklist) || I18n.translate(checklist.shift, :default => event.to_s.humanize)
|
||||
end
|
||||
|
||||
def human_state_name(klass, state)
|
||||
checklist = ancestors_list(klass).inject([]) do |list, ancestor|
|
||||
list << item_for(klass, state, ancestor)
|
||||
list << item_for(klass, state, ancestor, :old_style => true)
|
||||
list
|
||||
end
|
||||
translate_queue(checklist) || I18n.translate(checklist.shift, :default => state.to_s.humanize)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def item_for(klass, state, ancestor, options={})
|
||||
separator = options[:old_style] ? '.' : '/'
|
||||
:"#{i18n_scope(klass)}.attributes.#{i18n_klass(ancestor)}.#{klass.aasm_column}#{separator}#{state}"
|
||||
end
|
||||
|
||||
def translate_queue(checklist)
|
||||
(0...(checklist.size-1)).each do |i|
|
||||
begin
|
||||
return I18n.translate(checklist.shift, :raise => true)
|
||||
rescue I18n::MissingTranslationData
|
||||
# that's okay
|
||||
end
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# added for rails 2.x compatibility
|
||||
def i18n_scope(klass)
|
||||
klass.respond_to?(:i18n_scope) ? klass.i18n_scope : :activerecord
|
||||
end
|
||||
|
||||
# added for rails < 3.0.3 compatibility
|
||||
def i18n_klass(klass)
|
||||
klass.model_name.respond_to?(:i18n_key) ? klass.model_name.i18n_key : klass.name.underscore
|
||||
end
|
||||
|
||||
def ancestors_list(klass)
|
||||
klass.ancestors.select do |ancestor|
|
||||
ancestor.respond_to?(:model_name) unless ancestor == ActiveRecord::Base
|
||||
end
|
||||
end
|
||||
end
|
||||
end # SupportingClasses
|
||||
end # AASM
|
||||
|
|
@ -1,80 +0,0 @@
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class State
|
||||
attr_reader :name, :options
|
||||
|
||||
def initialize(name, clazz, options={})
|
||||
@name = name
|
||||
@clazz = clazz
|
||||
update(options)
|
||||
end
|
||||
|
||||
def ==(state)
|
||||
if state.is_a? Symbol
|
||||
name == state
|
||||
else
|
||||
name == state.name
|
||||
end
|
||||
end
|
||||
|
||||
def <=>(state)
|
||||
if state.is_a? Symbol
|
||||
name <=> state
|
||||
else
|
||||
name <=> state.name
|
||||
end
|
||||
end
|
||||
|
||||
def to_s
|
||||
name.to_s
|
||||
end
|
||||
|
||||
def fire_callbacks(action, record)
|
||||
action = @options[action]
|
||||
catch :halt_aasm_chain do
|
||||
action.is_a?(Array) ?
|
||||
action.each {|a| _fire_callbacks(a, record)} :
|
||||
_fire_callbacks(action, record)
|
||||
end
|
||||
end
|
||||
|
||||
def display_name
|
||||
@display_name ||= begin
|
||||
if Module.const_defined?(:I18n)
|
||||
localized_name
|
||||
else
|
||||
name.to_s.gsub(/_/, ' ').capitalize
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def localized_name
|
||||
AASM::SupportingClasses::Localizer.new.human_state_name(@clazz, self)
|
||||
end
|
||||
|
||||
def for_select
|
||||
[display_name, name.to_s]
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update(options = {})
|
||||
if options.key?(:display) then
|
||||
@display_name = options.delete(:display)
|
||||
end
|
||||
@options = options
|
||||
self
|
||||
end
|
||||
|
||||
def _fire_callbacks(action, record)
|
||||
case action
|
||||
when Symbol, String
|
||||
record.send(action)
|
||||
when Proc
|
||||
action.call(record)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # SupportingClasses
|
||||
end # AASM
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
module AASM
|
||||
module SupportingClasses
|
||||
class Transition
|
||||
attr_reader :from, :to, :opts
|
||||
alias_method :options, :opts
|
||||
|
||||
def initialize(opts)
|
||||
@from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
|
||||
@opts = opts
|
||||
end
|
||||
|
||||
# TODO: should be named allowed? or similar
|
||||
def perform(obj, *args)
|
||||
case @guard
|
||||
when Symbol, String
|
||||
obj.send(@guard, *args)
|
||||
when Proc
|
||||
@guard.call(obj, *args)
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def execute(obj, *args)
|
||||
@on_transition.is_a?(Array) ?
|
||||
@on_transition.each {|ot| _execute(obj, ot, *args)} :
|
||||
_execute(obj, @on_transition, *args)
|
||||
end
|
||||
|
||||
def ==(obj)
|
||||
@from == obj.from && @to == obj.to
|
||||
end
|
||||
|
||||
def from?(value)
|
||||
@from == value
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def _execute(obj, on_transition, *args)
|
||||
case on_transition
|
||||
when Proc
|
||||
on_transition.arity == 0 ? on_transition.call : on_transition.call(obj, *args)
|
||||
when Symbol, String
|
||||
obj.send(:method, on_transition.to_sym).arity == 0 ? obj.send(on_transition) : obj.send(on_transition, *args)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # SupportingClasses
|
||||
end # AASM
|
||||
49
lib/aasm/transition.rb
Normal file
49
lib/aasm/transition.rb
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
module AASM
|
||||
class Transition
|
||||
attr_reader :from, :to, :opts
|
||||
alias_method :options, :opts
|
||||
|
||||
def initialize(opts)
|
||||
@from, @to, @guard, @on_transition = opts[:from], opts[:to], opts[:guard], opts[:on_transition]
|
||||
@opts = opts
|
||||
end
|
||||
|
||||
# TODO: should be named allowed? or similar
|
||||
def perform(obj, *args)
|
||||
case @guard
|
||||
when Symbol, String
|
||||
obj.send(@guard, *args)
|
||||
when Proc
|
||||
@guard.call(obj, *args)
|
||||
else
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def execute(obj, *args)
|
||||
@on_transition.is_a?(Array) ?
|
||||
@on_transition.each {|ot| _execute(obj, ot, *args)} :
|
||||
_execute(obj, @on_transition, *args)
|
||||
end
|
||||
|
||||
def ==(obj)
|
||||
@from == obj.from && @to == obj.to
|
||||
end
|
||||
|
||||
def from?(value)
|
||||
@from == value
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def _execute(obj, on_transition, *args)
|
||||
case on_transition
|
||||
when Proc
|
||||
on_transition.arity == 0 ? on_transition.call : on_transition.call(obj, *args)
|
||||
when Symbol, String
|
||||
obj.send(:method, on_transition.to_sym).arity == 0 ? obj.send(on_transition) : obj.send(on_transition, *args)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end # AASM
|
||||
|
|
@ -2,7 +2,7 @@ require 'spec_helper'
|
|||
|
||||
describe 'adding an event' do
|
||||
let(:event) do
|
||||
AASM::SupportingClasses::Event.new(:close_order, {:success => :success_callback}) do
|
||||
AASM::Event.new(:close_order, {:success => :success_callback}) do
|
||||
before :before_callback
|
||||
after :after_callback
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
|
|
@ -36,7 +36,7 @@ end
|
|||
|
||||
describe 'transition inspection' do
|
||||
let(:event) do
|
||||
AASM::SupportingClasses::Event.new(:run) do
|
||||
AASM::Event.new(:run) do
|
||||
transitions :to => :running, :from => :sleeping
|
||||
end
|
||||
end
|
||||
|
|
@ -63,12 +63,12 @@ describe 'firing an event' do
|
|||
obj = mock('object')
|
||||
obj.stub!(:aasm_current_state)
|
||||
|
||||
event = AASM::SupportingClasses::Event.new(:event)
|
||||
event = AASM::Event.new(:event)
|
||||
event.fire(obj).should be_nil
|
||||
end
|
||||
|
||||
it 'should return the state of the first matching transition it finds' do
|
||||
event = AASM::SupportingClasses::Event.new(:event) do
|
||||
event = AASM::Event.new(:event) do
|
||||
transitions :to => :closed, :from => [:open, :received]
|
||||
end
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ describe 'firing an event' do
|
|||
end
|
||||
|
||||
it 'should call the guard with the params passed in' do
|
||||
event = AASM::SupportingClasses::Event.new(:event) do
|
||||
event = AASM::Event.new(:event) do
|
||||
transitions :to => :closed, :from => [:open, :received], :guard => :guard_fn
|
||||
end
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ describe 'localized state names' do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Localizer, "new style" do
|
||||
describe AASM::Localizer, "new style" do
|
||||
before(:all) do
|
||||
I18n.load_path << 'spec/en.yml'
|
||||
I18n.default_locale = :en
|
||||
|
|
@ -73,7 +73,7 @@ describe AASM::SupportingClasses::Localizer, "new style" do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Localizer, "deprecated style" do
|
||||
describe AASM::Localizer, "deprecated style" do
|
||||
before(:all) do
|
||||
I18n.load_path << 'spec/en_deprecated_style.yml'
|
||||
I18n.default_locale = :en
|
||||
|
|
@ -12,27 +12,27 @@
|
|||
|
||||
# it "should be created without memory leak" do
|
||||
# machines_count = machines.size
|
||||
# state_count = number_of_objects(AASM::SupportingClasses::State)
|
||||
# event_count = number_of_objects(AASM::SupportingClasses::Event)
|
||||
# state_count = number_of_objects(AASM::State)
|
||||
# event_count = number_of_objects(AASM::Event)
|
||||
# puts "event_count = #{event_count}"
|
||||
# transition_count = number_of_objects(AASM::SupportingClasses::Transition)
|
||||
# transition_count = number_of_objects(AASM::Transition)
|
||||
|
||||
# load File.expand_path(File.dirname(__FILE__) + '/../models/not_auto_loaded/process.rb')
|
||||
# machines.size.should == machines_count + 1 # + Process
|
||||
# number_of_objects(Models::Process).should == 0
|
||||
# number_of_objects(AASM::SupportingClasses::State).should == state_count + 3 # + Process
|
||||
# puts "event_count = #{number_of_objects(AASM::SupportingClasses::Event)}"
|
||||
# number_of_objects(AASM::SupportingClasses::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::SupportingClasses::Transition).should == transition_count + 2 # + Process
|
||||
# number_of_objects(AASM::State).should == state_count + 3 # + Process
|
||||
# puts "event_count = #{number_of_objects(AASM::Event)}"
|
||||
# number_of_objects(AASM::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Transition).should == transition_count + 2 # + Process
|
||||
|
||||
# Models.send(:remove_const, "Process") if Models.const_defined?("Process")
|
||||
# load File.expand_path(File.dirname(__FILE__) + '/../models/not_auto_loaded/process.rb')
|
||||
# machines.size.should == machines_count + 1 # + Process
|
||||
# number_of_objects(AASM::SupportingClasses::State).should == state_count + 3 # + Process
|
||||
# # ObjectSpace.each_object(AASM::SupportingClasses::Event) {|o| puts o.inspect}
|
||||
# puts "event_count = #{number_of_objects(AASM::SupportingClasses::Event)}"
|
||||
# number_of_objects(AASM::SupportingClasses::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::SupportingClasses::Transition).should == transition_count + 2 # + Process
|
||||
# number_of_objects(AASM::State).should == state_count + 3 # + Process
|
||||
# # ObjectSpace.each_object(AASM::Event) {|o| puts o.inspect}
|
||||
# puts "event_count = #{number_of_objects(AASM::Event)}"
|
||||
# number_of_objects(AASM::Event).should == event_count + 2 # + Process
|
||||
# number_of_objects(AASM::Transition).should == transition_count + 2 # + Process
|
||||
# end
|
||||
|
||||
# end
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe AASM::SupportingClasses::State do
|
||||
describe AASM::State do
|
||||
before(:each) do
|
||||
@name = :astate
|
||||
@options = { :crazy_custom_key => 'key' }
|
||||
end
|
||||
|
||||
def new_state(options={})
|
||||
AASM::SupportingClasses::State.new(@name, Conversation, @options.merge(options))
|
||||
AASM::State.new(@name, Conversation, @options.merge(options))
|
||||
end
|
||||
|
||||
it 'should set the name' do
|
||||
|
|
@ -28,10 +28,10 @@ describe 'transitions' do
|
|||
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Transition do
|
||||
describe AASM::Transition do
|
||||
it 'should set from, to, and opts attr readers' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
st.from.should == opts[:from]
|
||||
st.to.should == opts[:to]
|
||||
|
|
@ -40,7 +40,7 @@ describe AASM::SupportingClasses::Transition do
|
|||
|
||||
it 'should pass equality check if from and to are the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.stub!(:from).and_return(opts[:from])
|
||||
|
|
@ -51,7 +51,7 @@ describe AASM::SupportingClasses::Transition do
|
|||
|
||||
it 'should fail equality check if from are not the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.stub!(:from).and_return('blah')
|
||||
|
|
@ -62,7 +62,7 @@ describe AASM::SupportingClasses::Transition do
|
|||
|
||||
it 'should fail equality check if to are not the same' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'g'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.stub!(:from).and_return(opts[:from])
|
||||
|
|
@ -72,17 +72,17 @@ describe AASM::SupportingClasses::Transition do
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Transition, '- when performing guard checks' do
|
||||
describe AASM::Transition, '- when performing guard checks' do
|
||||
it 'should return true of there is no guard' do
|
||||
opts = {:from => 'foo', :to => 'bar'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
st.perform(nil).should be_true
|
||||
end
|
||||
|
||||
it 'should call the method on the object if guard is a symbol' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => :test}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.should_receive(:test)
|
||||
|
|
@ -92,7 +92,7 @@ describe AASM::SupportingClasses::Transition, '- when performing guard checks' d
|
|||
|
||||
it 'should call the method on the object if guard is a string' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => 'test'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.should_receive(:test)
|
||||
|
|
@ -102,7 +102,7 @@ describe AASM::SupportingClasses::Transition, '- when performing guard checks' d
|
|||
|
||||
it 'should call the proc passing the object if the guard is a proc' do
|
||||
opts = {:from => 'foo', :to => 'bar', :guard => Proc.new {|o| o.test}}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
|
||||
obj = mock('object')
|
||||
obj.should_receive(:test)
|
||||
|
|
@ -111,10 +111,10 @@ describe AASM::SupportingClasses::Transition, '- when performing guard checks' d
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Transition, '- when executing the transition with a Proc' do
|
||||
describe AASM::Transition, '- when executing the transition with a Proc' do
|
||||
it 'should call a Proc on the object with args' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {|o| o.test}}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
|
|
@ -125,7 +125,7 @@ describe AASM::SupportingClasses::Transition, '- when executing the transition w
|
|||
|
||||
it 'should call a Proc on the object without args' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => Proc.new {||}}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
|
|
@ -135,10 +135,10 @@ describe AASM::SupportingClasses::Transition, '- when executing the transition w
|
|||
end
|
||||
end
|
||||
|
||||
describe AASM::SupportingClasses::Transition, '- when executing the transition with an :on_transtion method call' do
|
||||
describe AASM::Transition, '- when executing the transition with an :on_transtion method call' do
|
||||
it 'should accept a String for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => 'test'}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
|
|
@ -149,7 +149,7 @@ describe AASM::SupportingClasses::Transition, '- when executing the transition w
|
|||
|
||||
it 'should accept a Symbol for the method name' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
|
|
@ -160,7 +160,7 @@ describe AASM::SupportingClasses::Transition, '- when executing the transition w
|
|||
|
||||
it 'should pass args if the target method accepts them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
|
|
@ -175,7 +175,7 @@ describe AASM::SupportingClasses::Transition, '- when executing the transition w
|
|||
|
||||
it 'should NOT pass args if the target method does NOT accept them' do
|
||||
opts = {:from => 'foo', :to => 'bar', :on_transition => :test}
|
||||
st = AASM::SupportingClasses::Transition.new(opts)
|
||||
st = AASM::Transition.new(opts)
|
||||
args = {:arg1 => '1', :arg2 => '2'}
|
||||
obj = mock('object')
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue