Sha256: b384437808cc5b1de40fce7e6316e117638bcb4877b6358a3aeb95b47bba3983

Contents?: true

Size: 1.1 KB

Versions: 4

Compression:

Stored size: 1.1 KB

Contents

module Shoppe
  class ApplicationController < ActionController::Base
    
    # Require that a user is logged in for all parts of the Shoppe admin
    # interface.
    before_filter :login_required
    
    rescue_from ActiveRecord::DeleteRestrictionError do |e|
      redirect_to request.referer || root_path, :alert => e.message
    end

    private

    # If not logged in, redirect users to the login page. This should be
    # used in a before filter.
    def login_required
      unless logged_in?
        redirect_to login_path
      end
    end

    # Is there a user currently logged in?
    def logged_in?
      current_user.is_a?(User)
    end
    
    # Returns the currently logged in user
    def current_user
      @current_user ||= login_from_session || :false
    end

    # Attempt to find a user based on the value stored in the local session
    def login_from_session
      if session[:shoppe_user_id]
        @user = User.find_by_id(session[:shoppe_user_id])
      end
    end
    
    # Expose a current_user and logged_in? as helpers to views
    helper_method :current_user, :logged_in?
    
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
shoppe-0.0.14 app/controllers/shoppe/application_controller.rb
shoppe-0.0.13 app/controllers/shoppe/application_controller.rb
shoppe-0.0.12 app/controllers/shoppe/application_controller.rb
shoppe-0.0.11 app/controllers/shoppe/application_controller.rb