Module: Slotify::Extensions::PartialRenderer

Defined in:
lib/slotify/extensions/partial_renderer.rb

Instance Method Summary collapse

Instance Method Details

#decorate_strict_slots_errors(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/slotify/extensions/partial_renderer.rb', line 17

def decorate_strict_slots_errors(&block)
  yield
rescue ActionView::Template::Error => error
  if missing_strict_locals_error?(error)
    local_names = error.cause.message.scan(/\s:(\w+)/).flatten
    if local_names.any?
      slot_names = local_names.intersection(error.template.strict_slots_keys).map { ":#{_1}" }
      if slot_names.any?
        message = "missing #{"slot".pluralize(slot_names.size)}: #{slot_names.join(", ")} for #{error.template.short_identifier}"
        raise Slotify::StrictSlotsError, message
      end
    end
  end
  raise error
end

#missing_strict_locals_error?(error) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/slotify/extensions/partial_renderer.rb', line 33

def missing_strict_locals_error?(error)
  error.template && (defined?(ActionView::StrictLocalsError) && error.cause.is_a?(ActionView::StrictLocalsError)) ||
    (error.cause.is_a?(ArgumentError) && error.cause.message.match(/missing local/))
end

#render_partial_template(view, locals, template, layout, block) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/slotify/extensions/partial_renderer.rb', line 4

def render_partial_template(view, locals, template, layout, block)
  return super unless template.strict_slots?

  view.partial.define_slots!(template.strict_slots_keys)

  view.capture_with_outer_partial_access(&block) if block
  locals = locals.merge(view.partial.slot_locals)

  decorate_strict_slots_errors do
    super(view, locals, template, layout, block)
  end
end