Last active
December 10, 2015 14:59
-
-
Save svs/4451580 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class ActivityLog | |
include DataMapper::Resource | |
property :id, Serial | |
property :type, Discriminator | |
property :model_id, Integer | |
property :event, String | |
property :diff, JSON | |
property :user_id, Integer | |
property :staff_id, Integer | |
def self.save_with_log(object, saver, options, &blk) | |
if object.save | |
diff = object.attributes.reject{|k,v| options[:exclude_attributes]} | |
create( :event => "save", | |
:model_id => object.id, | |
:diff => diff, | |
:staff_id => saver.id if saver.class == Staff, | |
:user_id => object.user ? object.user.id : nil | |
) | |
else | |
return false | |
end | |
end | |
def self.update_with_log(object, attrs, saver, options, &blk) | |
orig_attrs = object.attributes | |
if object.update(attrs) | |
diff = orig_attrs.log_diff(object.attributes).reject{|k,v| options[:exclude_attributes].include?(k)} | |
create(:event => "update", | |
:model_id => object.id, | |
:diff => diff, | |
:staff_id => saver.id if saver.class == Staff, | |
:user_id => object.user ? object.user.id : nil) | |
end | |
end | |
end | |
class FooActivityLog < ActivityLog | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment