module AASM::Persistence::ActiveRecordPersistence::InstanceMethods
def aasm_column_is_blank?(state_machine_name)
def aasm_column_is_blank?(state_machine_name) attribute_name = self.class.aasm(state_machine_name).attribute_name attribute_names.include?(attribute_name.to_s) && (send(attribute_name).respond_to?(:empty?) ? !!send(attribute_name).empty? : !send(attribute_name)) end
def aasm_column_looks_like_enum(name=:default)
def aasm_column_looks_like_enum(name=:default) column_name = self.class.aasm(name).attribute_name.to_s column = self.class.columns_hash[column_name] raise NoMethodError.new("undefined method '#{column_name}' for #{self.class}") if column.nil? column.type == :integer end
def aasm_ensure_initial_state
foo.aasm_state # => nil
foo.valid?
foo.aasm_state = nil
foo.aasm_state # => 1
foo = Foo.find(:first)
foo.aasm_state # => "open" (where :open is the initial state)
foo = Foo.new
then the initial state gets populated after initialization
Ensures that if the aasm_state column is nil and the record is new
def aasm_ensure_initial_state AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name| # checking via respond_to? does not work in Rails <= 3 # if respond_to?(self.class.aasm(state_machine_name).attribute_name) && send(self.class.aasm(state_machine_name).attribute_name).blank? # Rails 4 if aasm_column_is_blank?(state_machine_name) aasm(state_machine_name).enter_initial_state end end end
def aasm_enum(name=:default)
def aasm_enum(name=:default) case AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum when false then nil when true then aasm_guess_enum_method(name) when nil then aasm_guess_enum_method(name) if aasm_column_looks_like_enum(name) else AASM::StateMachineStore.fetch(self.class, true).machine(name).config.enum end end
def aasm_execute_after_commit
def aasm_execute_after_commit begin require 'after_commit_everywhere' raise LoadError unless Gem::Version.new(::AfterCommitEverywhere::VERSION) >= Gem::Version.new('0.1.5') self.extend ::AfterCommitEverywhere after_commit do yield end rescue LoadError warn <<-MSG CATION] :after_commit AASM callback is not safe in terms of race conditions and redundant calls. Please add `gem 'after_commit_everywhere', '~> 1.0'` to your Gemfile in order to fix that. MSG yield end end
def aasm_guess_enum_method(name=:default)
def aasm_guess_enum_method(name=:default) self.class.aasm(name).attribute_name.to_s.pluralize.to_sym end
def aasm_invalid_state?(state_machine_name)
def aasm_invalid_state?(state_machine_name) aasm(state_machine_name).current_state && !aasm(state_machine_name).states.include?(aasm(state_machine_name).current_state) end
def aasm_raise_invalid_record
def aasm_raise_invalid_record raise ActiveRecord::RecordInvalid.new(self) end
def aasm_raw_attribute_value(state, name=:default)
def aasm_raw_attribute_value(state, name=:default) if aasm_enum(name) self.class.send(aasm_enum(name))[state] else super end end
def aasm_read_attribute(name)
def aasm_read_attribute(name) read_attribute(name) end
def aasm_save
def aasm_save self.save end
def aasm_transaction(requires_new, requires_lock)
def aasm_transaction(requires_new, requires_lock) self.class.transaction(:requires_new => requires_new) do lock!(requires_lock) if requires_lock yield end end
def aasm_update_column(attribute_name, value)
def aasm_update_column(attribute_name, value) self.class.unscoped.where(self.class.primary_key => self.id).update_all(attribute_name => value) == 1 end
def aasm_validate_states
def aasm_validate_states AASM::StateMachineStore.fetch(self.class, true).machine_names.each do |state_machine_name| unless aasm_skipping_validations(state_machine_name) if aasm_invalid_state?(state_machine_name) self.errors.add(AASM::StateMachineStore.fetch(self.class, true).machine(state_machine_name).config.column , "is invalid") end end end end
def aasm_write_attribute(name, value)
def aasm_write_attribute(name, value) write_attribute(name, value) end