Module: NicePartials::RenderingWithAutoContext

Defined in:
lib/nice_partials/monkey_patch.rb

Instance Method Summary collapse

Instance Method Details

#__partialsObject



48
49
50
# File 'lib/nice_partials/monkey_patch.rb', line 48

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.



78
79
80
81
82
83
84
# File 'lib/nice_partials/monkey_patch.rb', line 78

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.



96
97
98
99
100
101
# File 'lib/nice_partials/monkey_patch.rb', line 96

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

#p(*args) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/nice_partials/monkey_patch.rb', line 53

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

#render(options = {}, locals = {}, &block) ⇒ Object

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



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

def render(options = {}, locals = {}, &block)
  partial_locals = options.is_a?(Hash) ? options[:locals] : locals
  __partials.prepend nice_partial_with(partial_locals)
  super
ensure
  __partials.shift
end