module AASM::Persistence::ActiveRecordPersistence::InstanceMethods

Private Instance Methods

aasm_column_is_blank?(state_machine_name) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 160
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
aasm_column_looks_like_enum(name=:default) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 118
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
aasm_ensure_initial_state() click to toggle source

Ensures that if the aasm_state column is nil and the record is new then the initial state gets populated after initialization

foo = Foo.new
foo.aasm_state # => "open" (where :open is the initial state)

foo = Foo.find(:first)
foo.aasm_state # => 1
foo.aasm_state = nil
foo.valid?
foo.aasm_state # => nil
# File lib/aasm/persistence/active_record_persistence.rb, line 150
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
aasm_enum(name=:default) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 109
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
aasm_execute_after_commit() { || ... } click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 64
        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
  [DEPRECATION] :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
aasm_guess_enum_method(name=:default) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 125
def aasm_guess_enum_method(name=:default)
  self.class.aasm(name).attribute_name.to_s.pluralize.to_sym
end
aasm_invalid_state?(state_machine_name) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 176
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
aasm_raise_invalid_record() click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 82
def aasm_raise_invalid_record
  raise ActiveRecord::RecordInvalid.new(self)
end
aasm_raw_attribute_value(state, name=:default) click to toggle source
Calls superclass method
# File lib/aasm/persistence/active_record_persistence.rb, line 129
def aasm_raw_attribute_value(state, name=:default)
  if aasm_enum(name)
    self.class.send(aasm_enum(name))[state]
  else
    super
  end
end
aasm_read_attribute(name) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 94
def aasm_read_attribute(name)
  read_attribute(name)
end
aasm_save() click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 86
def aasm_save
  self.save
end
aasm_transaction(requires_new, requires_lock) { || ... } click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 102
def aasm_transaction(requires_new, requires_lock)
  self.class.transaction(:requires_new => requires_new) do
    lock!(requires_lock) if requires_lock
    yield
  end
end
aasm_update_column(attribute_name, value) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 90
def aasm_update_column(attribute_name, value)
  self.class.unscoped.where(self.class.primary_key => self.id).update_all(attribute_name => value) == 1
end
aasm_validate_states() click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 166
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
aasm_write_attribute(name, value) click to toggle source
# File lib/aasm/persistence/active_record_persistence.rb, line 98
def aasm_write_attribute(name, value)
  write_attribute(name, value)
end