Created
November 14, 2011 17:10
-
-
Save davidjbeveridge/1364473 to your computer and use it in GitHub Desktop.
My application controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
helper_method :current_user_session, :current_user | |
before_filter :ensure_domain | |
before_filter :production_down | |
APP_DOMAIN = 'davidbeveridge.net' | |
protected | |
def ensure_domain | |
if Rails.env.production? | |
if request.env['HTTP_HOST'] != APP_DOMAIN | |
# HTTP 301 is a "permanent" redirect | |
redirect_to "http://#{APP_DOMAIN}", :status => 301 | |
end | |
end | |
end | |
def current_user | |
@current_user = current_user_session.record if (current_user_session and current_user_session.record) | |
end | |
def require_user! | |
(login_page and return) unless current_user | |
end | |
def require_admin! | |
(login_page and return) unless is_admin? | |
end | |
def require_no_user! | |
return | |
(confirm_logout_page and return) if current_user | |
end | |
def is_admin? | |
current_user and current_user.admin? | |
end | |
def store_location | |
session[:return_to] = request.request_uri | |
end | |
def redirect_back_or_default(default=nil) | |
redirect_to(session[:return_to] || default || root_url) | |
session[:return_to] = nil | |
end | |
def login_page | |
store_location | |
redirect_to(login_url) and return | |
end | |
def logout_page | |
store_location | |
redirect_to(confirm_logout_url) and return | |
end | |
# Temp page | |
def production_down | |
if Rails.env.production? | |
render(:nothing => true, :layout => 'comingsoon') and return | |
end | |
end | |
# 404 | |
# def method_missing(methodname, *args) | |
# @methodname = methodname | |
# @args = args | |
# render 'shared/404', :status => 404 | |
# end | |
# 403 | |
def forbidden_403 | |
render "shared/403", :status => 403 | |
end | |
def not_found_404 | |
render "shared/404", :status => 404 | |
end | |
private | |
def current_user_session | |
return @user_session if defined?(@user_session) | |
@user_session = UserSession.find | |
end | |
def render(opts=nil, extra_opts={},&block) | |
extra_opts.merge({:layout => false}) if request.xhr? | |
super(opts, extra_opts, &block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment