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 error with tip when required ActionView context is missing #229

Open
wants to merge 1 commit 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
26 changes: 20 additions & 6 deletions lib/phlex/rails/sgml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ def render_in(...)

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

Expand All @@ -33,23 +33,23 @@ def render(*args, **kwargs, &block)

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|
view_context!.render(partial, **kwargs) do |*yielded_args|
capture(*yielded_args, &block)
end,
)
end
end

output = if block
@_view_context.render(*args, **kwargs) do |*yielded_args|
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)
view_context!.render(*args, **kwargs)
end

raw(output)
Expand Down Expand Up @@ -82,6 +82,20 @@ def capture(...)
# @api private
def set_original_view_context(view_context)
end

private

def view_context!
unless @_view_context
instance_name = "#{self.class.name.sub(/::[^:]+\z/, '')}_instance"
err_s = "👋 Please use `render` to render #{self.class.name} instances (instead of " \
"`#{instance_name}.call`). Using `render` allows the component to receive the " \
"ActionView context, which is required by Phlex helpers it includes."
raise StandardError.new(err_s)
end

@_view_context
end
end
end
end
Expand Down
9 changes: 9 additions & 0 deletions test/dummy/app/controllers/helpers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,13 @@ def notice_test # can't use 'notice' as the method name because it interferes wi
flash.now.notice = "My Flash Notice"
render Helpers::NoticeView.new
end

def view_context_required
case params[:render_style]
when "call"
Helpers::ViewContextRequired.new.call
when "render"
render Helpers::ViewContextRequired.new
end
end
end
10 changes: 10 additions & 0 deletions test/dummy/app/views/helpers/view_context_required.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

class Helpers::ViewContextRequired < ApplicationView
include ActionView::Helpers::UrlHelper

def view_template
# root_path will fail unless the view context has been set.
p { root_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 @@ -17,6 +17,7 @@
get "/helpers/tag", to: "helpers#tag"
get "/helpers/missing_helper", to: "helpers#missing_helper"
get "/helpers/notice", to: "helpers#notice_test"
get "/helpers/view_context_required", to: "helpers#view_context_required"

get "/rendering/partial_from_phlex", to: "rendering#partial_from_phlex"
get "/rendering/view_component_from_phlex", to: "rendering#view_component_from_phlex"
Expand Down
11 changes: 11 additions & 0 deletions test/phlex/helpers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@ class HelpersTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select "div > h1#hello.text-xl", "Hello World"
end

test "view_context required" do
get "/helpers/view_context_required?render_style=render"
assert_response :success
assert_select "p", "/"

exception = assert_raises(StandardError) do
get "/helpers/view_context_required?render_style=call"
end
assert_includes exception.message, "Please use `render`"
end
end