Sha256: 304c35c9996255964729d526101eac54c51e0e0b8cb3a1f5504f635f69cbefc9

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

module Memento
  class State < ActiveRecord::Base
    self.table_name = "memento_states"

    belongs_to :session, :class_name => "Memento::Session"
    belongs_to :record, :polymorphic => true

    validates_presence_of :session
    validates_presence_of :record
    validates_presence_of :action_type
    validates_inclusion_of :action_type, :in => Memento::Action::Base.action_types, :allow_blank => true

    before_create :set_record_data

    def self.store(action_type, record)
      self.new(:action_type => action_type.to_s, :record => record) do |state|
        state.save if state.fetch?
      end
    end

    def undo
      Memento::Result.new(action.undo, self)
    end

    def record_data
      @record_data ||= Memento.serializer.load(read_attribute(:record_data))
    end

    def record_data=(data)
      @record_data = nil
      write_attribute(:record_data, data.is_a?(String) ? data : Memento.serializer.dump(data))
    end

    def fetch?
      action.fetch?
    end

    private

    def set_record_data
      self.record_data = action.fetch
    end

    def action
      "memento/action/#{action_type}".classify.constantize.new(self)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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