Skip to content

Commit

Permalink
raise if helpers called from #initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagab committed Sep 14, 2024
1 parent d524389 commit 197cbd3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 15 deletions.
34 changes: 19 additions & 15 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ def render_in(...)
end

module Overrides
class HelpersCalledBeforeRenderError < StandardError; end

def helpers
raise HelpersCalledBeforeRenderError.new("Do not call `helpers` until after the view has been rendered.") unless @_view_context

if defined?(ViewComponent::Base) && ViewComponent::Base === @_view_context
@_view_context.helpers
else
Expand All @@ -22,22 +26,22 @@ 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|
capture(*yielded_args, &block)
end,
)
end
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|
capture(*yielded_args, &block)
end,
)
end
end

output = if block
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 call `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 197cbd3

Please sign in to comment.