public
Description: 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.
Homepage:
Clone URL: git://github.com/delynn/userstamp.git
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
48334f21 » delynn 2008-04-05 Updating documentation. 17 # Used to set the stamper for a particular request. See the Userstamp module for more
18 # details on how to use this method.
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 19 def stamper=(object)
20 object_stamper = if object.is_a?(ActiveRecord::Base)
21 object.send("#{object.class.primary_key}".to_sym)
22 else
23 object
24 end
25
26 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
27 end
28
48334f21 » delynn 2008-04-05 Updating documentation. 29 # Retrieves the existing stamper for the current request.
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 30 def stamper
210dad9a » delynn 2008-04-16 Decided to not use AR#find ... 31 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 32 end
33
48334f21 » delynn 2008-04-05 Updating documentation. 34 # Sets the stamper back to +nil+ to prepare for the next request.
31bb8108 » delynn 2008-02-16 Adding in the CHANGELOG fil... 35 def reset_stamper
36 Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
37 end
38 end
39 end
40 end
41 end
42
9555b43d » delynn 2008-03-08 Removing the 'acts_as' bits... 43 ActiveRecord::Base.send(:include, Ddb::Userstamp::Stamper) if defined?(ActiveRecord)