Module: TemplateStreaming::Controller

Defined in:
lib/template_streaming.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/template_streaming.rb', line 15

def self.included(base)
  base.class_eval do
    alias_method_chain :render, :template_streaming
    alias_method_chain :render_to_string, :template_streaming
    helper_method :flush, :push

    include ActiveSupport::Callbacks
    define_callbacks :when_streaming_template
  end
end

Instance Method Details

#flushObject

Flush the current template’s output buffer out to the client immediately.



74
75
76
77
78
# File 'lib/template_streaming.rb', line 74

def flush
  unless @template.output_buffer.nil?
    push @template.output_buffer.slice!(0..-1)
  end
end

#push(data) ⇒ Object

Push the given data to the client immediately.



83
84
85
86
# File 'lib/template_streaming.rb', line 83

def push(data)
  @streaming_body.push(data)
  flush_thin
end

#render_to_string_with_template_streaming(*args, &block) ⇒ Object

Override to ensure calling render_to_string from a helper doesn’t trigger template streaming.



62
63
64
65
66
67
68
# File 'lib/template_streaming.rb', line 62

def render_to_string_with_template_streaming(*args, &block) # :nodoc
  # Ensure renders within a render_to_string aren't considered
  # top-level.
  with_template_streaming_condition do
    render_to_string_without_template_streaming(*args, &block)
  end
end

#render_with_template_streaming(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/template_streaming.rb', line 26

def render_with_template_streaming(*args, &block)
  with_template_streaming_condition(*args) do |condition|
    if condition
      check_thin_support
      @performed_render = true
      @streaming_body = StreamingBody.new(progressive_rendering_threshold) do
        @performed_render = false
        last_piece = render_without_template_streaming(*args, &block)
        # The original render will clobber our response.body, so
        # we must push the buffer ourselves.
        push last_piece
      end
      response.body = @streaming_body
      response.prepare!
      flash if TemplateStreaming.autosweep_flash
      run_callbacks :when_streaming_template

      # Normally, @_flash is removed after #perform_action, which
      # means calling #flash in the view would cause a new
      # FlashHash to be constructed. On top of that, the flash is
      # swept on construction, which results in sweeping the flash
      # twice, obliterating its contents.
      #
      # So, we preserve the flash here under a different ivar, and
      # override the #flash helper to return it.
      if defined?(@_flash)
        @template_streaming_flash = @_flash
      end
    else
      render_without_template_streaming(*args, &block)
    end
  end
end

#template_streaming_flashObject

:nodoc:



88
89
90
# File 'lib/template_streaming.rb', line 88

def template_streaming_flash # :nodoc:
  @template_streaming_flash
end