lib/action_view/component/base.rb in actionview-component-1.11.1 vs lib/action_view/component/base.rb in actionview-component-1.12.0

- old
+ new

@@ -12,14 +12,13 @@ delegate :form_authenticity_token, :protect_against_forgery?, to: :helpers class_attribute :content_areas, default: [] self.content_areas = [] # default doesn't work until Rails 5.2 - # Entrypoint for rendering components. Called by ActionView::Base#render. + # Entrypoint for rendering components. # # view_context: ActionView context from calling view - # args(hash): params to be passed to component being rendered # block: optional block to be captured within the view context # # returns HTML that has been escaped by the respective template handler # # Example subclass: @@ -33,15 +32,15 @@ # # app/components/my_component.html.erb # <span title="<%= @title %>">Hello, <%= content %>!</span> # # In use: - # <%= render MyComponent, title: "greeting" do %>world<% end %> + # <%= render MyComponent.new(title: "greeting") do %>world<% end %> # returns: # <span title="greeting">Hello, world!</span> # - def render_in(view_context, *args, &block) + def render_in(view_context, &block) self.class.compile! @view_context = view_context @view_renderer ||= view_context.view_renderer @lookup_context ||= view_context.lookup_context @view_flow ||= view_context.view_flow @@ -51,19 +50,25 @@ old_current_template = @current_template @current_template = self @content = view_context.capture(self, &block) if block_given? - validate! + before_render_check - return "" unless render? - - send(self.class.call_method_name(@variant)) + if render? + send(self.class.call_method_name(@variant)) + else + "" + end ensure @current_template = old_current_template end + def before_render_check + validate! + end + def render? true end def initialize(*); end @@ -140,18 +145,16 @@ end end def source_location @source_location ||= - if const_source_location_supported? - const_source_location(self.name)[0] - else - # Require `#initialize` to be defined so that we can use `method#source_location` - # to look up the filename of the component. - initialize_method = instance_method(:initialize) - initialize_method.source_location[0] if initialize_method.owner == self - end + begin + # Require `#initialize` to be defined so that we can use `method#source_location` + # to look up the filename of the component. + initialize_method = instance_method(:initialize) + initialize_method.source_location[0] if initialize_method.owner == self + end end def compiled? @compiled && ActionView::Base.cache_template_loading end @@ -204,14 +207,10 @@ self.content_areas = areas end private - def const_source_location_supported? - respond_to? :const_source_location # introduced in Ruby 2.7 - end - def matching_views_in_source_location return [] unless source_location (Dir["#{source_location.chomp(File.extname(source_location))}.*{#{ActionView::Template.template_handler_extensions.join(',')}}"] - [source_location]) end @@ -230,10 +229,10 @@ def template_errors @template_errors ||= begin errors = [] - if source_location.nil? && !const_source_location_supported? + if source_location.nil? # Require `#initialize` to be defined so that we can use `method#source_location` # to look up the filename of the component. errors << "#{self} must implement #initialize." end