module AwesomeBackUrl::Helpers

Public Instance Methods

awesome_back_path(path = request.referrer, only: nil, except: nil, fallback: :back) click to toggle source
# File lib/awesome_back_url.rb, line 11
def awesome_back_path(path = request.referrer, only: nil, except: nil, fallback: :back)
  return fallback if path.blank?
  return fallback if !request.path.blank? && path =~ /#{request.path}$/

  if only.present?
    raise "Only regexp is allowed for \"only\" parameter" unless only.is_a?(Regexp)
  end

  if except.present?
    raise "Only regexp is allowed for \"except\" parameter" unless except.is_a?(Regexp)
  end

  # by default we assume path is OK
  only_pass   = true
  except_pass = true

  only_pass   = path.match?(only) if only.present?
  except_pass = !path.match?(except) if except.present?

  if only_pass && except_pass
    path
  else
    fallback
  end
end
Also aliased as: awesome_back_url
awesome_back_url(path = request.referrer, only: nil, except: nil, fallback: :back)
Alias for: awesome_back_path