app/controllers/alchemy/base_controller.rb in alchemy_cms-2.2.4 vs app/controllers/alchemy/base_controller.rb in alchemy_cms-2.3.rc5
- old
+ new
@@ -37,12 +37,34 @@
# Overriding +I18n+s default +t+ helper, so we can pass it through +Alchemy::I18n+
def t(key, *args)
::Alchemy::I18n.t(key, *args)
end
- private
+ private
+ # Sets Alchemy's GUI translation to users preffered language and stores it in the session.
+ #
+ # Guesses the language from browser locale. If not successful it takes the default.
+ #
+ # You can set the default translation in your +config/application.rb+ file, via Rails +default_locale+ config option.
+ #
+ # If one passes a locale parameter the locale is set to its value
+ #
+ def set_translation
+ if params[:locale].blank? && session[:current_locale].present?
+ ::I18n.locale = session[:current_locale]
+ elsif params[:locale].present? && ::I18n.available_locales.include?(params[:locale].to_sym)
+ session[:current_locale] = ::I18n.locale = params[:locale]
+ elsif current_user && current_user.language.present?
+ ::I18n.locale = current_user.language
+ elsif Rails.env == 'test' # OMG I hate to do this. But it helps...
+ ::I18n.locale = 'en'
+ else
+ ::I18n.locale = request.env['HTTP_ACCEPT_LANGUAGE'].try(:scan, /^[a-z]{2}/).try(:first)
+ end
+ end
+
# Sets the language for rendering pages in pages controller
def set_language
if params[:lang].blank? and session[:language_id].blank?
set_language_to_default
elsif !params[:lang].blank?
@@ -99,13 +121,33 @@
else
params[:options]
end
end
+ # Handles the layout rendering
+ #
+ # Can be used inside the controllerĀ“s +layout+ class method
+ #
+ # === Example:
+ # layout :layout_for_page
+ #
+ # === Usage:
+ # 1. You can pass none or false as url parameter to avoid any layout rendering.
+ # 2. You can pass a layout name of any existing layout file in +app/views/layouts+ folder.
+ #
+ # If no layout name is given, Alchemy tries to render +app/views/layouts/application/+ layout.
+ # If that is not present, Alchemy tries to render +app/views/layouts/alchemy/pages+ layout.
+ #
def layout_for_page
- if !params[:layout].blank? && params[:layout] != 'none'
- params[:layout]
+ if params[:layout] == 'none' || params[:layout] == 'false'
+ false
+ elsif !params[:layout].blank?
+ if File.exist?(Rails.root.join('app/views/layouts', "#{params[:layout]}.html.erb"))
+ params[:layout]
+ else
+ raise_not_found_error
+ end
elsif File.exist?(Rails.root.join('app/views/layouts', 'application.html.erb'))
'application'
else
'alchemy/pages'
end
@@ -114,13 +156,13 @@
def render_404(exception = nil)
if exception
logger.info "Rendering 404: #{exception.message}"
end
@page = Page.language_root_for(session[:language_id])
- render :file => "#{Rails.root}/public/404", :status => 404, :layout => !@page.nil?
+ render :file => Rails.root.join("public/404.html"), :status => 404, :layout => !@page.nil?
end
- protected
+ protected
def permission_denied
if current_user
if current_user.role == 'registered'
redirect_to alchemy.root_path