delynn / userstamp

This Rails plugin extends ActiveRecord::Base to add automatic updating of created_by and updated_by attributes of your models in much the same way that the ActiveRecord::Timestamp module updates created_(at/on) and updated_(at/on) attributes.

This URL has Read+Write access

userstamp / lib / stamper.rb
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 1 module Ddb #:nodoc:
2 module Userstamp
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 3 module Stamper
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 4 def self.included(base) # :nodoc:
5 base.extend(ClassMethods)
6 end
7
8 module ClassMethods
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 9 def model_stamper
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 10 # don't allow multiple calls
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 11 return if self.included_modules.include?(Ddb::Userstamp::Stamper::InstanceMethods)
12 send(:extend, Ddb::Userstamp::Stamper::InstanceMethods)
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 13 end
14 end
15
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 16 module InstanceMethods
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 17 def stamper=(object)
18 object_stamper = if object.is_a?(ActiveRecord::Base)
19 object.send("#{object.class.primary_key}".to_sym)
20 else
21 object
22 end
23
24 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
25 end
26
27 def stamper
28 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
29 end
30
31 def reset_stamper
32 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
33 end
34 end
35 end
36 end
37 end
38
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 39 ActiveRecord::Base.send(:include, Ddb::Userstamp::Stamper) if defined?(ActiveRecord)