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

Returns:

  • (Boolean)


26
27
28
# File 'lib/breezy_template/partial_extension.rb', line 26

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

#_normalize_options_for_partial(options) ⇒ Object



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

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



48
49
50
51
52
# File 'lib/breezy_template/partial_extension.rb', line 48

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

#_partial_options?(options) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/breezy_template/partial_extension.rb', line 30

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

#_render_partial(options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/breezy_template/partial_extension.rb', line 73

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

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

#_render_partial_with_collection(collection, options) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/breezy_template/partial_extension.rb', line 87

def _render_partial_with_collection(collection, options)
  options = _normalize_options_for_partial(options)
  partial, partial_opts = options[:partial]
  array_opts = options.dup

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

  extract_fragment_name = partial_opts.delete(:fragment_name)
  locals = partial_opts.delete(:locals)

  array_opts.delete(:partial)
  array! collection, array_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_fragment_name.respond_to?(:call)
      partial_opts.merge!(fragment_name: extract_fragment_name.call(member))
    end
    _render_partial options
  end
end

#_set_inline_partial(name, object, options) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/breezy_template/partial_extension.rb', line 54

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

  partial, partial_opts = options[:partial]

  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)

    _result(BLANK, options){ _render_partial(options) }
  end

  set! name, value
end

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



15
16
17
18
19
20
21
22
23
24
# 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)
    _render_partial_with_collection(collection, 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