Module: TemplateStreaming::Controller

Defined in:
lib/template_streaming.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/template_streaming.rb', line 41

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.



100
101
102
103
104
# File 'lib/template_streaming.rb', line 100

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.



109
110
111
112
# File 'lib/template_streaming.rb', line 109

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.



88
89
90
91
92
93
94
# File 'lib/template_streaming.rb', line 88

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/template_streaming.rb', line 52

def render_with_template_streaming(*args, &block)
  with_template_streaming_condition(*args) do |condition|
    if condition
      @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
      form_authenticity_token if TemplateStreaming.set_authenticity_token
      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:



114
115
116
# File 'lib/template_streaming.rb', line 114

def template_streaming_flash # :nodoc:
  @template_streaming_flash
end