2024-10-01 02:58:15 +05:30
|
|
|
module StoreLocation
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
helper_method :previous_path
|
|
|
|
before_action :store_return_to
|
|
|
|
after_action :clear_previous_path
|
2024-11-18 10:47:05 -05:00
|
|
|
|
|
|
|
rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found
|
2024-10-01 02:58:15 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def previous_path
|
|
|
|
session[:return_to] || fallback_path
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2024-11-18 10:47:05 -05:00
|
|
|
def handle_not_found
|
|
|
|
if request.fullpath == session[:return_to]
|
|
|
|
session.delete(:return_to)
|
|
|
|
redirect_to fallback_path
|
|
|
|
else
|
|
|
|
head :not_found
|
|
|
|
end
|
|
|
|
end
|
2024-10-01 02:58:15 +05:30
|
|
|
|
|
|
|
def store_return_to
|
|
|
|
if params[:return_to].present?
|
|
|
|
session[:return_to] = params[:return_to]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_previous_path
|
|
|
|
if request.fullpath == session[:return_to]
|
|
|
|
session.delete(:return_to)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def fallback_path
|
|
|
|
root_path
|
|
|
|
end
|
|
|
|
end
|