-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsession_answer_store.rb
More file actions
51 lines (41 loc) · 1.12 KB
/
session_answer_store.rb
File metadata and controls
51 lines (41 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module Store
class SessionAnswerStore
include Store::Access
ANSWERS_KEY = :answers
LOCALES_KEY = :locales
def initialize(store, form_id)
@store = store
@form_key = form_id.to_s
@store[ANSWERS_KEY] ||= {}
@store[LOCALES_KEY] ||= {}
end
def save_step(step, answer)
@store[ANSWERS_KEY][@form_key] ||= {}
@store[ANSWERS_KEY][@form_key][page_key(step)] = answer
end
def get_stored_answer(step)
@store.dig(ANSWERS_KEY, @form_key, page_key(step))
end
def clear_stored_answer(step)
@store.dig(ANSWERS_KEY, @form_key)&.delete(page_key(step))
end
def clear
@store[ANSWERS_KEY][@form_key] = nil
@store[LOCALES_KEY][@form_key] = nil
end
def form_submitted?
@store[ANSWERS_KEY][@form_key].nil?
end
def answers
@store[ANSWERS_KEY][@form_key]
end
def add_locale(locale)
@store[LOCALES_KEY][@form_key] ||= []
@store[LOCALES_KEY][@form_key] |= [locale.to_s]
end
def locales_used
locales = @store.dig(LOCALES_KEY, @form_key) || []
locales.map(&:to_sym) || []
end
end
end