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.
def count_transitions!
klass.class_eval do
aasm_with CustomAASMBase do
aasm :with_klass => CustomAASMBase do
after_all_transitions :increment_transition_count
end
end
@ -435,14 +435,14 @@ class CustomAASMBase < AASM::Base
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
include AASM
# Let's build an AASM state machine with our custom class.
aasm_with CustomAASMBase do
aasm :with_klass => CustomAASMBase do
requires_guards!
count_transitions!

View File

@ -24,19 +24,8 @@ module AASM
super
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
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)
# using custom name
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)
# 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)
@aasm ||= {}

View File

@ -28,6 +28,9 @@ module AASM
# set to true to forbid direct assignment of aasm_state column (in ActiveRecord)
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
# 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)
attr_accessor :no_direct_assignment
# allow a AASM::Base sub-class to be used for state machine
attr_accessor :with_klass
attr_accessor :enum
end
end
end

View File

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

View File

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