Module: RestyPrefill

Defined in:
lib/resty_prefill.rb

Constant Summary collapse

VERSION =
'1.0.1'

Instance Method Summary collapse

Instance Method Details

#clear_redirect_and_prefill_for(obj) ⇒ Object



12
13
14
15
16
# File 'lib/resty_prefill.rb', line 12

def clear_redirect_and_prefill_for(obj)
  h = redirect_and_prefill_for(obj)
  h[:attrs] = {}
  h[:errors] = []
end

#prefill(obj) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/resty_prefill.rb', line 18

def prefill(obj)
  attrs = redirect_and_prefill_for(obj)[:attrs]
  attrs.each do |k,v|
    obj[k] = v
  end

  errs = redirect_and_prefill_for(obj)[:errors]
  errs.each do |attr, msg|
    obj.errors.add(attr, msg)
  end
ensure
  clear_redirect_and_prefill_for(obj)
end

#ready_prefill(obj, attrs) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/resty_prefill.rb', line 32

def ready_prefill(obj, attrs)
  redirect_and_prefill_for(obj)[:attrs] = attrs
  errs = []
  obj.errors.each do |attr, msg|
    errs << [attr, msg]
  end
  redirect_and_prefill_for(obj)[:errors] = errs
end

#redirect_and_prefill_for(obj) ⇒ Object



5
6
7
8
9
10
# File 'lib/resty_prefill.rb', line 5

def redirect_and_prefill_for(obj)
  (session[:redirect_and_prefill] ||= {})[obj.class.to_s] ||= {
    attrs: {},
    errors: []
  }
end