闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class ApplicationController < ActionController::Base
  3. # Prevent CSRF attacks by raising an exception.
  4. # For APIs, you may want to use :null_session instead.
  5. protect_from_forgery with: :exception
  6. force_ssl if: "Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'"
  7. helper_method :current_account
  8. rescue_from ActionController::RoutingError, with: :not_found
  9. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  10. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  11. before_action :set_locale
  12. def raise_not_found
  13. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  14. end
  15. private
  16. def store_current_location
  17. store_location_for(:user, request.url)
  18. end
  19. def set_locale
  20. I18n.locale = current_user.try(:locale) || I18n.default_locale
  21. rescue I18n::InvalidLocale
  22. I18n.locale = I18n.default_locale
  23. end
  24. protected
  25. def not_found
  26. respond_to do |format|
  27. format.any { head 404 }
  28. end
  29. end
  30. def gone
  31. respond_to do |format|
  32. format.any { head 410 }
  33. end
  34. end
  35. def current_account
  36. current_user.try(:account)
  37. end
  38. end