avoid code duplication

This commit is contained in:
Thorsten Böttger 2014-01-24 00:49:19 +01:00
parent 214637d8e3
commit cc19ab60fd
1 changed files with 20 additions and 20 deletions

View File

@ -5,30 +5,19 @@ module AASM
@clazz = clazz
@state_machine = AASM::StateMachine[@clazz]
@state_machine.config.column = options[:column].to_sym if options[:column]
@options = options
if options.key?(:whiny_transitions)
@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
# let's cry if the transition is invalid
configure :whiny_transitions, true
if options.key?(:create_scopes)
@state_machine.config.create_scopes = options[:create_scopes]
elsif @state_machine.config.create_scopes.nil?
@state_machine.config.create_scopes = true # this is the default, so let's create scopes
end
# create named scopes for each state
configure :create_scopes, true
if options.key?(:skip_validation_on_save)
@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
# don't store any new state if the model is invalid
configure :skip_validation_on_save, false
if options.key?(:requires_new_transaction)
@state_machine.config.requires_new_transaction = options[:requires_new_transaction]
elsif @state_machine.config.requires_new_transaction.nil?
@state_machine.config.requires_new_transaction = true # use requires_new for nested transactions
end
# use requires_new for nested transactions
configure :requires_new_transaction, true
end
def initial_state(new_initial_state=nil)
@ -93,5 +82,16 @@ module AASM
end
end
private
def configure(key, default_value)
@state_machine.config.send(:new_ostruct_member, key)
if @options.key?(key)
@state_machine.config.send("#{key}=", @options[key])
elsif @state_machine.config.send(key).nil?
@state_machine.config.send("#{key}=", default_value)
end
end
end
end