lib/action_view/component/base.rb in actionview-component-1.1.0 vs lib/action_view/component/base.rb in actionview-component-1.2.0

- old
+ new

@@ -1,21 +1,31 @@ # frozen_string_literal: true # Monkey patch ActionView::Base#render to support ActionView::Component # -# Upstreamed in https://github.com/rails/rails/pull/36388 -# Necessary for Rails versions < 6.1.0.alpha +# A version of this monkey patch was upstreamed in https://github.com/rails/rails/pull/36388 +# We'll need to upstream an updated version of this eventually. class ActionView::Base module RenderMonkeyPatch - def render(component, _ = nil, &block) - return super unless component.respond_to?(:render_in) + def render(options = {}, args = {}, &block) + if options.respond_to?(:render_in) + ActiveSupport::Deprecation.warn( + "passing component instances to `render` has been deprecated and will be removed in v2.0.0. Use `render MyComponent, foo: :bar` instead." + ) - component.render_in(self, &block) + options.render_in(self, &block) + elsif options.is_a?(Class) && options < ActionView::Component::Base + options.new(args).render_in(self, &block) + elsif options.is_a?(Hash) && options.has_key?(:component) + options[:component].new(options[:locals]).render_in(self, &block) + else + super + end end end - prepend RenderMonkeyPatch unless Rails::VERSION::MINOR > 0 && Rails::VERSION::MAJOR == 6 + prepend RenderMonkeyPatch end module ActionView module Component class Base < ActionView::Base @@ -40,10 +50,10 @@ # # app/components/my_component.html.erb # <span title="<%= @title %>">Hello, <%= content %>!</span> # # In use: - # <%= render MyComponent.new(title: "greeting") do %>world<% end %> + # <%= render MyComponent, title: "greeting" do %>world<% end %> # returns: # <span title="greeting">Hello, world!</span> # def render_in(view_context, *args, &block) self.class.compile