rosa-build/app/controllers/application_controller.rb

85 lines
2.6 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
2011-03-09 13:13:36 +00:00
class ApplicationController < ActionController::Base
protect_from_forgery
layout :layout_by_resource
# Hack to prevent token auth on all pages except atom feed:
prepend_before_filter lambda { redirect_to(new_user_session_path) if params[:token] && params[:format] != 'atom'}
before_filter :set_locale
before_filter lambda { EventLog.current_controller = self },
:only => [:create, :destroy, :open_id, :cancel, :publish, :change_visibility] # :update
after_filter lambda { EventLog.current_controller = nil }
helper_method :get_owner
rescue_from CanCan::AccessDenied do |exception|
respond_to do |format|
format.json { render :json => {:message => t("flash.exception_message")}.to_json }
format.html { redirect_to forbidden_url, :alert => t("flash.exception_message") }
end
end
2012-08-13 10:58:26 +01:00
if !Rails.env.development?
#rescue_from Exception do |exception|
# respond_to do |format|
# format.json { render :json => {:message => t("flash.500_message")}.to_json }
# format.html { redirect_to '/500.html', :alert => t("flash.500_message") }
# end
#end
rescue_from ActiveRecord::RecordNotFound,
ActionController::RoutingError,
ActionController::UnknownController,
ActionController::UnknownAction do |exception|
respond_to do |format|
format.json { render :json => {:message => t("flash.404_message")}.to_json }
format.html { redirect_to '/404.html', :alert => t("flash.404_message") }
end
end
end
rescue_from Grit::NoSuchPathError, :with => :not_found
protected
def set_locale
I18n.locale = check_locale( get_user_locale ||
2012-05-17 11:28:58 +01:00
(request.env['HTTP_ACCEPT_LANGUAGE'] ? request.env['HTTP_ACCEPT_LANGUAGE'][0,2].downcase : nil ))
end
def get_user_locale
user_signed_in? ? current_user.language : nil
end
def check_locale(locale)
User::LANGUAGES.include?(locale.to_s) ? locale : :en
end
def get_owner
if self.class.method_defined? :parent
if parent and (parent.is_a? User or parent.is_a? Group)
return parent
2011-11-30 12:58:14 +00:00
else
return current_user
2011-11-30 12:58:14 +00:00
end
else
params['user_id'] && User.find_by_id(params['user_id']) ||
params['group_id'] && Group.find_by_id(params['group_id']) || current_user
end
end
def layout_by_resource
if devise_controller? && !(params[:controller] == 'devise/registrations' && ['edit', 'update'].include?(params[:action]))
"sessions"
else
"application"
end
end
def not_found
raise ActionController::RoutingError.new('Not Found')
end
2011-03-09 13:13:36 +00:00
end