module AASM::Bang::ClassMethods

Public Instance Methods

aasm_bang_event(name, options = {}, &block) click to toggle source
# File lib/aasm/bang.rb, line 11
def aasm_bang_event(name, options = {}, &block)
  aasm_event(name, options, &block)
  define_aasm_bang_method(name)
end
define_aasm_bang_method(name) click to toggle source
# File lib/aasm/bang.rb, line 16
def define_aasm_bang_method(name)
  define_method("#{name}!") do |*args|
    options = args[-1].is_a?(Hash) ? args.pop : {}
    if options[:lifecycle] == false
      if send(name)
        self.class.where(:id => self.id).update_all(:updated_at => Time.now, :aasm_state => self.aasm_current_state)
      else
        raise(AASM::TransitionFailure, "cannot transition from '#{self.aasm_current_state}' to '#{name}'")
      end
    else
      old_state = self.aasm_current_state
      aasm_fire_event(name, true, *args) || begin
        if errors.any?
          raise(AASM::TransitionFailure, "record is invalid: #{errors.to_a.inspect}")
        else
          raise(AASM::TransitionFailure, "cannot transition from '#{old_state}' to '#{name}'")
        end
      end
    end
  end
end