2012-11-05 08:56:42 +00:00
|
|
|
module AASM
|
|
|
|
module Persistence
|
|
|
|
module Base
|
|
|
|
|
2013-04-24 11:05:49 +00:00
|
|
|
def self.included(base) #:nodoc:
|
|
|
|
base.extend ClassMethods
|
|
|
|
end
|
|
|
|
|
2014-11-24 22:50:04 +00:00
|
|
|
# Returns the value of the aasm.attribute_name - called from <tt>aasm.current_state</tt>
|
2013-04-24 11:03:24 +00:00
|
|
|
#
|
|
|
|
# If it's a new record, and the aasm state column is blank it returns the initial state
|
|
|
|
# (example provided here for ActiveRecord, but it's true for Mongoid as well):
|
|
|
|
#
|
|
|
|
# class Foo < ActiveRecord::Base
|
|
|
|
# include AASM
|
|
|
|
# aasm :column => :status do
|
|
|
|
# state :opened
|
|
|
|
# state :closed
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# foo = Foo.new
|
|
|
|
# foo.current_state # => :opened
|
|
|
|
# foo.close
|
|
|
|
# foo.current_state # => :closed
|
|
|
|
#
|
|
|
|
# foo = Foo.find(1)
|
|
|
|
# foo.current_state # => :opened
|
|
|
|
# foo.aasm_state = nil
|
|
|
|
# foo.current_state # => nil
|
|
|
|
#
|
|
|
|
# NOTE: intended to be called from an event
|
|
|
|
#
|
|
|
|
# This allows for nil aasm states - be sure to add validation to your model
|
2015-05-05 10:16:25 +00:00
|
|
|
def aasm_read_state(name=:default)
|
|
|
|
state = send(self.class.aasm(name).attribute_name)
|
2019-05-16 10:27:37 +00:00
|
|
|
if !state || state.empty?
|
2017-06-13 16:57:32 +00:00
|
|
|
aasm_new_record? ? aasm(name).determine_state_name(self.class.aasm(name).initial_state) : nil
|
2013-04-24 11:03:24 +00:00
|
|
|
else
|
2017-06-13 16:57:32 +00:00
|
|
|
state.to_sym
|
2013-04-24 11:03:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-13 16:57:32 +00:00
|
|
|
def aasm_new_record?
|
2017-06-13 22:49:02 +00:00
|
|
|
new_record?
|
2017-06-13 16:57:32 +00:00
|
|
|
end
|
|
|
|
|
2012-11-05 08:56:42 +00:00
|
|
|
module ClassMethods
|
2014-11-24 22:50:04 +00:00
|
|
|
def aasm_column(attribute_name=nil)
|
|
|
|
warn "[DEPRECATION] aasm_column is deprecated. Use aasm.attribute_name instead"
|
|
|
|
aasm.attribute_name(attribute_name)
|
2012-11-05 08:56:42 +00:00
|
|
|
end
|
2013-04-24 11:03:24 +00:00
|
|
|
end # ClassMethods
|
2012-11-05 08:56:42 +00:00
|
|
|
|
|
|
|
end # Base
|
|
|
|
end # Persistence
|
2013-01-06 22:29:35 +00:00
|
|
|
|
|
|
|
class Base
|
2013-04-28 13:16:15 +00:00
|
|
|
# make sure to create a (named) scope for each state
|
2016-09-17 10:15:54 +00:00
|
|
|
def state_with_scope(*args)
|
|
|
|
names = state_without_scope(*args)
|
2021-03-25 02:50:46 +00:00
|
|
|
names.each do |name|
|
2021-03-25 03:10:59 +00:00
|
|
|
create_scopes(name)
|
2021-03-25 02:50:46 +00:00
|
|
|
end
|
2015-12-06 18:56:33 +00:00
|
|
|
end
|
|
|
|
alias_method :state_without_scope, :state
|
|
|
|
alias_method :state, :state_with_scope
|
2013-05-29 21:19:04 +00:00
|
|
|
|
2015-12-06 18:56:33 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def create_scope?(name)
|
2016-02-27 11:06:36 +00:00
|
|
|
@state_machine.config.create_scopes && !@klass.respond_to?(name) && @klass.respond_to?(:aasm_create_scope)
|
2015-12-06 18:56:33 +00:00
|
|
|
end
|
2015-10-27 09:28:00 +00:00
|
|
|
|
2015-12-06 18:56:33 +00:00
|
|
|
def create_scope(name)
|
2021-03-25 03:10:59 +00:00
|
|
|
@klass.aasm_create_scope(@name, name) if create_scope?(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_scopes(name)
|
|
|
|
if namespace?
|
|
|
|
# Create default scopes even when namespace? for backward compatibility
|
|
|
|
namepaced_name = "#{namespace}_#{name}"
|
|
|
|
create_scope(namepaced_name)
|
|
|
|
end
|
|
|
|
create_scope(name)
|
2015-12-06 18:56:33 +00:00
|
|
|
end
|
2013-01-06 22:29:35 +00:00
|
|
|
end # Base
|
|
|
|
|
2012-11-05 08:56:42 +00:00
|
|
|
end # AASM
|