Skip to content

Commit

Permalink
add controlelr implicit rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
reeganviljoen committed Aug 20, 2024
1 parent 57e8f09 commit 74b647a
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/render_kit/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

require "render_kit/view_paths_monkey_patch"
require "render_kit/rendering_helper_monkey_patch"
require "render_kit/implicit_render_monkey_patch"

module RenderKit
class Engine < ::Rails::Engine # :nodoc:
Expand All @@ -14,5 +15,12 @@ class Engine < ::Rails::Engine # :nodoc:
ActionView::Base.prepend RenderKit::RenderingHelperMonkeyPatch
end
end

initializer "render_kit.action_controller" do |app|
ActiveSupport.on_load(:action_controller) do
ActionController::Base.prepend RenderKit::RenderingHelperMonkeyPatch
ActionController::Base.prepend RenderKit::ImplicitRenderMonkeyPatch
end
end
end
end
19 changes: 19 additions & 0 deletions lib/render_kit/implicit_render_monkey_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen string literal: true

require "active_support/concern"

module RenderKit
module ImplicitRenderMonkeyPatch
def default_render
if renderable = find_renderable(action_name.to_s, _prefixes)
render(renderable.new)
else
super
end
end

def find_renderable(action_name, prefixes)
RenderKit::RenderableRegistry.get_renderables([prefixes, action_name].join("_"), controller=true)
end
end
end
18 changes: 13 additions & 5 deletions lib/render_kit/renderable_registry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

module RenderKit
module RenderableRegistry # :nodoc:
@renderables = Hash.new {}
@renderables = Hash.new { |h, k| h[k] = {} }

def self.get_renderables(path)
@renderables[path]
def self.get_renderables(path, controller=false)
if controller
renderable = @renderables[path]
renderable[:renderable_klass] if renderable&.[](:controller_render)
else
@renderables[path][:renderable_klass]
end
end

def self.set_renderable(path, renderable_klass)
@renderables[path] = renderable_klass
def self.set_renderable(path, renderable_klass, controller_render=false)
@renderables[path] = {
renderable_klass: renderable_klass,
controller_render: controller_render
}
end
end
end
4 changes: 2 additions & 2 deletions lib/render_kit/view_paths_monkey_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module ViewPathsMonkeyPatch
extend ActiveSupport::Concern

module ClassMethods
def register_renderable(path, renderable_klass)
RenderKit::RenderableRegistry.set_renderable(path, renderable_klass)
def register_renderable(path, renderable_klass, controller_render=false)
RenderKit::RenderableRegistry.set_renderable(path, renderable_klass, controller_render)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/sandbox/app/components/implicit_render_component.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

class ImplicitRenderComponent < ViewComponent::Base
ActionView::Base.register_renderable("implicit_render_component", ImplicitRenderComponent)
ActionView::Base.register_renderable("implicit_render", ImplicitRenderComponent)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div> I have been implicity rendered by a controller </div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class TestImplicitControllerRenderComponent < ViewComponent::Base
ActionView::Base.register_renderable("test_implicit_controller_render", TestImplicitControllerRenderComponent, controller_render=true)
end
3 changes: 3 additions & 0 deletions test/sandbox/app/controllers/test_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ class TestController < ActionController::Base

def component_implicit_render
end

def implicit_controller_render
end
end
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<div>
<%= render "implicit_render_component" %>
<%= render "implicit_render" %>
</div>
1 change: 1 addition & 0 deletions test/sandbox/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 "implicit_controller_render", to: "test#implicit_controller_render"
# Defines the root path route ("/")
# root "posts#index"
end
7 changes: 7 additions & 0 deletions test/sandbox/test/view_component_compatability_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@ def test_rendering_view_component
assert_response :success
assert_match(/I have been implicity rendered/, @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
end

0 comments on commit 74b647a

Please sign in to comment.