summaryrefslogtreecommitdiff
path: root/app/controllers/application_controller.rb
blob: b73a463e7131e7dbcd910c27a746f801a21005f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :set_locale, :initialize_pages

  class << self
    attr_accessor :current_site # a class instance variable
  end

  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 request.format == 'text/html'
      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
  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

  #
  # same as render page, but returns the string
  #
  def page_body(page)
    begin
      render_to_string :template => page.template_path
    rescue ActionView::MissingTemplate => exc
      begin
        render_to_string :template => page.template_path(DEFAULT_LOCALE)
      rescue
        raise exc
      end
    end
  end
  helper_method :page_body

  ##
  ## SITE
  ##

  #
  # if we ever make this code support multiple sites, this should depend on the request's domain
  #
  def site
    self.class.current_site ||= Site.new
  end
  helper_method :site

  def initialize_pages
    if Rails.env.development?
      site.load_pages
    else
      site.reload_pages_if_needed
    end
  end

  #def run_once(name, options={})
  #  key_name = "run_once_#{name}"
  #  if !Thread.current[key_name] || options[:unless]
  #    yield
  #  end
  #  Thread.current[key_name] = true
  #end

end