Sha256: f95e1c30ed41330a7e2583783f7fc82ca094994798de0b6b34d3adb337d86e86

Contents?: true

Size: 963 Bytes

Versions: 2

Compression:

Stored size: 963 Bytes

Contents

module ResourcesController
  module LocationHistory
    extend ActiveSupport::Concern

    included do
      if respond_to?(:before_action)
        before_action :store_location
      else
        before_filter :store_location
      end

      helper_method :last_location
    end

    private

    def store_location
      return if request.referer.nil?
      truncate_location_history(9)
      location_history[Time.zone.now] = request.referer
    end

    def location_history
      session[:location_history] ||= {}
    end

    def last_location
      location_history.sort.last.try(:last)
    end

    def truncate_location_history(count = 0)
      return if location_history.size <= count
      truncated = session[:location_history].sort.last(count)
      session[:location_history] = if truncated.respond_to?(:to_h)
        truncated.to_h
      else
        truncated.each_with_object({}) { |a, hash| hash[a.first] = a.last }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails-add_ons-2.2.1 app/concerns/resources_controller/location_history.rb
rails-add_ons-2.2.0 app/concerns/resources_controller/location_history.rb