Sha256: 97c5274dfdaf679a0b14f20042b429c86a0e9214c8b400fd0bc0914a0bda3ff5

Contents?: true

Size: 776 Bytes

Versions: 4

Compression:

Stored size: 776 Bytes

Contents

module Memento
  class Session < ActiveRecord::Base
    self.table_name = "memento_sessions"

    has_many :states, -> { order "id DESC" },
             :class_name => "Memento::State", :dependent => :delete_all
    belongs_to :user

    # attr_accessible nil

    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
memento-0.5.2 lib/memento/session.rb
memento-0.5.1 lib/memento/session.rb
memento-0.5.0 lib/memento/session.rb
memento-0.4.3 lib/memento/session.rb