From 9579e5bf273f7311937758004f67958bc62afaea Mon Sep 17 00:00:00 2001 From: Reegan Viljoen Date: Thu, 22 Aug 2024 22:09:45 +0200 Subject: [PATCH 1/2] add: suport for params --- lib/render_kit/implicit_render_monkey_patch.rb | 8 +++++++- lib/render_kit/rendering_helper_monkey_patch.rb | 4 ++-- .../implicit_args_render_component.html.erb | 1 + .../components/implicit_args_render_component.rb | 9 +++++++++ ...implicit_controller_render_component.html.erb | 6 +++++- .../test_implicit_controller_render_component.rb | 4 ++++ test/sandbox/app/controllers/test_controller.rb | 3 +++ .../test/component_args_implicit_render.html.erb | 3 +++ test/sandbox/config/routes.rb | 1 + .../test/view_component_compatability_test.rb | 16 ++++++++++++++++ 10 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 test/sandbox/app/components/implicit_args_render_component.html.erb create mode 100644 test/sandbox/app/components/implicit_args_render_component.rb create mode 100644 test/sandbox/app/views/test/component_args_implicit_render.html.erb diff --git a/lib/render_kit/implicit_render_monkey_patch.rb b/lib/render_kit/implicit_render_monkey_patch.rb index a06a68e..7d53e34 100644 --- a/lib/render_kit/implicit_render_monkey_patch.rb +++ b/lib/render_kit/implicit_render_monkey_patch.rb @@ -6,7 +6,7 @@ module RenderKit module ImplicitRenderMonkeyPatch def default_render if renderable = find_renderable(action_name.to_s, _prefixes) - render(renderable.new) + render(renderable.new(**renderable_params)) else super end @@ -15,5 +15,11 @@ def default_render def find_renderable(action_name, prefixes) RenderKit::RenderableRegistry.get_renderables([prefixes, action_name].join("_"), controller=true) end + + # unsafe_h is used to convert the params to a hash, + # this is fine because the validtion is done in the renderable initializer + def renderable_params + params.to_unsafe_h.symbolize_keys.except(:controller, :action) + end end end diff --git a/lib/render_kit/rendering_helper_monkey_patch.rb b/lib/render_kit/rendering_helper_monkey_patch.rb index 9b718e5..53e2728 100644 --- a/lib/render_kit/rendering_helper_monkey_patch.rb +++ b/lib/render_kit/rendering_helper_monkey_patch.rb @@ -4,9 +4,9 @@ module RenderKit module RenderingHelperMonkeyPatch - def render(options = {}, locals = {}, &) + def render(options = {}, locals = {}, &block) if renderable = RenderKit::RenderableRegistry.get_renderables(options) - renderable.new(locals).render_in(self, &) + renderable.new(**locals).render_in(self, &block) else super end diff --git a/test/sandbox/app/components/implicit_args_render_component.html.erb b/test/sandbox/app/components/implicit_args_render_component.html.erb new file mode 100644 index 0000000..8c0abab --- /dev/null +++ b/test/sandbox/app/components/implicit_args_render_component.html.erb @@ -0,0 +1 @@ +
<%= @title %>
diff --git a/test/sandbox/app/components/implicit_args_render_component.rb b/test/sandbox/app/components/implicit_args_render_component.rb new file mode 100644 index 0000000..fc1938a --- /dev/null +++ b/test/sandbox/app/components/implicit_args_render_component.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class ImplicitArgsRenderComponent < ViewComponent::Base + ActionView::Base.register_renderable("implicit_args_render", ImplicitArgsRenderComponent) + + def initialize(title:) + @title = title + end +end diff --git a/test/sandbox/app/components/test_implicit_controller_render_component.html.erb b/test/sandbox/app/components/test_implicit_controller_render_component.html.erb index acfce2a..cd16df4 100644 --- a/test/sandbox/app/components/test_implicit_controller_render_component.html.erb +++ b/test/sandbox/app/components/test_implicit_controller_render_component.html.erb @@ -1 +1,5 @@ -
I have been implicity rendered by a controller
+<% if @title.present? %> +
<%= @title %>
+<% else %> +
I have been implicity rendered by a controller
+<% end %> diff --git a/test/sandbox/app/components/test_implicit_controller_render_component.rb b/test/sandbox/app/components/test_implicit_controller_render_component.rb index 3f46156..2384a8a 100644 --- a/test/sandbox/app/components/test_implicit_controller_render_component.rb +++ b/test/sandbox/app/components/test_implicit_controller_render_component.rb @@ -2,4 +2,8 @@ class TestImplicitControllerRenderComponent < ViewComponent::Base ActionView::Base.register_renderable("test_implicit_controller_render", TestImplicitControllerRenderComponent, controller_render=true) + + def initialize(title: nil) + @title = title + end end diff --git a/test/sandbox/app/controllers/test_controller.rb b/test/sandbox/app/controllers/test_controller.rb index 0cec83e..209c234 100644 --- a/test/sandbox/app/controllers/test_controller.rb +++ b/test/sandbox/app/controllers/test_controller.rb @@ -4,6 +4,9 @@ class TestController < ActionController::Base def component_implicit_render end + def component_args_implicit_render + end + def implicit_controller_render end end diff --git a/test/sandbox/app/views/test/component_args_implicit_render.html.erb b/test/sandbox/app/views/test/component_args_implicit_render.html.erb new file mode 100644 index 0000000..b983f1c --- /dev/null +++ b/test/sandbox/app/views/test/component_args_implicit_render.html.erb @@ -0,0 +1,3 @@ +
+ <%= render "implicit_args_render", title: 'I am using args' %> +
diff --git a/test/sandbox/config/routes.rb b/test/sandbox/config/routes.rb index 26c609b..6bb0eaa 100644 --- a/test/sandbox/config/routes.rb +++ b/test/sandbox/config/routes.rb @@ -9,6 +9,7 @@ get "service-worker" => "rails/pwa#service_worker", :as => :pwa_service_worker get "manifest" => "rails/pwa#manifest", :as => :pwa_manifest get "component_implicit_render", to: "test#component_implicit_render" + get "component_args_implicit_render", to: "test#component_args_implicit_render" get "implicit_controller_render", to: "test#implicit_controller_render" # Defines the root path route ("/") # root "posts#index" diff --git a/test/sandbox/test/view_component_compatability_test.rb b/test/sandbox/test/view_component_compatability_test.rb index 2e5a5f1..c89ac28 100644 --- a/test/sandbox/test/view_component_compatability_test.rb +++ b/test/sandbox/test/view_component_compatability_test.rb @@ -31,10 +31,26 @@ def test_rendering_view_component assert_match(/I have been implicity rendered/, @response.body) end + def test_rendering_view_component_args + get :component_args_implicit_render + assert_response 200 + assert_response :success + assert_match(/I am using args/, @response.body) + end + def test_rendering_view_component_with_controller_render get :implicit_controller_render assert_response 200 assert_response :success assert_match(/I have been implicity rendered by a controller/, @response.body) end + + def test_rendering_view_component_with_controller_render + get :implicit_controller_render, params: { title: "I have rendered controller params" } + assert_response 200 + assert_response :success + assert_match(/I have rendered controller params/, @response.body) + end + + end From 67ec6fd58ae87c02ede2e07167b0df0584c899ab Mon Sep 17 00:00:00 2001 From: Reegan Viljoen Date: Thu, 22 Aug 2024 22:10:20 +0200 Subject: [PATCH 2/2] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 957623a..45bcfa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## [Unreleased] - Update Documentation +- Add support for params in controller and locals in view ## [0.1.0] - 2024-08-20