Skip to content

Commit

Permalink
Merge branch 'main' into rendering_block
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Drapper <[email protected]>
  • Loading branch information
joeldrapper authored Nov 26, 2024
2 parents 1a2c901 + cc3703e commit 70119dd
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
34 changes: 20 additions & 14 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,36 @@ def render_in(...)
end

module Overrides
class HelpersCalledBeforeRenderError < StandardError; end

def helpers
if defined?(ViewComponent::Base) && ViewComponent::Base === @_view_context
@_view_context.helpers
unless @_context && (view_context = @_context.view_context)
raise HelpersCalledBeforeRenderError.new("Do not use rails helpers until after the view has been rendered.") unless view_context
end

if defined?(ViewComponent::Base) && ViewComponent::Base === view_context
view_context.helpers
else
@_view_context
view_context
end
end

def render(*args, **kwargs, &block)
renderable = args[0]

case renderable
when Phlex::SGML, Proc, Method, String
return super
when Class
return super if renderable < Phlex::SGML
when Enumerable
return super unless ActiveRecord::Relation === renderable
when nil
partial = kwargs.delete(:partial)
when Phlex::SGML, Proc, Method, String
return super
when Class
return super if renderable < Phlex::SGML
when Enumerable
return super unless ActiveRecord::Relation === renderable
when nil
partial = kwargs.delete(:partial)

if partial # this is a hack to get around https://github.com/rails/rails/issues/51015
return raw(
@_view_context.render(partial, **kwargs) do |*yielded_args|
@_context.view_context.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end,
)
Expand All @@ -43,15 +49,15 @@ def render(*args, **kwargs, &block)
end

output = if block
@_view_context.render(*args, **kwargs) do |*yielded_args|
@_context.view_context.render(*args, **kwargs) do |*yielded_args|
if yielded_args.length == 1 && defined?(ViewComponent::Base) && ViewComponent::Base === yielded_args[0]
capture(Phlex::Rails::Buffered.new(yielded_args[0], view: self), &block)
else
capture(*yielded_args, &block)
end
end
else
@_view_context.render(*args, **kwargs)
@_context.view_context.render(*args, **kwargs)
end

raw(output)
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/controllers/helpers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
class HelpersController < ApplicationController
layout false

def helper_in_initializer
render Helpers::HelperInInitializer.new
end

def form_with
render Helpers::FormWithView.new
end
Expand Down
9 changes: 9 additions & 0 deletions test/dummy/app/views/helpers/helper_in_initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class Helpers::HelperInInitializer < ApplicationView
include Phlex::Rails::Helpers::Routes

def initialize
helpers_helper_in_initializer_path
end
end
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
get "/layout/with_erb_view", to: "layout#with_erb_view"
get "/layout/with_phlex_view", to: "layout#with_phlex_view"

get "/helpers/helper_in_initializer", to: "helpers#helper_in_initializer"
get "/helpers/form_with", to: "helpers#form_with"
get "/helpers/tag", to: "helpers#tag"
get "/helpers/missing_helper", to: "helpers#missing_helper"
Expand Down
8 changes: 8 additions & 0 deletions test/phlex/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ class HelpersTest < ActionDispatch::IntegrationTest
end
end

test "helper in initializer" do
error = assert_raises(Phlex::Rails::SGML::Overrides::HelpersCalledBeforeRenderError) do
get "/helpers/helper_in_initializer"
end

assert_equal "Do not use rails helpers until after the view has been rendered.", error.message
end

test "form with" do
get "/helpers/form_with"

Expand Down

0 comments on commit 70119dd

Please sign in to comment.