module RestyPrefill

Constants

VERSION

Public Instance Methods

clear_redirect_and_prefill_for(obj) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/resty_prefill.rb, line 5
def redirect_and_prefill_for(obj)
  (session[:redirect_and_prefill] ||= {})[obj.class.to_s] ||= {
    attrs: {},
    errors: []
  }
end