Class: Props::Deferment

Inherits:
Object
  • Object
show all
Defined in:
lib/props_template/extensions/deferment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, deferred = []) ⇒ Deferment

Returns a new instance of Deferment.



5
6
7
8
# File 'lib/props_template/extensions/deferment.rb', line 5

def initialize(base, deferred = [])
  @deferred = deferred
  @base = base
end

Instance Attribute Details

#deferredObject (readonly)

Returns the value of attribute deferred.



3
4
5
# File 'lib/props_template/extensions/deferment.rb', line 3

def deferred
  @deferred
end

Instance Method Details

#handle(options) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/props_template/extensions/deferment.rb', line 32

def handle(options)
  return if !options[:defer]

  type, rest = options[:defer]
  placeholder = rest[:placeholder]
  success_action = rest[:success_action]
  fail_action = rest[:fail_action]

  if type.to_sym == :auto && options[:key]
    key, val = options[:key]
    placeholder = {}
    placeholder[key] = val
  end

  request_path = @base.context.controller.request.fullpath
  path = @base.traveled_path.join(".")
  uri = ::URI.parse(request_path)
  qry = ::URI.decode_www_form(uri.query || "")
    .reject { |x| x[0] == "props_at" }
    .push(["props_at", path])

  uri.query = ::URI.encode_www_form(qry)

  deferral = {
    url: uri.to_s,
    path: path,
    type: type.to_s
  }

  # camelize for JS land
  deferral[:successAction] = success_action if success_action
  deferral[:failAction] = fail_action if fail_action

  @deferred.push(deferral)

  placeholder
end

#refine_options(options, item = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/props_template/extensions/deferment.rb', line 10

def refine_options(options, item = nil)
  return options if !options[:defer]
  pass_opts = options.clone

  type, rest = [*options[:defer]]
  rest ||= {
    placeholder: {}
  }

  if item
    type = (Proc === type) ? type.call(item) : type
  end

  if type
    pass_opts[:defer] = [type, rest]
  else
    pass_opts.delete(:defer)
  end

  pass_opts
end