Module: AwesomeBackUrl::Helpers

Extended by:
ActiveSupport::Concern
Defined in:
lib/awesome_back_url.rb

Instance Method Summary collapse

Instance Method Details

#awesome_back_path(path = request.referrer, only: nil, except: nil, fallback: :back) ⇒ Object Also known as: awesome_back_url



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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