Last active
April 16, 2018 11:54
-
-
Save SylarRuby/e6fa879eb064db84f4f85d6fab8a3fca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationController < ActionController::Base | |
protect_from_forgery prepend: true, with: :exception | |
private | |
def smart_backlink_session | |
# Clear all other links when on home page - my preference | |
delete_session_paths_visited_if_on_dashboard | |
route_name = Rails.application.routes.router.recognize(request) { |route, _| route.name }.flatten.last.name | |
# This global variable will be passed to our views | |
@path_name = "#{request.env['ORIGINAL_FULLPATH']}|#{route_name}" | |
# We keep track on all page visited | |
# Do not add same path more than once in the array ie if user refreshes browser | |
session[:paths_visited] << @path_name if session[:paths_visited] and !session[:paths_visited].include?(@path_name) | |
end | |
# Delete and recreate the session with only the dashboard path in its array - again, my prefrence | |
def delete_session_paths_visited_if_on_dashboard | |
if request.env['ORIGINAL_FULLPATH'] == '/dashboard' | |
session.delete(:paths_visited) | |
# Check if your version of ruby supports "if not" or use unless | |
session[:paths_visited] = Array.new(1, dashboard_path) if not session[:paths_visited] | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment