Sha256: 56603ef89c34933152e0ca2afa5f1d29ddc4e2807a7cc8e8c40191b6e48a17c0

Contents?: true

Size: 1.34 KB

Versions: 8

Compression:

Stored size: 1.34 KB

Contents

module Ddb #:nodoc:
  module Userstamp
    module Stamper
      def self.included(base) # :nodoc:
        base.extend(ClassMethods)
      end

      module ClassMethods
        def model_stamper
          # don't allow multiple calls
          return if self.included_modules.include?(Ddb::Userstamp::Stamper::InstanceMethods)
          send(:extend, Ddb::Userstamp::Stamper::InstanceMethods)
        end
      end

      module InstanceMethods
        # Used to set the stamper for a particular request. See the Userstamp module for more
        # details on how to use this method.
        def stamper=(object)
          object_stamper = if object.is_a?(ActiveRecord::Base)
            object.send("#{object.class.primary_key}".to_sym)
          else
            object
          end

          Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
        end

        # Retrieves the existing stamper for the current request.
        def stamper
          Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
        end

        # Sets the stamper back to +nil+ to prepare for the next request.
        def reset_stamper
          Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
        end
      end
    end
  end
end

ActiveRecord::Base.send(:include, Ddb::Userstamp::Stamper) if defined?(ActiveRecord)

Version data entries

8 entries across 8 versions & 4 rubygems

Version Path
originator-3.1 lib/stamper.rb
originator-3.0 lib/stamper.rb
magiclabs-userstamp-3.0 lib/stamper.rb
grosser-userstamp-2.0.0 lib/ddb/userstamp/stamper.rb
magiclabs-userstamp-2.1.0 lib/stamper.rb
magiclabs-userstamp-2.0.2 lib/stamper.rb
userstamp-2.0.1 lib/stamper.rb
userstamp-2.0.0 lib/stamper.rb