Sha256: 9c16db264e16dc97dde56db60762159a121cd8f1368197905aa15b62bd530a1a

Contents?: true

Size: 1.05 KB

Versions: 4

Compression:

Stored size: 1.05 KB

Contents

module Devise
  module Models
    module Masqueradable
      def self.included(base)
        base.class_eval do
          attr_reader :masquerade_key

          include InstanceMethods
          extend ClassMethods
        end
      end

      module InstanceMethods
        def masquerade!
          @masquerade_key = SecureRandom.urlsafe_base64(Devise.masquerade_key_size)

          Rails.cache.write("#{self.class.name.pluralize.downcase}:#{@masquerade_key}:masquerade", id, :expires_in => Devise.masquerade_expires_in)
        end
      end

      module ClassMethods
        def cache_masquerade_key_by(key)
          "#{self.name.pluralize.downcase}:#{key}:masquerade"
        end

        def remove_masquerade_key!(key)
          Rails.cache.delete(cache_masquerade_key_by(key))
        end

        def find_by_masquerade_key(key)
          id = Rails.cache.read(cache_masquerade_key_by(key))

          # clean up the cached masquerade key value
          remove_masquerade_key!(key)

          find_by_id(id)
        end
      end # ClassMethods
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
devise_masquerade-0.1.3 lib/devise_masquerade/model.rb
devise_masquerade-0.1.2 lib/devise_masquerade/model.rb
devise_masquerade-0.1.1 lib/devise_masquerade/model.rb
devise_masquerade-0.1.0 lib/devise_masquerade/model.rb