Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mikhail Gulin
committed
Feb 22, 2012
1 parent
d701dbd
commit 01bc8fc
Showing
7 changed files
with
106 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module RecordHistory | ||
VERSION = "0.0.1" | ||
VERSION = "0.0.2" | ||
end |
Binary file not shown.