DistroPropaganda/code/rails31/depot_s/app/controllers/application_controller.rb
2018-11-24 13:20:20 +01:00

54 satır
1,5 KiB
Ruby

#---
# Excerpted from "Agile Web Development with Rails",
# published by The Pragmatic Bookshelf.
# Copyrights apply to this code. It may not be used to create training material,
# courses, books, articles, and the like. Contact us if you are in doubt.
# We make no guarantees that this code is fit for any purpose.
# Visit http://www.pragmaticprogrammer.com/titles/rails4 for more book information.
#---
class ApplicationController < ActionController::Base
before_filter :set_i18n_locale_from_params
# ...
before_filter :authorize
protect_from_forgery
private
def current_cart
Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart = Cart.create
session[:cart_id] = cart.id
cart
end
# ...
protected
def authorize
if request.format == Mime::HTML
unless User.find_by_id(session[:user_id])
redirect_to login_url, notice: "Please log in"
end
else
authenticate_or_request_with_http_basic do |username, password|
user = User.find_by_name(username)
user && user.authenticate(password)
end
end
end
def set_i18n_locale_from_params
if params[:locale]
if I18n.available_locales.include?(params[:locale].to_sym)
I18n.locale = params[:locale]
else
flash.now[:notice] =
"#{params[:locale]} translation not available"
logger.error flash.now[:notice]
end
end
end
def default_url_options
{ locale: I18n.locale }
end
end