1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

it is now possible to pass details options (:formats, :details, :locales, ...) to #render, #find_template and friends. this doesn't change anything in global context.

This commit is contained in:
Nick Sutterer 2011-09-22 11:54:13 +02:00 committed by José Valim
parent 41bca5d466
commit cbaad674f1
5 changed files with 27 additions and 10 deletions

View file

@ -125,17 +125,17 @@ module ActionView
@view_paths = ActionView::PathSet.new(Array.wrap(paths))
end
def find(name, prefixes = [], partial = false, keys = [])
@view_paths.find(*args_for_lookup(name, prefixes, partial, keys))
def find(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.find(*args_for_lookup(name, prefixes, partial, keys, options))
end
alias :find_template :find
def find_all(name, prefixes = [], partial = false, keys = [])
@view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys))
def find_all(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.find_all(*args_for_lookup(name, prefixes, partial, keys, options))
end
def exists?(name, prefixes = [], partial = false, keys = [])
@view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys))
def exists?(name, prefixes = [], partial = false, keys = [], options = {})
@view_paths.exists?(*args_for_lookup(name, prefixes, partial, keys, options))
end
alias :template_exists? :exists?
@ -154,9 +154,17 @@ module ActionView
protected
def args_for_lookup(name, prefixes, partial, keys) #:nodoc:
def args_for_lookup(name, prefixes, partial, keys, details_options) #:nodoc:
name, prefixes = normalize_name(name, prefixes)
[name, prefixes, partial || false, @details, details_key, keys]
details, details_key = detail_args_for(details_options)
[name, prefixes, partial || false, details, details_key, keys]
end
# Compute details hash and key according to user options (e.g. passed from #render).
def detail_args_for(options)
return @details, details_key if options.empty? # most common path.
user_details = @details.merge(options)
[user_details, DetailsKey.get(user_details)]
end
# Support legacy foo.erb names even though we now ignore .erb

View file

@ -16,11 +16,12 @@ module ActionView
# Determine the template to be rendered using the given options.
def determine_template(options) #:nodoc:
keys = options[:locals].try(:keys) || []
details = options.slice(:formats, :locale, :handlers)
if options.key?(:text)
Template::Text.new(options[:text], formats.try(:first))
elsif options.key?(:file)
with_fallbacks { find_template(options[:file], nil, false, keys) }
with_fallbacks { find_template(options[:file], nil, false, keys, details) }
elsif options.key?(:inline)
handler = Template.handler_for_extension(options[:type] || "erb")
Template.new(options[:inline], "inline template", handler, :locals => keys)

View file

@ -0,0 +1 @@
<h1>No Comment</h1>

View file

@ -0,0 +1 @@
<error>No Comment</error>

View file

@ -32,6 +32,12 @@ module RenderTestCases
assert_equal "Hello world!", @view.render(:file => "test/hello_world")
end
# Test if :formats, :locale etc. options are passed correctly to the resolvers.
def test_render_file_with_format
assert_equal "<h1>No Comment</h1>", @view.render(:file => "comments/empty", :formats => [:html])
assert_equal "<error>No Comment</error>", @view.render(:file => "comments/empty", :formats => [:xml])
end
def test_render_file_with_localization
old_locale, @view.locale = @view.locale, :da
assert_equal "Hey verden", @view.render(:file => "test/hello_world")