Module: Card::Env::LocationHistory

Included in:
Card::Env
Defined in:
card/lib/card/env/location_history.rb

Overview

session history helpers: we keep a history stack so that in the case of card removal we can crawl back up to the last un-removed location

Instance Method Summary collapse

Instance Method Details

#discard_locations_for(card) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'card/lib/card/env/location_history.rb', line 31

def discard_locations_for card
  # quoting necessary because cards have things like "+*" in the names..
  session[:history] = location_history.reject do |loc|
    if (url_key = url_key_for_location(loc))
      url_key.to_name.key == card.key
    end
  end.compact
  session[:previous_location] = nil
end

#interrupted_actionObject



45
46
47
# File 'card/lib/card/env/location_history.rb', line 45

def interrupted_action
  session.delete :interrupted_action
end

#location_historyObject



6
7
8
9
10
# File 'card/lib/card/env/location_history.rb', line 6

def location_history
  session[:history] ||= [Env::Location.card_path("")]
  session[:history].shift if session[:history].size > 5
  session[:history]
end

#previous_locationObject



25
26
27
28
29
# File 'card/lib/card/env/location_history.rb', line 25

def previous_location
  return unless location_history

  session[:previous_location] ||= location_history.last
end

#save_interrupted_action(uri) ⇒ Object



41
42
43
# File 'card/lib/card/env/location_history.rb', line 41

def save_interrupted_action uri
  session[:interrupted_action] = uri
end

#save_location(card) ⇒ Object



12
13
14
15
16
17
18
19
# File 'card/lib/card/env/location_history.rb', line 12

def save_location card
  return unless save_location?(card)

  discard_locations_for card
  session[:previous_location] =
    Env::Location.card_path card.name.url_key
  location_history.push previous_location
end

#save_location?(card) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'card/lib/card/env/location_history.rb', line 21

def save_location? card
  !Env.ajax? && Env.html? && card.known? && (card.codename != :signin)
end

#url_key_for_location(location) ⇒ Object



49
50
51
# File 'card/lib/card/env/location_history.rb', line 49

def url_key_for_location location
  %r{/([^/]*$)} =~ location ? Regexp.last_match[1] : nil
end