Sha256: d86550e23ad6ce1c7ca5042b686829a1dbd1f582422063df7709385d03149bbe

Contents?: true

Size: 1.33 KB

Versions: 1

Compression:

Stored size: 1.33 KB

Contents

module DeviseMasquerade
  module Controllers
    module Helpers
      def self.define_helpers(mapping)
        name = mapping.name
        class_name = mapping.class_name

        class_eval <<-METHODS, __FILE__, __LINE__ + 1
          def masquerade_#{name}!
            return if params[:masquerade].blank?

            #{name} = ::#{class_name}.find_by_masquerade_key(params[:masquerade])

            if #{name}
              if Devise.masquerade_bypass_warden_callback
                if respond_to?(:bypass_sign_in)
                  bypass_sign_in(#{name})
                else
                  sign_in(#{name}, :bypass => true)
                end
              else
                sign_in(#{name})
              end
            end
          end

          def #{name}_masquerade?
            session[:"devise_masquerade_#{name}"].present?
          end

          def #{name}_masquerade_owner
            return nil unless send(:#{name}_masquerade?)
            ::#{class_name}.to_adapter.find_first(:id => session[:"devise_masquerade_#{name}"])
          end
        METHODS

        ActiveSupport.on_load(:action_controller) do
          helper_method "#{name}_masquerade?"
          helper_method "#{name}_masquerade_owner"
        end
      end
    end
  end
end

ActionController::Base.send(:include, DeviseMasquerade::Controllers::Helpers)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
devise_masquerade-0.1.8 lib/devise_masquerade/controllers/helpers.rb