Class: Volt::Persistors::Cookies

Inherits:
Base show all
Defined in:
lib/volt/models/persistors/cookies.rb

Overview

Backs a collection in the local store

Instance Method Summary collapse

Methods inherited from Base

#clear, #event_added, #event_removed, #root_model

Constructor Details

#initialize(model) ⇒ Cookies

Returns a new instance of Cookies.



48
49
50
# File 'lib/volt/models/persistors/cookies.rb', line 48

def initialize(model)
  @model = model
end

Instance Method Details

#added(model, index) ⇒ Object

Called when a model is added to the collection



53
54
55
# File 'lib/volt/models/persistors/cookies.rb', line 53

def added(model, index)
  # Save an added cookie
end

#changed(attribute_name) ⇒ Object

Callled when an cookies value is changed



75
76
77
78
79
80
81
82
83
# File 'lib/volt/models/persistors/cookies.rb', line 75

def changed(attribute_name)
  # TODO: Make sure we're only assigning directly, not sub models
  unless $writing_cookies
    value = @model.get(attribute_name)

    # Temp, expire in 1 year, going to expand this api
    write_cookie(attribute_name, value.to_s, expires: Time.now + (356 * 24 * 60 * 60), path: '/')
  end
end

#loaded(initial_state = nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/volt/models/persistors/cookies.rb', line 57

def loaded(initial_state = nil)
  # When the main model is first loaded, we pull in the data from the
  # store if it exists
  if !@cookies_loaded && @model.path == []
    @cookies_loaded = true

    writing_cookies do
      # Assign directly so we don't trigger the callbacks on the initial load
      attrs = @model.attributes

      read_cookies.each_pair do |key, value|
        attrs[key.to_sym] = value
      end
    end
  end
end

#read_cookiesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/volt/models/persistors/cookies.rb', line 10

def read_cookies
  cookies = `document.cookie`
  Hash[cookies.split(';').map do |v|
    # Equals are valid as part of a cookie, so only parse the first equals.
    parts = v.split('=', 2).map { |p| p = p.strip; `decodeURIComponent(p)` }

    # Default to empty if no value
    parts << '' if parts.size == 1

    # Equals are valid in
    parts
  end]
end

#removed(attribute_name) ⇒ Object



85
86
87
88
89
# File 'lib/volt/models/persistors/cookies.rb', line 85

def removed(attribute_name)
  writing_cookies do
    write_cookie(attribute_name, '', max_age: 0, path: '/')
  end
end


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/volt/models/persistors/cookies.rb', line 24

def write_cookie(key, value, options = {})
  options[:path] ||= '/'
  parts = []

  parts << `encodeURIComponent(key)`
  parts << '='
  parts << `encodeURIComponent(value)`
  parts << '; '

  parts << 'path='    << options[:path] << '; '           if options[:path]
  parts << 'max-age=' << options[:max_age] << '; '        if options[:max_age]

  if (expires = options[:expires])
    parts << 'expires=' << `expires.toGMTString()` << '; '
  end

  parts << 'domain='  << options[:domain] << '; '         if options[:domain]
  parts << 'secure'                                       if options[:secure]

  cookie_val = parts.join

  `document.cookie = cookie_val`
end

#writing_cookiesObject



91
92
93
94
95
# File 'lib/volt/models/persistors/cookies.rb', line 91

def writing_cookies
  $writing_cookies = true
  yield
  $writing_cookies = false
end