Sha256: a5aeb80d15a96b05cffca1b20cac89512b28bb671bcb22daba595b90f5785ff4

Contents?: true

Size: 732 Bytes

Versions: 1

Compression:

Stored size: 732 Bytes

Contents

module Memento
  class Session < ActiveRecord::Base
    self.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
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
memento-0.4.0 lib/memento/session.rb