Skip to content

Commit

Permalink
Improve compatibility with upcoming Rails 7.1
Browse files Browse the repository at this point in the history
In rails/rails#45614 I modified
`ActionView::OutputBuffer` to no longer be a subclass of
`ActiveSupport::SafeBuffer` and as such it's not recommended
to use `SafeBuffer` for buffering views anymore.

Additionally in rails/rails#45731
I start doing some changes so that we stop mutating `@output_buffer`.
So template should no longer assign `@output_buffer` themselves.
  • Loading branch information
byroot authored and judofyr committed Aug 7, 2022
1 parent f9099a0 commit de64682
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
8 changes: 2 additions & 6 deletions lib/temple/generators/rails_output_buffer.rb
Expand Up @@ -10,7 +10,7 @@ module Generators
# @api public
class RailsOutputBuffer < StringBuffer
define_options :streaming,
buffer_class: 'ActiveSupport::SafeBuffer',
buffer_class: 'ActionView::OutputBuffer',
buffer: '@output_buffer',
# output_buffer is needed for Rails 3.1 Streaming support
capture_generator: RailsOutputBuffer
Expand All @@ -20,11 +20,7 @@ def call(exp)
end

def create_buffer
if options[:streaming] && options[:buffer] == '@output_buffer'
"#{buffer} = output_buffer || #{options[:buffer_class]}.new"
else
"#{buffer} = #{options[:buffer_class]}.new"
end
"#{buffer} = output_buffer || #{options[:buffer_class]}.new"
end

def concat(str)
Expand Down
8 changes: 4 additions & 4 deletions test/test_generator.rb
Expand Up @@ -143,16 +143,16 @@ def on_code(s)
describe Temple::Generators::RailsOutputBuffer do
it 'should compile simple expressions' do
gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: false)
gen.call([:static, 'test']).should.equal '@output_buffer = ActiveSupport::SafeBuffer.new; ' +
gen.call([:static, 'test']).should.equal '@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
'@output_buffer.safe_concat(("test")); @output_buffer'
gen.call([:dynamic, 'test']).should.equal '@output_buffer = ActiveSupport::SafeBuffer.new; ' +
gen.call([:dynamic, 'test']).should.equal '@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
'@output_buffer.safe_concat(((test).to_s)); @output_buffer'
gen.call([:code, 'test']).should.equal '@output_buffer = ActiveSupport::SafeBuffer.new; ' +
gen.call([:code, 'test']).should.equal '@output_buffer = output_buffer || ActionView::OutputBuffer.new; ' +
'test; @output_buffer'
end

it 'should freeze static' do
gen = Temple::Generators::RailsOutputBuffer.new(freeze_static: true)
gen.call([:static, 'test']).should.equal '@output_buffer = ActiveSupport::SafeBuffer.new; @output_buffer.safe_concat(("test".freeze)); @output_buffer'
gen.call([:static, 'test']).should.equal '@output_buffer = output_buffer || ActionView::OutputBuffer.new; @output_buffer.safe_concat(("test".freeze)); @output_buffer'
end
end

0 comments on commit de64682

Please sign in to comment.