2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-10-10 05:03:18 -04:00
|
|
|
module ActionView
|
2013-04-18 14:39:04 -04:00
|
|
|
# This class defines the interface for a renderer. Each class that
|
|
|
|
# subclasses +AbstractRenderer+ is used by the base +Renderer+ class to
|
|
|
|
# render a specific type of object.
|
|
|
|
#
|
|
|
|
# The base +Renderer+ class uses its +render+ method to delegate to the
|
|
|
|
# renderers. These currently consist of
|
|
|
|
#
|
|
|
|
# PartialRenderer - Used for rendering partials
|
|
|
|
# TemplateRenderer - Used for rendering other types of templates
|
|
|
|
# StreamingTemplateRenderer - Used for streaming
|
|
|
|
#
|
|
|
|
# Whenever the +render+ method is called on the base +Renderer+ class, a new
|
|
|
|
# renderer object of the correct type is created, and the +render+ method on
|
|
|
|
# that new object is called in turn. This abstracts the setup and rendering
|
|
|
|
# into a separate classes for partials and templates.
|
2010-10-10 05:03:18 -04:00
|
|
|
class AbstractRenderer #:nodoc:
|
2019-01-28 18:09:13 -05:00
|
|
|
delegate :template_exists?, :any_templates?, :formats, to: :@lookup_context
|
2010-10-10 05:03:18 -04:00
|
|
|
|
2011-05-03 18:12:11 -04:00
|
|
|
def initialize(lookup_context)
|
2011-05-01 04:33:30 -04:00
|
|
|
@lookup_context = lookup_context
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def render
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2019-02-13 20:59:21 -05:00
|
|
|
class RenderedCollection # :nodoc:
|
2019-02-22 14:58:04 -05:00
|
|
|
def self.empty(format)
|
|
|
|
EmptyCollection.new format
|
|
|
|
end
|
|
|
|
|
2019-02-13 20:59:21 -05:00
|
|
|
attr_reader :rendered_templates
|
|
|
|
|
|
|
|
def initialize(rendered_templates, spacer)
|
|
|
|
@rendered_templates = rendered_templates
|
|
|
|
@spacer = spacer
|
|
|
|
end
|
|
|
|
|
|
|
|
def body
|
|
|
|
@rendered_templates.map(&:body).join(@spacer.body).html_safe
|
|
|
|
end
|
|
|
|
|
|
|
|
def format
|
|
|
|
rendered_templates.first.format
|
|
|
|
end
|
|
|
|
|
|
|
|
class EmptyCollection
|
2019-02-22 14:58:04 -05:00
|
|
|
attr_reader :format
|
|
|
|
|
|
|
|
def initialize(format)
|
|
|
|
@format = format
|
|
|
|
end
|
|
|
|
|
2019-02-13 20:59:21 -05:00
|
|
|
def body; nil; end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RenderedTemplate # :nodoc:
|
2019-02-20 08:20:24 -05:00
|
|
|
attr_reader :body, :template
|
2019-02-13 20:59:21 -05:00
|
|
|
|
2019-02-20 08:20:24 -05:00
|
|
|
def initialize(body, template)
|
2019-02-13 20:59:21 -05:00
|
|
|
@body = body
|
|
|
|
@template = template
|
|
|
|
end
|
|
|
|
|
|
|
|
def format
|
2019-02-25 16:18:44 -05:00
|
|
|
template.format
|
2019-02-13 20:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
EMPTY_SPACER = Struct.new(:body).new
|
|
|
|
end
|
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
private
|
|
|
|
def extract_details(options) # :doc:
|
2016-08-06 13:55:02 -04:00
|
|
|
@lookup_context.registered_details.each_with_object({}) do |key, details|
|
|
|
|
value = options[key]
|
2014-07-18 23:54:03 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
details[key] = Array(value) if value
|
|
|
|
end
|
2011-09-22 17:51:44 -04:00
|
|
|
end
|
2012-01-05 15:59:25 -05:00
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
def instrument(name, **options) # :doc:
|
2016-08-06 13:55:02 -04:00
|
|
|
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options) do |payload|
|
|
|
|
yield payload
|
|
|
|
end
|
2016-02-18 15:55:42 -05:00
|
|
|
end
|
2012-07-11 04:18:52 -04:00
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
def prepend_formats(formats) # :doc:
|
2016-08-06 13:55:02 -04:00
|
|
|
formats = Array(formats)
|
|
|
|
return if formats.empty? || @lookup_context.html_fallback_for_js
|
2014-07-18 23:54:03 -04:00
|
|
|
|
2016-08-06 13:55:02 -04:00
|
|
|
@lookup_context.formats = formats | @lookup_context.formats
|
|
|
|
end
|
2019-02-13 20:59:21 -05:00
|
|
|
|
2019-02-20 08:20:24 -05:00
|
|
|
def build_rendered_template(content, template)
|
|
|
|
RenderedTemplate.new content, template
|
2019-02-13 20:59:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def build_rendered_collection(templates, spacer)
|
2019-02-19 17:57:29 -05:00
|
|
|
RenderedCollection.new templates, spacer
|
2019-02-13 20:59:21 -05:00
|
|
|
end
|
2010-10-10 05:03:18 -04:00
|
|
|
end
|
2010-10-16 11:39:11 -04:00
|
|
|
end
|