Module: TemplateStreaming::Controller
- Defined in:
- lib/template_streaming.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #action_streams ⇒ Object
- #action_streams? ⇒ Boolean
-
#flush ⇒ Object
Flush the current template’s output buffer out to the client immediately.
-
#pick_layout(options) ⇒ Object
Mark the case when it’s a layout for a toplevel render.
-
#push(data) ⇒ Object
Push the given data to the client immediately.
-
#render_to_string_with_template_streaming(*args, &block) ⇒ Object
Override to ensure calling render_to_string from a helper doesn’t trigger template streaming.
- #render_with_template_streaming(*args, &block) ⇒ Object
- #streaming_template? ⇒ Boolean
-
#template_streaming_flash ⇒ Object
:nodoc:.
Class Method Details
.included(base) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/template_streaming.rb', line 13 def self.included(base) base.class_eval do extend ClassMethods alias_method_chain :render, :template_streaming alias_method_chain :render_to_string, :template_streaming alias_method_chain :flash, :template_streaming helper_method :flush, :push include ActiveSupport::Callbacks define_callbacks :when_streaming_template end end |
Instance Method Details
#action_streams ⇒ Object
32 33 34 |
# File 'lib/template_streaming.rb', line 32 def action_streams @action_streams = true end |
#action_streams? ⇒ Boolean
36 37 38 |
# File 'lib/template_streaming.rb', line 36 def action_streams? @action_streams end |
#flush ⇒ Object
Flush the current template’s output buffer out to the client immediately.
113 114 115 116 117 |
# File 'lib/template_streaming.rb', line 113 def flush if @streaming_body && !@template.output_buffer.nil? push @template.output_buffer.slice!(0..-1) end end |
#pick_layout(options) ⇒ Object
Mark the case when it’s a layout for a toplevel render. This is done here, as it’s called after the option wrangling in AC::Base#render, and nowhere else.
95 96 97 98 99 |
# File 'lib/template_streaming.rb', line 95 def pick_layout() result = super [:toplevel_render_with_layout] = true if result result end |
#push(data) ⇒ Object
Push the given data to the client immediately.
122 123 124 125 126 127 |
# File 'lib/template_streaming.rb', line 122 def push(data) if @streaming_body @streaming_body.push(data) flush_thin end 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.
103 104 105 106 107 |
# File 'lib/template_streaming.rb', line 103 def render_to_string_with_template_streaming(*args, &block) # :nodoc push_render_stack_frame do render_to_string_without_template_streaming(*args, &block) end end |
#render_with_template_streaming(*args, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 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 85 86 87 88 89 90 |
# File 'lib/template_streaming.rb', line 40 def render_with_template_streaming(*args, &block) = args.first { |a| a.is_a?(Hash) } if && .size == 1 && .key?(:stream) # Need to set the default values, since the standard #render won't. [:template] = default_template [:layout] = true end push_render_stack_frame do |stack_height| if start_streaming_template?(stack_height, *args) @streaming_template = true @template.streaming_template = true @performed_render = true @streaming_body = StreamingBody.new(template_streaming_threshold) do .freeze if self.class.session_store.sent_with_headers? session.freeze flash.freeze end @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! form_authenticity_token # generate now # Normally, the flash is swept on first reference. This # means we need to ensure it's referenced before the session # is persisted. In the case of the cookie store, that's when # the headers are sent, so we force a reference now. # # But alas, that's not all. @_flash is removed after # #perform_action, which means calling #flash in the view # would cause the flash to be referenced again, sweeping the # flash a second time. To prevent this, we preserve the # flash in a separate ivar, and patch #flash to return this # if we're streaming. # flash # ensure sweep @template_streaming_flash = @_flash request.env[STREAMING_KEY] = true run_callbacks :when_streaming_template else render_without_template_streaming(*args, &block) end end end |
#streaming_template? ⇒ Boolean
133 134 135 |
# File 'lib/template_streaming.rb', line 133 def streaming_template? @streaming_template end |
#template_streaming_flash ⇒ Object
:nodoc:
129 130 131 |
# File 'lib/template_streaming.rb', line 129 def template_streaming_flash # :nodoc: @template_streaming_flash end |