Permalink
Browse files

writing hist

  • Loading branch information...
1 parent d701dbd commit 01bc8fcb678174c6d88aeac2b6b2ded61fac5ea1 Mikhail Gulin committed Feb 22, 2012
View
@@ -0,0 +1,18 @@
+PATH
+ remote: .
+ specs:
+ record_history (0.0.1)
+
+GEM
+ remote: http://rubygems.org/
+ specs:
+ redcarpet (1.17.2)
+ yard (0.7.5)
+
+PLATFORMS
+ ruby
+
+DEPENDENCIES
+ record_history!
+ redcarpet (~> 1.17)
+ yard (~> 0.7.5)
@@ -4,9 +4,10 @@ def self.up
t.string :item_type, :null => false
t.integer :item_id, :null => false
t.string :attr_name, :null => false
- t.text :old_value
- t.text :new_value
+ t.text :old_value_dump, :null => false
+ t.text :new_value_dump, :null => false
t.integer :author_id
+ t.datetime :created_at
end
add_index :record_histories, [:item_type, :item_id, :attr_name]
end
View
@@ -1,5 +1,11 @@
require "record_history/version"
+require "record_history/record_history_model"
+require "record_history/has_record_history"
module RecordHistory
end
+
+ActiveSupport.on_load(:active_record) do
+ include RecordHistory::Model
+end
@@ -0,0 +1,54 @@
+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
@@ -0,0 +1,24 @@
+class RecordHistoryModel < ActiveRecord::Base
+ self.table_name = "record_histories"
+
+ belongs_to :item, :polymorphic => true
+ validates :item_type, :item_id, :attr_name, :old_value_dump, :new_value_dump, { :presence => true }
+
+ def old_value
+ self.old_value = nil if self.old_value_dump.nil?
+ self.old_value_dump.nil? ? nil : Marshal.load(self.old_value_dump)
+ end
+
+ def old_value=(value)
+ self.old_value_dump = Marshal.dump(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
+end
@@ -1,3 +1,3 @@
module RecordHistory
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
Binary file not shown.

0 comments on commit 01bc8fc

Please sign in to comment.