Module: NicePartials::RenderingWithAutoContext

Defined in:
lib/nice_partials/monkey_patch.rb

Instance Method Summary collapse

Instance Method Details

#__partialsObject



35
36
37
# File 'lib/nice_partials/monkey_patch.rb', line 35

def __partials
  @__partials ||= NicePartials::Partial::Stack.new
end

#_layout_for(*arguments, &block) ⇒ Object

Since Action View passes any ‘yield`s in partials through `_layout_for`, we override `_layout_for` to detects if it’s a capturing yield and append the current partial to the arguments.

So ‘render … do |some_object|` can become `render … do |some_object, partial|` without needing to find and update the inner `yield some_object` call.



64
65
66
67
68
69
70
# File 'lib/nice_partials/monkey_patch.rb', line 64

def _layout_for(*arguments, &block)
  if block && !arguments.first.is_a?(Symbol)
    capture_with_outer_partial_access(*arguments, &block)
  else
    super
  end
end

#capture_with_outer_partial_access(*arguments, &block) ⇒ Object

Reverts ‘partial` to return the outer partial before the `render` call started.

So we don’t clobber the ‘partial` shown here:

<%= render "card" do |inner_partial| %>
  <% inner_partial.content_for :title, partial.content_for(:title) %>
<% end %>

Note: this happens because the ‘@partial` instance variable is shared between all `render` calls since rendering happens in one `ActionView::Base` instance.



82
83
84
85
86
87
# File 'lib/nice_partials/monkey_patch.rb', line 82

def capture_with_outer_partial_access(*arguments, &block)
  __partials.locate_previous
  __partials.first.capture(*arguments, &block)
ensure
  __partials.reset_locator
end

#p(*args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/nice_partials/monkey_patch.rb', line 40

def p(*args)
  if args.empty?
    NicePartials::DEPRECATOR.deprecation_warning :p, :partial # In-branch printing so we don't warn on legit `Kernel.p` calls.
    partial
  else
    super # …we're really Kernel.p
  end
end

#renderObject

Overrides ‘ActionView::Helpers::RenderingHelper#render` to push a new `nice_partial` on the stack, so rendering has a fresh `partial` to store content in.



51
52
53
54
55
56
# File 'lib/nice_partials/monkey_patch.rb', line 51

def render(*)
  __partials.prepend nice_partial
  super
ensure
  __partials.shift
end