module Authenticatable::Controllers::Helpers

Public Class Methods

define_authenticate_helper(scope) click to toggle source
# File lib/authenticatable/controllers/helpers.rb, line 58
        def define_authenticate_helper(scope)
          class_eval <<-METHOD, __FILE__, __LINE__ + 1
            # def authenticate_user!
            #   request.env["authenticatable"].authenticate!(:user) unless authenticatable_controller?
            # end

            def authenticate_#{scope}!
              request.env["authenticatable"].authenticate!(:#{scope}) unless authenticatable_controller?
            end
          METHOD
        end
define_current_helper(scope) click to toggle source
# File lib/authenticatable/controllers/helpers.rb, line 34
        def define_current_helper(scope)
          class_eval <<-METHOD, __FILE__, __LINE__ + 1
            # def current_user
            #   @current_user ||= request.env["authenticatable"].authenticate(:user)
            # end

            def current_#{scope}
              @current_#{scope} ||= request.env["authenticatable"].authenticate(:#{scope})
            end
          METHOD
        end
define_helpers(scope) click to toggle source

Dynamically define helpers methods for the given scope For example, current_user, authenticate_user! and user_signed_in? will be generated for an authenticatable User model.

# File lib/authenticatable/controllers/helpers.rb, line 20
def define_helpers(scope)
  scope = scope.to_s

  define_current_helper(scope)
  define_signed_in_helper(scope)
  define_authenticate_helper(scope)

  # Make current_{scope} and {scope}_signed_in? available as helpers in views.
  ActiveSupport.on_load(:action_controller) do
    helpers = "current_#{scope}", "#{scope}_signed_in?"
    helper_method helpers if respond_to?(:helper_method)
  end
end
define_signed_in_helper(scope) click to toggle source
# File lib/authenticatable/controllers/helpers.rb, line 46
        def define_signed_in_helper(scope)
          class_eval <<-METHOD, __FILE__, __LINE__ + 1
            # def user_signed_in?
            #   !!current_user
            # end

            def #{scope}_signed_in?
              !!current_#{scope}
            end
          METHOD
        end

Public Instance Methods

authenticatable_controller?() click to toggle source

Check if the current controller is a AuthenticatableController

# File lib/authenticatable/controllers/helpers.rb, line 12
def authenticatable_controller?
  is_a?(::AuthenticatableController)
end