diff options
Diffstat (limited to 'app/controllers')
-rw-r--r-- | app/controllers/application_controller.rb | 88 | ||||
-rw-r--r-- | app/controllers/pages_controller.rb | 29 | ||||
-rw-r--r-- | app/controllers/root_controller.rb | 11 |
3 files changed, 128 insertions, 0 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..62a4e13 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,88 @@ + +class ApplicationController < ActionController::Base + protect_from_forgery + before_filter :set_locale, :initialize_pages + + if Rails.env == 'production' + rescue_from Exception, :with => :render_500 + rescue_from ActionController::RoutingError, :with => :render_404 + end + + protected + + ## + ## LOCALIZATION + ## + + # + # ensure that the locale is encoded as the url prefix and I18n.locale is set. + # + def set_locale + if params[:locale].nil? || !AVAILABLE_LANGUAGES.include?(params[:locale]) + locale = HttpAcceptLanguage::compatible_language_from(request.headers['HTTP_ACCEPT_LANGUAGE'], AVAILABLE_LANGUAGES) || DEFAULT_LOCALE + locale = locale.to_s.sub('-', '_').sub(/_\w\w/, '') + if request.path == '/' + url = '/' + locale + else + url = url_for(params.merge(:locale => locale)) + end + redirect_to url + else + I18n.locale = params[:locale] + self.default_url_options[:locale] = params[:locale] + end + end + + #def default_url_options + # {:locale => locale } + #end + + ## + ## RENDERING + ## + + def render_500 + render :template => 'errors/error', :status => 500 + end + + def render_404 + render :template => 'errors/not_found', :status => 404 + end + + # + # renders the content of a static page + # + def render_page(page) + begin + render :template => page.template_path + rescue ActionView::MissingTemplate => exc + begin + render :template => page.template_path(DEFAULT_LOCALE) + rescue + raise exc + end + end + end + + ## + ## INITIALIZATION + ## + + # + # run every time in development mode, run once in production mode + # + def initialize_pages + run_once(:unless => Rails.env.development?) do + StaticPage.load(PAGE_DIRECTORY) + Menu.load(PAGE_DIRECTORY + '/menu.txt') + end + end + + def run_once(options={}) + if !@run_once || !options[:unless] + yield + end + @run_once = true + end + +end diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb new file mode 100644 index 0000000..c86f313 --- /dev/null +++ b/app/controllers/pages_controller.rb @@ -0,0 +1,29 @@ +class PagesController < ApplicationController + + class PageNotFound < Exception; end + + layout :choose_layout + rescue_from ActionView::MissingTemplate, :with => :render_404 + rescue_from PageNotFound, :with => :render_404 + + def show + @page = StaticPage.find(params[:page]) + if @page + render_page(@page) + else + raise PageNotFound.new + end + end + + protected + + def choose_layout + if @page && @page.props.layout + @page.props.layout + else + 'application' + end + end + +end + diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb new file mode 100644 index 0000000..939145d --- /dev/null +++ b/app/controllers/root_controller.rb @@ -0,0 +1,11 @@ +class RootController < ApplicationController + + # + # for now, just redirect to locale using before filter set in ApplicationController + # + def index + # never called + end + +end + |