lib/action_view/component/base.rb in actionview-component-1.4.0 vs lib/action_view/component/base.rb in actionview-component-1.5.0
- old
+ new
@@ -1,40 +1,21 @@
# frozen_string_literal: true
-# Monkey patch ActionView::Base#render to support ActionView::Component
-#
-# 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(options = {}, args = {}, &block)
- if options.respond_to?(:render_in)
- ActiveSupport::Deprecation.warn(
- "passing component instances (`render MyComponent.new(foo: :bar)`) has been deprecated and will be removed in v2.0.0. Use `render MyComponent, foo: :bar` instead."
- )
+require "active_model"
+require "action_view"
+require "active_support/configurable"
+require_relative "preview"
- 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
-end
-
module ActionView
module Component
class Base < ActionView::Base
include ActiveModel::Validations
include ActiveSupport::Configurable
- include ActionController::RequestForgeryProtection
+ include ActionView::Component::Previews
+ delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers
+
validate :variant_exists
# Entrypoint for rendering components. Called by ActionView::Base#render.
#
# view_context: ActionView context from calling view
@@ -91,14 +72,18 @@
def controller
@controller ||= view_context.controller
end
- # Looks for the source file path of the initialize method of the instance's class.
+ # Provides a proxy to access helper methods through
+ def helpers
+ @helpers ||= view_context
+ end
+
# Removes the first part of the path and the extension.
def virtual_path
- self.class.source_location.gsub(%r{(.*app/)|(\.rb)}, "")
+ self.class.source_location.gsub(%r{(.*app/components)|(\.rb)}, "")
end
def view_cache_dependencies
[]
end
@@ -171,10 +156,19 @@
def variants
templates.map { |template| template[:variant] }
end
+ # we'll eventually want to update this to support other types
+ def type
+ "text/html"
+ end
+
+ def identifier
+ ""
+ end
+
private
def templates
@templates ||=
(Dir["#{source_location.sub(/#{File.extname(source_location)}$/, '')}.*{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [source_location]).each_with_object([]) do |path, memo|
@@ -204,33 +198,17 @@
def compiled_template(file_path)
handler = ActionView::Template.handler_for_extension(File.extname(file_path).gsub(".", ""))
template = File.read(file_path)
- # This can be removed once this code is merged into Rails
if handler.method(:call).parameters.length > 1
- handler.call(DummyTemplate.new, template)
- else
- handler.call(DummyTemplate.new(template))
+ handler.call(self, template)
+ else # remove before upstreaming into Rails
+ handler.call(OpenStruct.new(source: template, identifier: identifier, type: type))
end
end
end
- class DummyTemplate
- attr_reader :source
-
- def initialize(source = nil)
- @source = source
- end
-
- def identifier
- ""
- end
-
- # we'll eventually want to update this to support other types
- def type
- "text/html"
- end
- end
+ ActiveSupport.run_load_hooks(:action_view_component, self)
end
end
end