1 #---
2 # Excerpted from "Agile Web Development with Rails, 3rd Ed.",
3 # published by The Pragmatic Bookshelf.
4 # Copyrights apply to this code. It may not be used to create training material,
5 # courses, books, articles, and the like. Contact us if you are in doubt.
6 # We make no guarantees that this code is fit for any purpose.
7 # Visit http://www.pragmaticprogrammer.com/titles/rails3 for more book information.
8 #---
9 # Filters added to this controller apply to all controllers in the application.
10 # Likewise, all the methods added will be available for all controllers.
11
12
13
14 class ApplicationController < ActionController::Base
15 layout "store"
16 before_filter :authorize, :except => :login
17 before_filter :set_locale
18 #...
19
20
21 helper :all # include all helpers, all the time
22
23 # See ActionController::RequestForgeryProtection for details
24 # Uncomment the :secret if you're not using the cookie session store
25 protect_from_forgery :secret => '8fc080370e56e929a2d5afca5540a0f7'
26
27 # See ActionController::Base for details
28 # Uncomment this to filter the contents of submitted sensitive data parameters
29 # from your application log (in this case, all fields with names like "password").
30 # filter_parameter_logging :password
31
32
33
34 protected
35
36 def authorize
37 unless User.find_by_id(session[:user_id])
38 if session[:user_id] != :logged_out
39
40 authenticate_or_request_with_http_basic('Depot') do |username, password|
41 user = User.authenticate(username, password)
42 session[:user_id] = user.id if user
43 end
44
45 else
46 flash[:notice] = "Please log in"
47 redirect_to :controller => 'admin', :action => 'login'
48 end
49 end
50 end
51
52
53 def set_locale
54 session[:locale] = params[:locale] if params[:locale]
55 I18n.locale = session[:locale] || I18n.default_locale
56
57 locale_path = "#{LOCALES_DIRECTORY}#{I18n.locale}.yml"
58
59 unless I18n.load_path.include? locale_path
60 I18n.load_path << locale_path
61 I18n.backend.send(:init_translations)
62 end
63
64 rescue Exception => err
65 logger.error err
66 flash.now[:notice] = "#{I18n.locale} translation not available"
67
68 I18n.load_path -= [locale_path]
69 I18n.locale = session[:locale] = I18n.default_locale
70 end
71 end
72
73