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