Module: BackMark::ControllerMethods::InstanceMethods

Defined in:
lib/back_mark.rb

Instance Method Summary collapse

Instance Method Details

#back_mark(label, url_to_mark = request.url, mark_now = false) ⇒ Object

Marks the current url with the given label. Invoke from an action with a meaningful label, if you want that page to be linked back from future pages

Params

label

label for the back mark

url_to_mark

the url to remember instead of the current request url

mark_now

Mark the location so that the back link can be rendered in the current action

Examples
back_mark("Inbox")
back_mark("Home")
back_mark("Login", '/login', true)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/back_mark.rb', line 49

def back_mark(label, url_to_mark = request.url, mark_now = false)
  # Ignore AJAX requests since they cannot be linked back.
  return if request.xhr?

  # Mark directly into back_url directly so that we can render in the
  # current action itself.
  if mark_now
    session[:back_label] = session[:prev_label] = label
    session[:back_url] = session[:prev_label] = url_to_mark
    @back_marked = true
    return
  end

  # Set back url and label from previously back marked page
  session[:back_url] = session[:prev_url]
  session[:back_label] = session[:prev_label]

  # Mark the current page
  session[:prev_url] = url_to_mark
  session[:prev_label] = label
  @back_marked = true
end

#redirect_to_back_mark_or_default(default_url) ⇒ Object

Redirect to the back link stored in the session or redirect to the default url passed.

NOTE: We redirect back to the url stored by the filter. Not the last back_marked url.



78
79
80
81
82
83
# File 'lib/back_mark.rb', line 78

def redirect_to_back_mark_or_default(default_url)
  redirect_to((@marked_in_filter ? session[:filter_back_url] :
    session[:filter_prev_url]) || default_url)

  session[:filter_back_url] = nil
end