Sha256: 69c7164bdfb36f0ac90dec9be7f410928cdd880b6c8cf30594eb2673f4987421

Contents?: true

Size: 904 Bytes

Versions: 3

Compression:

Stored size: 904 Bytes

Contents

require 'base64'

class RecordHistoryModel < ActiveRecord::Base
	self.table_name = "record_histories"

	belongs_to :item, :polymorphic => true
	belongs_to :author, :polymorphic => true
	validates :item_type, :item_id, { :presence => true }
	validates :old_value_dump, :new_value_dump, :presence => { :unless => Proc.new{|record| record.attr_name.blank?} }

	def old_value
		self.old_value = nil if self.old_value_dump.nil?
		self.old_value_dump.nil? ? nil : decode_value(self.old_value_dump)
	end

	def old_value=(value)
		self.old_value_dump = encode_value(value)
	end

	def new_value
		self.new_value = nil if self.new_value_dump.nil?
		Marshal.load(self.new_value_dump)
	end

	def new_value=(value)
		self.new_value_dump = Marshal.dump(value)
	end

	private

	def encode_value(value)
		Base64.encode64(Marshal.dump(value))
	end

	def decode_value(value)
		Marshal.load(Base64.decode64(value))
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
record_history-0.8.8 lib/record_history/record_history_model.rb
record_history-0.8.7 lib/record_history/record_history_model.rb
record_history-0.8.6 lib/record_history/record_history_model.rb