mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #16224 from egilburg/renderer_refactors
Some ActionView renderer refactors
This commit is contained in:
commit
e89ef3cee9
4 changed files with 32 additions and 17 deletions
|
@ -29,8 +29,9 @@ module ActionView
|
|||
|
||||
def extract_details(options)
|
||||
@lookup_context.registered_details.each_with_object({}) do |key, details|
|
||||
next unless value = options[key]
|
||||
details[key] = Array(value)
|
||||
value = options[key]
|
||||
|
||||
details[key] = Array(value) if value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -41,6 +42,7 @@ module ActionView
|
|||
def prepend_formats(formats)
|
||||
formats = Array(formats)
|
||||
return if formats.empty? || @lookup_context.html_fallback_for_js
|
||||
|
||||
@lookup_context.formats = formats | @lookup_context.formats
|
||||
end
|
||||
end
|
||||
|
|
|
@ -312,6 +312,8 @@ module ActionView
|
|||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_collection
|
||||
return nil if @collection.blank?
|
||||
|
||||
|
@ -353,25 +355,27 @@ module ActionView
|
|||
# respond to +to_partial_path+ in order to setup the path.
|
||||
def setup(context, options, block)
|
||||
@view = context
|
||||
partial = options[:partial]
|
||||
|
||||
@options = options
|
||||
@locals = options[:locals] || {}
|
||||
@block = block
|
||||
|
||||
@locals = options[:locals] || {}
|
||||
@details = extract_details(options)
|
||||
|
||||
prepend_formats(options[:formats])
|
||||
|
||||
partial = options[:partial]
|
||||
|
||||
if String === partial
|
||||
@object = options[:object]
|
||||
@collection = collection_from_options
|
||||
@path = partial
|
||||
@collection = collection
|
||||
else
|
||||
@object = partial
|
||||
@collection = collection_from_object || collection_from_options
|
||||
|
||||
if @collection = collection_from_object || collection
|
||||
if @collection
|
||||
paths = @collection_data = @collection.map { |o| partial_path(o) }
|
||||
@path = paths.uniq.size == 1 ? paths.first : nil
|
||||
@path = paths.uniq.one? ? paths.first : nil
|
||||
else
|
||||
@path = partial_path
|
||||
end
|
||||
|
@ -392,7 +396,7 @@ module ActionView
|
|||
self
|
||||
end
|
||||
|
||||
def collection
|
||||
def collection_from_options
|
||||
if @options.key?(:collection)
|
||||
collection = @options[:collection]
|
||||
collection.respond_to?(:to_ary) ? collection.to_ary : []
|
||||
|
@ -404,9 +408,7 @@ module ActionView
|
|||
end
|
||||
|
||||
def find_partial
|
||||
if path = @path
|
||||
find_template(path, @template_keys)
|
||||
end
|
||||
find_template(@path, @template_keys) if @path
|
||||
end
|
||||
|
||||
def find_template(path, locals)
|
||||
|
|
|
@ -6,19 +6,18 @@ module ActionView
|
|||
@view = context
|
||||
@details = extract_details(options)
|
||||
template = determine_template(options)
|
||||
context = @lookup_context
|
||||
|
||||
prepend_formats(template.formats)
|
||||
|
||||
unless context.rendered_format
|
||||
context.rendered_format = template.formats.first || formats.first
|
||||
end
|
||||
@lookup_context.rendered_format ||= (template.formats.first || formats.first)
|
||||
|
||||
render_template(template, options[:layout], options[:locals])
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Determine the template to be rendered using the given options.
|
||||
def determine_template(options) #:nodoc:
|
||||
def determine_template(options)
|
||||
keys = options.fetch(:locals, {}).keys
|
||||
|
||||
if options.key?(:body)
|
||||
|
|
|
@ -324,6 +324,10 @@ module RenderTestCases
|
|||
@controller_view.render(customers, :greeting => "Hello")
|
||||
end
|
||||
|
||||
def test_render_partial_using_collection_without_path
|
||||
assert_equal "hi good customer: david0", @controller_view.render([ GoodCustomer.new("david") ], greeting: "hi")
|
||||
end
|
||||
|
||||
def test_render_partial_without_object_or_collection_does_not_generate_partial_name_local_variable
|
||||
exception = assert_raises ActionView::Template::Error do
|
||||
@controller_view.render("partial_name_local_variable")
|
||||
|
@ -388,6 +392,14 @@ module RenderTestCases
|
|||
ActionView::Template.unregister_template_handler :foo
|
||||
end
|
||||
|
||||
def test_render_body
|
||||
assert_equal 'some body', @view.render(body: 'some body')
|
||||
end
|
||||
|
||||
def test_render_plain
|
||||
assert_equal 'some plaintext', @view.render(plain: 'some plaintext')
|
||||
end
|
||||
|
||||
def test_render_knows_about_types_registered_when_extensions_are_checked_earlier_in_initialization
|
||||
ActionView::Template::Handlers.extensions
|
||||
ActionView::Template.register_template_handler :foo, CustomHandler
|
||||
|
|
Loading…
Reference in a new issue