Module: BreezyTemplate::PartialExtension

Included in:
BreezyTemplate
Defined in:
lib/breezy_template/partial_extension.rb

Instance Method Summary collapse

Instance Method Details

#_extended_options?(value) ⇒ Boolean



28
29
30
# File 'lib/breezy_template/partial_extension.rb', line 28

def _extended_options?(value)
  _partial_options?(value) || super
end

#_normalize_options_for_partial(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/breezy_template/partial_extension.rb', line 36

def _normalize_options_for_partial(options)
  if !_partial_options?(options)
    return options
  end

  partial_options = [*options[:partial]]
  partial, rest = partial_options
  if partial && !rest
    options.dup.merge(partial: [partial, rest || {}])
  else
    options
  end
end

#_partial_digest(partial) ⇒ Object



50
51
52
53
54
# File 'lib/breezy_template/partial_extension.rb', line 50

def _partial_digest(partial)
  lookup_context = @context.lookup_context
  name = lookup_context.find(partial, lookup_context.prefixes, true).virtual_path
  _partial_digestor({name: name, finder: lookup_context})
end

#_partial_options?(options) ⇒ Boolean



32
33
34
# File 'lib/breezy_template/partial_extension.rb', line 32

def _partial_options?(options)
  ::Hash === options && options.key?(:partial)
end

#_render_partial(options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/breezy_template/partial_extension.rb', line 80

def _render_partial(options)
  partial, options = options[:partial]
  joint = options[:joint]
  if joint
    joint = joint.to_sym
    path = @path.dup.join('.')
    @js.push "joints['#{joint}'] ||= []; joints['#{joint}'].push('#{path}');"
    @joints[joint]
  end

  options[:locals].merge! json: self
  @context.render options.merge(partial: partial)
end

#_render_partial_with_options(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/breezy_template/partial_extension.rb', line 94

def _render_partial_with_options(options)
  options = _normalize_options_for_partial(options)
  partial, partial_opts = options[:partial]
  ary_opts = options.dup

  partial_opts.reverse_merge! locals: {}
  partial_opts.reverse_merge! ::BreezyTemplate.template_lookup_options
  as = partial_opts[:as]

  if partial_opts.key?(:collection)
    collection = partial_opts.delete(:collection)
    extract_joint_name = partial_opts.delete(:joint)
    locals = partial_opts.delete(:locals)

    ary_opts.delete(:partial)
    array! collection, ary_opts do |member|

      member_locals = locals.clone
      member_locals.merge! collection: collection
      member_locals.merge! as.to_sym => member if as
      partial_opts.merge!(locals: member_locals)
      if extract_joint_name.respond_to?(:call)
        partial_opts.merge!(joint: extract_joint_name.call(member))
      end
      _render_partial options
    end
  else
    _render_partial options
  end
end

#_set_inline_partial(name, object, options) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/breezy_template/partial_extension.rb', line 56

def _set_inline_partial(name, object, options)
  options = _normalize_options_for_partial(options)

  partial, partial_opts = options[:partial]
  if partial_opts[:joint] == true
    partial_opts[:joint] = name
  end

  value = if object.nil? && partial.empty?
    []
  else
    locals = {}
    locals[partial_opts[:as]] = object if !_blank?(partial_opts) && partial_opts.key?(:as)
    locals.merge!(partial_opts[:locals]) if partial_opts.key? :locals
    partial_opts.merge!(locals: locals)

    _scope{ _render_partial(options) }
  end

  options = options.dup
  options.delete(:partial)
  set! name, value, options
end

#array!(collection = [], *attributes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/breezy_template/partial_extension.rb', line 15

def array!(collection = [], *attributes)
  options = attributes.last || {}
  options = _normalize_options_for_partial(options)

  if attributes.one? && _partial_options?(options)
    _, opts = options[:partial]
    opts.reverse_merge!(collection: collection)
    _render_partial_with_options(options)
  else
    super
  end
end

#set!(key, value = BLANK, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/breezy_template/partial_extension.rb', line 5

def set!(key, value = BLANK, *args)
  options = args.last || {}

  if args.one? && _partial_options?(options)
    _set_inline_partial key, value, options
  else
    super
  end
end