Skip to content

Commit

Permalink
Double render (phlex-ruby#734)
Browse files Browse the repository at this point in the history
should resolve phlex-ruby#607
(last pr had some formatting issues)
  • Loading branch information
Vagab authored Jun 14, 2024
1 parent e4aecac commit fe4979d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/phlex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

module Phlex
autoload :ArgumentError, "phlex/errors/argument_error"
autoload :DoubleRenderError, "phlex/errors/double_render_error"
autoload :BlackHole, "phlex/black_hole"
autoload :CSV, "phlex/csv"
autoload :Callable, "phlex/callable"
Expand Down
5 changes: 5 additions & 0 deletions lib/phlex/errors/double_render_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Phlex::DoubleRenderError < RuntimeError
include Phlex::Error
end
3 changes: 3 additions & 0 deletions lib/phlex/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ def call(buffer = +"", context: Phlex::Context.new, view_context: nil, parent: n
@_view_context = view_context
@_parent = parent

raise Phlex::DoubleRenderError, "You can't render a #{self.class.name} more than once." if @_rendered
@_rendered = true

if fragments
@_context.target_fragments(fragments)
end
Expand Down
17 changes: 17 additions & 0 deletions test/phlex/view/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ def some_method
expect(output).to be == "<h1>Hi</h1>"
end
end

with "a view that is rendered twice" do
view do
def view_template
plain "Hi"
end
end

let(:output) do
example.call
example.call
end

it "raises an error" do
expect { output }.to raise_exception(Phlex::DoubleRenderError)
end
end
end

0 comments on commit fe4979d

Please sign in to comment.