lib/aasm/base.rb in aasm-4.2.0 vs lib/aasm/base.rb in aasm-4.3.0
- old
+ new
@@ -1,14 +1,16 @@
module AASM
class Base
attr_reader :state_machine
- def initialize(klass, options={}, &block)
+ def initialize(klass, name, state_machine, options={}, &block)
@klass = klass
- @state_machine = AASM::StateMachine[@klass]
- @state_machine.config.column ||= (options[:column] || :aasm_state).to_sym # aasm4
+ @name = name
+ # @state_machine = @klass.aasm(@name).state_machine
+ @state_machine = state_machine
+ @state_machine.config.column ||= (options[:column] || default_column).to_sym
# @state_machine.config.column = options[:column].to_sym if options[:column] # master
@options = options
# let's cry if the transition is invalid
configure :whiny_transitions, true
@@ -29,11 +31,11 @@
# make sure to raise an error if no_direct_assignment is enabled
# and attribute is directly assigned though
@klass.class_eval %Q(
def #{@state_machine.config.column}=(state_name)
- if self.class.aasm.state_machine.config.no_direct_assignment
+ if self.class.aasm(:#{@name}).state_machine.config.no_direct_assignment
raise AASM::NoDirectAssignmentError.new(
'direct assignment of AASM column has been disabled (see AASM configuration for this class)'
)
else
super
@@ -62,39 +64,51 @@
# define a state
def state(name, options={})
@state_machine.add_state(name, @klass, options)
- @klass.send(:define_method, "#{name}?") do
- aasm.current_state == name
+ if @klass.instance_methods.include?("#{name}?")
+ warn "#{@klass.name}: The state name #{name} is already used!"
end
+ @klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
+ def #{name}?
+ aasm(:#{@name}).current_state == :#{name}
+ end
+ EORUBY
+
unless @klass.const_defined?("STATE_#{name.upcase}")
@klass.const_set("STATE_#{name.upcase}", name)
end
end
# define an event
def event(name, options={}, &block)
- @state_machine.events[name] = AASM::Core::Event.new(name, options, &block)
+ @state_machine.add_event(name, options, &block)
+ if @klass.instance_methods.include?("may_#{name}?".to_sym)
+ warn "#{@klass.name}: The event name #{name} is already used!"
+ end
+
# an addition over standard aasm so that, before firing an event, you can ask
# may_event? and get back a boolean that tells you whether the guard method
# on the transition will let this happen.
- @klass.send(:define_method, "may_#{name}?") do |*args|
- aasm.may_fire_event?(name, *args)
- end
+ @klass.class_eval <<-EORUBY, __FILE__, __LINE__ + 1
+ def may_#{name}?(*args)
+ aasm(:#{@name}).may_fire_event?(:#{name}, *args)
+ end
- @klass.send(:define_method, "#{name}!") do |*args, &block|
- aasm.current_event = "#{name}!".to_sym
- aasm_fire_event(name, {:persist => true}, *args, &block)
- end
+ def #{name}!(*args, &block)
+ aasm(:#{@name}).current_event = :#{name}!
+ aasm_fire_event(:#{@name}, :#{name}, {:persist => true}, *args, &block)
+ end
- @klass.send(:define_method, "#{name}") do |*args, &block|
- aasm.current_event = name.to_sym
- aasm_fire_event(name, {:persist => false}, *args, &block)
- end
+ def #{name}(*args, &block)
+ aasm(:#{@name}).current_event = :#{name}
+ aasm_fire_event(:#{@name}, :#{name}, {:persist => false}, *args, &block)
+ end
+ EORUBY
end
def states
@state_machine.states
end
@@ -119,9 +133,13 @@
events.map {|e| e.transitions_to_state(state)}.flatten.map(&:from).flatten
end
end
private
+
+ def default_column
+ @name.to_sym == :default ? :aasm_state : @name.to_sym
+ end
def configure(key, default_value)
if @options.key?(key)
@state_machine.config.send("#{key}=", @options[key])
elsif @state_machine.config.send(key).nil?