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

deprecate usage of AASM.aasm_initial_state (getter and setter method)

This commit is contained in:
Thorsten Böttger 2013-11-30 22:46:04 +01:00
parent 45250d09e2
commit c5ae69ca64
6 changed files with 13 additions and 9 deletions

View file

@ -26,12 +26,13 @@ module AASM
@aasm @aasm
end end
# TODO: maybe better: aasm.initial_state # TODO remove this method in v4.0.0
def aasm_initial_state(set_state=nil) def aasm_initial_state(set_state=nil)
if set_state if set_state
# deprecated way to set the value warn ".aasm_initial_state(:name) is deprecated and will be removed in version 4.0.0; please use .aasm.initial_state = :name instead!"
AASM::StateMachine[self].initial_state = set_state AASM::StateMachine[self].initial_state = set_state
else else
warn ".aasm_initial_state is deprecated and will be removed in version 4.0.0; please use .aasm.initial_state instead!"
AASM::StateMachine[self].initial_state AASM::StateMachine[self].initial_state
end end
end end

View file

@ -25,13 +25,16 @@ module AASM
end end
end end
def initial_state def initial_state(new_initial_state=nil)
if new_initial_state
@state_machine.initial_state = new_initial_state
else
@state_machine.initial_state @state_machine.initial_state
end end
end
# define a state # define a state
def state(name, options={}) def state(name, options={})
# @clazz.aasm_state(name, options)
@state_machine.add_state(name, @clazz, options) @state_machine.add_state(name, @clazz, options)
@state_machine.initial_state = name if options[:initial] || !@state_machine.initial_state @state_machine.initial_state = name if options[:initial] || !@state_machine.initial_state

View file

@ -15,7 +15,7 @@ module AASM
end end
def enter_initial_state def enter_initial_state
state_name = determine_state_name(@instance.class.aasm_initial_state) state_name = determine_state_name(@instance.class.aasm.initial_state)
state_object = state_object_for_name(state_name) state_object = state_object_for_name(state_name)
state_object.fire_callbacks(:before_enter, @instance) state_object.fire_callbacks(:before_enter, @instance)

View file

@ -35,7 +35,7 @@ module AASM
def aasm_read_state def aasm_read_state
state = send(self.class.aasm_column) state = send(self.class.aasm_column)
if new_record? if new_record?
state.blank? ? aasm.determine_state_name(self.class.aasm_initial_state) : state.to_sym state.blank? ? aasm.determine_state_name(self.class.aasm.initial_state) : state.to_sym
else else
state.nil? ? nil : state.to_sym state.nil? ? nil : state.to_sym
end end

View file

@ -62,10 +62,10 @@ class Thief < ActiveRecord::Base
set_table_name "thieves" set_table_name "thieves"
end end
include AASM include AASM
aasm_initial_state Proc.new { |thief| thief.skilled ? :rich : :jailed }
aasm do aasm do
state :rich state :rich
state :jailed state :jailed
initial_state Proc.new {|thief| thief.skilled ? :rich : :jailed }
end end
attr_accessor :skilled, :aasm_state attr_accessor :skilled, :aasm_state
end end

View file

@ -5,8 +5,8 @@ class Banker
aasm do aasm do
state :retired state :retired
state :selling_bad_mortgages state :selling_bad_mortgages
initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
end end
aasm_initial_state Proc.new { |banker| banker.rich? ? :retired : :selling_bad_mortgages }
RICH = 1_000_000 RICH = 1_000_000
attr_accessor :balance attr_accessor :balance
def initialize(balance = 0); self.balance = balance; end def initialize(balance = 0); self.balance = balance; end