mileszs (owner)

Revisions

gist: 230119 Download_button fork
public
Public Clone URL: git://gist.github.com/230119.git
Embed All Files: show embed
user_stamp.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module ActiveRecord
 
  module UserStamp
    
    def self.included(base)
      base.class_eval do
        alias_method :create_without_user, :create
        alias_method :create, :create_with_user
 
        alias_method :update_without_user, :update
        alias_method :update, :update_with_user
      end
    end
 
    def create_with_user
      user_id = User.current_user
      self[:created_by] = user_id if respond_to?(:created_by) && created_by.nil?
      self[:updated_by] = user_id if respond_to?(:updated_by) && updated_by.nil?
      create_without_user
    end
 
    def update_with_user
      user_id = User.current_user
      self[:updated_by] = user_id if respond_to?(:updated_by)
      update_without_user
    end
 
    def created_by
      begin
        User.find(self[:created_by])
      rescue ActiveRecord::RecordNotFound
        nil
      end
    end
 
    def updated_by
      begin
        User.find(self[:updated_by])
      rescue ActiveRecord::RecordNotFound
        nil
      end
    end
 
  end
 
end