application_controller.rb revision 8441cc0be003fcd6294a1b5b93e1143b5bb82cea
76b43e4417bab52e913da39b5f5bc2a130d3f149Timo Sirainenclass ApplicationController < ActionController::Base
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen protect_from_forgery
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen ensure_security_headers
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
ffd9a1898a18fadfc5dce399162c25d50548f905Timo Sirainen include Pagination
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # CanCan Authorization
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rescue_from CanCan::AccessDenied do |exception|
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen redirect_to root_url, :alert => exception.message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2d49f150b4bce6f2f59a84e268e4777901c3e42cTimo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if defined? PG
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # A foreign key constraint exception from the database
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen rescue_from PG::Error do |exception|
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen message = exception.message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen if message.include?('foreign key constraint')
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen logger.warn(message)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # shorten the message
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen message = message.match(/DETAIL: .+/).to_s
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen redirect_to :back,
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen :flash => {:error => "Whatever you tried to do - the server is unable to process your request because of a foreign key constraint. (#{message})" }
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen else
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen # anything else
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen raise exception
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen protected
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen helper_method :admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen def admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen current_user.try(:admin?)
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen def authenticate_admin!
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen unless admin?
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen flash[:error] = "you need admin privileges for this action"
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen redirect_to :root
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen end
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainenend
2a34e2be33f8a17d21384a5527ed9f75f4d270e0Timo Sirainen