Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise if helpers called from #initialize #228

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 use rails helpers until after the view has been rendered.") unless @_view_context
Copy link
Contributor Author

@Vagab Vagab Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively I think we could move this to helper_macros.rb. But I think it's better to leave it here. Wdyt?


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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran rubocop -A here. Let me know if I should roll this change back

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 use rails helpers until after the view has been rendered.", error.message
end

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

Expand Down