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
|
2012-01-17 11:48:40 +00:00
|
|
|
|
2011-03-31 00:10:23 +01:00
|
|
|
layout :layout_by_resource
|
2011-10-21 15:57:29 +01:00
|
|
|
|
2012-04-18 16:15:04 +01:00
|
|
|
# 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'}
|
|
|
|
|
2012-01-17 11:48:40 +00:00
|
|
|
before_filter :set_locale
|
2011-10-28 23:31:47 +01:00
|
|
|
before_filter lambda { EventLog.current_controller = self },
|
2012-03-29 19:43:46 +01:00
|
|
|
:only => [:create, :destroy, :open_id, :cancel, :publish, :change_visibility] # :update
|
2011-10-14 21:45:58 +01:00
|
|
|
after_filter lambda { EventLog.current_controller = nil }
|
|
|
|
|
2011-10-27 13:49:21 +01:00
|
|
|
helper_method :get_owner
|
2012-03-07 18:47:19 +00:00
|
|
|
|
2011-11-15 20:05:08 +00:00
|
|
|
rescue_from CanCan::AccessDenied do |exception|
|
2012-09-27 13:13:59 +01:00
|
|
|
redirect_to forbidden_url, :alert => t("flash.exception_message")
|
2012-07-26 10:22:51 +01:00
|
|
|
end
|
|
|
|
|
2012-08-13 10:58:26 +01:00
|
|
|
if !Rails.env.development?
|
2012-03-07 18:47:19 +00:00
|
|
|
|
2012-08-09 15:38:41 +01:00
|
|
|
rescue_from ActiveRecord::RecordNotFound,
|
|
|
|
ActionController::RoutingError,
|
|
|
|
ActionController::UnknownController,
|
2012-09-26 18:15:11 +01:00
|
|
|
::AbstractController::ActionNotFound do |exception|
|
2012-08-09 15:38:41 +01:00
|
|
|
respond_to do |format|
|
2012-09-05 14:47:25 +01:00
|
|
|
format.json { render :json => {:message => t("flash.404_message")}.to_json, :status => 404 }
|
2012-08-09 15:38:41 +01:00
|
|
|
format.html { redirect_to '/404.html', :alert => t("flash.404_message") }
|
|
|
|
end
|
2012-08-08 15:08:56 +01:00
|
|
|
end
|
|
|
|
end
|
2012-07-26 10:22:51 +01:00
|
|
|
|
2012-08-09 15:38:41 +01:00
|
|
|
rescue_from Grit::NoSuchPathError, :with => :not_found
|
|
|
|
|
2011-03-31 00:10:23 +01:00
|
|
|
protected
|
2012-01-17 11:48:40 +00:00
|
|
|
|
|
|
|
def set_locale
|
2012-05-16 18:20:42 +01:00
|
|
|
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 ))
|
2012-01-17 11:48:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def get_user_locale
|
|
|
|
user_signed_in? ? current_user.language : nil
|
|
|
|
end
|
|
|
|
|
2012-01-17 11:57:46 +00:00
|
|
|
def check_locale(locale)
|
|
|
|
User::LANGUAGES.include?(locale.to_s) ? locale : :en
|
|
|
|
end
|
|
|
|
|
2012-01-17 11:48:40 +00:00
|
|
|
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
|
2012-01-17 11:48:40 +00:00
|
|
|
return current_user
|
2011-11-30 12:58:14 +00:00
|
|
|
end
|
2012-01-17 11:48:40 +00:00
|
|
|
else
|
|
|
|
params['user_id'] && User.find_by_id(params['user_id']) ||
|
|
|
|
params['group_id'] && Group.find_by_id(params['group_id']) || current_user
|
2011-10-26 21:57:51 +01:00
|
|
|
end
|
2012-01-17 11:48:40 +00:00
|
|
|
end
|
2011-10-26 21:57:51 +01:00
|
|
|
|
2012-01-17 11:48:40 +00:00
|
|
|
def layout_by_resource
|
2012-03-07 18:47:19 +00:00
|
|
|
if devise_controller? && !(params[:controller] == 'devise/registrations' && ['edit', 'update'].include?(params[:action]))
|
2012-01-17 11:48:40 +00:00
|
|
|
"sessions"
|
|
|
|
else
|
|
|
|
"application"
|
2011-03-31 00:10:23 +01:00
|
|
|
end
|
2012-01-17 11:48:40 +00:00
|
|
|
end
|
2012-07-17 09:02:56 +01:00
|
|
|
|
|
|
|
def not_found
|
|
|
|
raise ActionController::RoutingError.new('Not Found')
|
|
|
|
end
|
2011-03-09 13:13:36 +00:00
|
|
|
end
|