module ActiveRecord #:nodoc: module CreateUpdateBy def self.included(base)#:nodoc: super base.class_eval do # alias (new name, old name) alias_method :old_save, :save alias_method :save, :save_with_user alias_method :old_update_attributes, :update_attributes alias_method :update_attributes, :update_attributes_with_user end end # modified update_attributes to accept a user id as a parameter # This parameter will be mapped to the 'updated_at' column in the # database if it exists. def update_attributes_with_user(attributes, user=nil) self.attributes = attributes save(user) # should actually call save_with_user via alias method end # Save takes an optional 'user id' parameter and will attempt # to record the value of that user id in the columns # 'created_by' and 'updated_by' if the model contains those attributes. def save_with_user(user = nil) raise ActiveRecord::ReadOnlyRecord if readonly? unless user == nil write_attribute(:created_by, user) if respond_to?(:created_by) and self.created_by.nil? write_attribute(:updated_by, user) if respond_to?(:updated_by) end old_save end end end