Sha256: 9aea28cf31024b3a0715d63130faed718bf1e56faab6a12a9b283cd5b724224f

Contents?: true

Size: 1.41 KB

Versions: 1

Compression:

Stored size: 1.41 KB

Contents

module RecordHistory
	module Model

		def self.included(base)
      base.send :extend, ClassMethods
    end

		module ClassMethods
			def has_record_history(options={})
				send :include, InstanceMethods

				attr_accessor :record_history_obj

				class_attribute :ignore
        self.ignore = ([options[:ignore]].flatten.compact || []).map &:to_s

        class_attribute :only
        self.only = ([options[:only]].flatten.compact || []).map{|attr| attr.to_s}

				has_many :record_history,
                 :class_name => 'RecordHistoryModel',
                 :order      => "created_at DESC",
                 :as         => :item

        before_save :build_record_history_obj
        after_save :save_record_history_obj
			end
		end

		module InstanceMethods


			def build_record_history_obj
        self.record_history_obj ||= []
				self.class.new.attributes.keys.each do |attr_name|
					if (self.send("#{attr_name}_changed?"))
						self.record_history_obj << RecordHistoryModel.new(
											:item_type => self.class.name,
                      :item_id => self.id,
                      :attr_name => attr_name,
                      :old_value => self.send("#{attr_name}_was"),
                      :new_value => self.send("#{attr_name}"),
                      :author_id => nil
						)
					end
				end
			end

			def save_record_history_obj
        self.record_history_obj.each{|item| item.save}
			end
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
record_history-0.0.2 lib/record_history/has_record_history.rb