orbit4-5/app/controllers/application_controller.rb

66 lines
1.7 KiB
Ruby
Raw Normal View History

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
2014-04-01 07:10:21 +00:00
before_action :set_locale
helper_method :current_site, :current_user
2014-04-08 10:46:27 +00:00
2014-04-01 07:10:21 +00:00
def default_url_options(options={})
{ locale: I18n.locale }
end
def set_locale
2014-05-14 04:07:51 +00:00
in_use_locales = current_site.in_use_locales
if params[:locale]
session[:locale] = in_use_locales.include?(params[:locale].to_sym) ? params[:locale] : nil
end
if !params[:locale] and !session[:locale]
if current_site.enable_language_detection
browser_locale = request.env['HTTP_ACCEPT_LANGUAGE'].split(',').first.underscore rescue nil
session[:locale] = in_use_locales.include?(browser_locale.to_sym) ? browser_locale : nil
elsif current_site.default_locale
session[:locale] = current_site.default_locale
end
end
session[:locale] = session[:locale].blank? ? I18n.default_locale : session[:locale]
I18n.locale = session[:locale]
2014-04-01 07:10:21 +00:00
end
def get_layout
f = File.join("../../templates/", "#{@key}", '/home/page.html.erb')
end
def get_key
@key = Template::KEY
end
2014-04-08 10:46:27 +00:00
def current_site
2014-04-21 02:48:36 +00:00
@current_site = Site.first
2014-04-08 10:46:27 +00:00
end
private
def current_user
2014-04-21 02:48:36 +00:00
@current_user ||= User.find(session[:user_id]) if session[:user_id] rescue nil
end
protected
def authenticate_user
if session[:user_id]
# set current user object to @current_user object variable
2014-04-21 02:48:36 +00:00
@current_user = User.find(session[:user_id]) rescue nil
redirect_to new_session_path if @current_user.nil?
return true
else
redirect_to new_session_path
return false
end
end
end