Sha256: 729b05ecb788cc2148af1feefa6343cd4f8826f95edf09505a7b750f28b18ef6

Contents?: true

Size: 682 Bytes

Versions: 4

Compression:

Stored size: 682 Bytes

Contents

class Memento::Session < ActiveRecord::Base
  set_table_name "memento_sessions"
  
  has_many :states, :class_name => "Memento::State", :dependent => :delete_all, :order => "id DESC"
  belongs_to :user
  validates_presence_of :user
  
  def add_state(action_type, record)
    states.store(action_type, record)
  end
  
  def undo
    states.map(&:undo).inject(Memento::ResultArray.new) do |results, result|
      result.state.destroy if result.success?
      results << result
    end
  ensure
    destroy if states.count.zero?
  end
  
  def undo!
    transaction do
      undo.tap do |results|
        raise Memento::ErrorOnRewind if results.failed?
      end
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
memento-0.3.5 lib/memento/session.rb
memento-0.3.4 lib/memento/session.rb
memento-0.3.3 lib/memento/session.rb
memento-0.3.2 lib/memento/session.rb