1
0
Fork 0
mirror of https://github.com/aasm/aasm synced 2023-03-27 23:22:41 -04:00

incorporate feedback using AASM configuration options

This commit is contained in:
HoyaBoya 2016-01-08 13:42:04 -05:00
parent 47826b8a55
commit 9370861cd2
6 changed files with 17 additions and 18 deletions

View file

@ -405,7 +405,7 @@ class CustomAASMBase < AASM::Base
# A custom transiton that we want available across many AASM models. # A custom transiton that we want available across many AASM models.
def count_transitions! def count_transitions!
klass.class_eval do klass.class_eval do
aasm_with CustomAASMBase do aasm :with_klass => CustomAASMBase do
after_all_transitions :increment_transition_count after_all_transitions :increment_transition_count
end end
end end
@ -435,14 +435,14 @@ class CustomAASMBase < AASM::Base
end end
``` ```
When we declare our model that has an AASM state machine, we simply declare the AASM block with `aasm_with` to our own class. When we declare our model that has an AASM state machine, we simply declare the AASM block with a `:with` key to our own class.
``` ```
class SimpleCustomExample class SimpleCustomExample
include AASM include AASM
# Let's build an AASM state machine with our custom class. # Let's build an AASM state machine with our custom class.
aasm_with CustomAASMBase do aasm :with_klass => CustomAASMBase do
requires_guards! requires_guards!
count_transitions! count_transitions!

View file

@ -24,19 +24,8 @@ module AASM
super super
end end
# allows you to build an AASM model with application specific AASM::Base class.
def aasm_with(application_klass, *args, &block)
build_aasm(application_klass, *args, &block)
end
# this is the entry point for all state and event definitions # this is the entry point for all state and event definitions
def aasm(*args, &block) def aasm(*args, &block)
build_aasm(AASM::Base, *args, &block)
end
private
def build_aasm(aasm_klass, *args, &block)
if args[0].is_a?(Symbol) || args[0].is_a?(String) if args[0].is_a?(Symbol) || args[0].is_a?(String)
# using custom name # using custom name
state_machine_name = args[0].to_sym state_machine_name = args[0].to_sym
@ -49,6 +38,10 @@ module AASM
AASM::StateMachine[self][state_machine_name] ||= AASM::StateMachine.new(state_machine_name) AASM::StateMachine[self][state_machine_name] ||= AASM::StateMachine.new(state_machine_name)
# use a default despite the DSL configuration default.
# this is because configuration hasn't been setup for the AASM class but we are accessing a DSL option already for the class.
aasm_klass = options[:with_klass] || AASM::Base
raise ArgumentError, "The class #{aasm_klass} must inherit from AASM::Base!" unless aasm_klass.ancestors.include?(AASM::Base) raise ArgumentError, "The class #{aasm_klass} must inherit from AASM::Base!" unless aasm_klass.ancestors.include?(AASM::Base)
@aasm ||= {} @aasm ||= {}

View file

@ -28,6 +28,9 @@ module AASM
# set to true to forbid direct assignment of aasm_state column (in ActiveRecord) # set to true to forbid direct assignment of aasm_state column (in ActiveRecord)
configure :no_direct_assignment, false configure :no_direct_assignment, false
# allow a AASM::Base sub-class to be used for state machine
configure :with_klass, AASM::Base
configure :enum, nil configure :enum, nil
# make sure to raise an error if no_direct_assignment is enabled # make sure to raise an error if no_direct_assignment is enabled

View file

@ -18,6 +18,9 @@ module AASM
# forbid direct assignment in aasm_state column (in ActiveRecord) # forbid direct assignment in aasm_state column (in ActiveRecord)
attr_accessor :no_direct_assignment attr_accessor :no_direct_assignment
# allow a AASM::Base sub-class to be used for state machine
attr_accessor :with_klass
attr_accessor :enum attr_accessor :enum
end end
end end

View file

@ -2,7 +2,7 @@ class CustomAASMBase < AASM::Base
# A custom transiton that we want available across many AASM models. # A custom transiton that we want available across many AASM models.
def count_transitions! def count_transitions!
klass.class_eval do klass.class_eval do
aasm_with CustomAASMBase do aasm :with_klass => CustomAASMBase do
after_all_transitions :increment_transition_count after_all_transitions :increment_transition_count
end end
end end
@ -35,7 +35,7 @@ class SimpleCustomExample
include AASM include AASM
# Let's build an AASM state machine with our custom class. # Let's build an AASM state machine with our custom class.
aasm_with CustomAASMBase do aasm :with_klass => CustomAASMBase do
requires_guards! requires_guards!
count_transitions! count_transitions!

View file

@ -27,7 +27,7 @@ describe 'Custom AASM::Base' do
Class.new do Class.new do
include AASM include AASM
aasm_with String do aasm :with_klass => String do
end end
end end
end end