Skip to content

Instantly share code, notes, and snippets.

@the-undefined
Created February 20, 2014 11:56
Show Gist options
  • Save the-undefined/9111985 to your computer and use it in GitHub Desktop.
Save the-undefined/9111985 to your computer and use it in GitHub Desktop.
Thinking on a further refactoring on a nice blog post regarding flog, using a Session Model came to mind to assist with the responsibilities. POST: http://cored.github.io/blog/2014/02/19/my-gpa-at-code-climate-is-3-dot-59-a-refactoring-story
class LastTracks
attr_accessor :session, :list
def initialize(session)
@session = session
@list = []
yield(self)
end
def blacklist(url)
list << url
end
def previous_url=(came_from)
return unless valid_origin(came_from)
session[:previous_url] = came_from
end
private
def valid_origin(came_from)
!list.any? { |no_return| no_return =~ came_from }
end
end
# APPLICATION CONTROLLER
before_filter :record_tracks
def tracks
@tracks ||= LastTracks.new(session) do |tracks|
tracks.blacklist some_path
tracks.blacklist another_path
tracks.blacklist one_more_path
end
end
helper_method :tracks
def record_tracks
return if request.xhr?
tracks.previous_url = request.fullpath
end
@cored
Copy link

cored commented Feb 20, 2014

Very interesting approach :-)

@the-undefined
Copy link
Author

@cored Yeah, first got the hang of using Session Models from a railscast yonks ago: http://railscasts.com/episodes/119-session-based-model it's served me well since then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment