Module: ViewComponent::GlobalOutputBuffer

Defined in:
lib/view_component/global_output_buffer.rb

Defined Under Namespace

Modules: ActionViewMods

Instance Method Summary collapse

Instance Method Details

#output_buffer=(other_buffer) ⇒ Object



30
31
32
# File 'lib/view_component/global_output_buffer.rb', line 30

def output_buffer=(other_buffer)
  @output_buffer.replace(other_buffer)
end

#perform_renderObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/view_component/global_output_buffer.rb', line 17

def perform_render
  # HAML unhelpfully assigns to @output_buffer directly, so we hold onto a reference to
  # it and restore @output_buffer when the HAML engine is finished. In non-HAML cases,
  # @output_buffer and orig_buf will point to the same object, making the reassignment
  # statements no-ops.
  orig_buf = @output_buffer
  @output_buffer.push
  result = render_template_for(@__vc_variant).to_s + _output_postamble
  @output_buffer = orig_buf
  @output_buffer.pop
  result
end

#render_in(view_context, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/view_component/global_output_buffer.rb', line 5

def render_in(view_context, &block)
  unless view_context.output_buffer.is_a?(OutputBufferStack)
    # use instance_variable_set here to avoid triggering the code in the #output_buffer= method below
    view_context.instance_variable_set(:@output_buffer, OutputBufferStack.new(view_context.output_buffer))
  end

  @output_buffer = view_context.output_buffer
  @global_buffer_in_use = true

  super(view_context, &block)
end

#with_output_buffer(buf = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/view_component/global_output_buffer.rb', line 34

def with_output_buffer(buf = nil)
  unless buf
    buf = ActionView::OutputBuffer.new
    if output_buffer && output_buffer.respond_to?(:encoding)
      buf.force_encoding(output_buffer.encoding)
    end
  end

  output_buffer.push(buf)
  result = nil

  begin
    yield
  ensure
    # assign result here to avoid a return statement, which will
    # immediately return to the caller and swallow any errors
    result = output_buffer.pop
  end

  result
end