mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Get rid of update_details in favor of passing details to find_template.
This commit is contained in:
parent
cbaad674f1
commit
119e9e2daf
10 changed files with 50 additions and 86 deletions
|
@ -113,6 +113,6 @@ class BaseMailer < ActionMailer::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
def email_with_translations
|
def email_with_translations
|
||||||
mail :body => render("email_with_translations.html")
|
mail :body => render("email_with_translations", :formats => [:html])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,18 +20,16 @@ module ActionView
|
||||||
|
|
||||||
def self.register_detail(name, options = {}, &block)
|
def self.register_detail(name, options = {}, &block)
|
||||||
self.registered_details << name
|
self.registered_details << name
|
||||||
|
|
||||||
initialize = registered_details.map { |n| "self.#{n} = details[:#{n}]" }
|
initialize = registered_details.map { |n| "self.#{n} = details[:#{n}]" }
|
||||||
update = registered_details.map { |n| "self.#{n} = details[:#{n}] if details.key?(:#{n})" }
|
|
||||||
|
|
||||||
Accessors.send :define_method, :"_#{name}_defaults", &block
|
Accessors.send :define_method, :"default_#{name}", &block
|
||||||
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||||
def #{name}
|
def #{name}
|
||||||
@details[:#{name}]
|
@details[:#{name}]
|
||||||
end
|
end
|
||||||
|
|
||||||
def #{name}=(value)
|
def #{name}=(value)
|
||||||
value = Array.wrap(value.presence || _#{name}_defaults)
|
value = Array.wrap(value.presence || default_#{name})
|
||||||
_set_detail(:#{name}, value) if value != @details[:#{name}]
|
_set_detail(:#{name}, value) if value != @details[:#{name}]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -39,11 +37,6 @@ module ActionView
|
||||||
def initialize_details(details)
|
def initialize_details(details)
|
||||||
#{initialize.join("\n")}
|
#{initialize.join("\n")}
|
||||||
end
|
end
|
||||||
|
|
||||||
remove_possible_method :update_details
|
|
||||||
def update_details(details)
|
|
||||||
#{update.join("\n")}
|
|
||||||
end
|
|
||||||
METHOD
|
METHOD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -88,24 +81,6 @@ module ActionView
|
||||||
@cache = old_value
|
@cache = old_value
|
||||||
end
|
end
|
||||||
|
|
||||||
# Update the details keys by merging the given hash into the current
|
|
||||||
# details hash. If a block is given, the details are modified just during
|
|
||||||
# the execution of the block and reverted to the previous value after.
|
|
||||||
def update_details(new_details)
|
|
||||||
if block_given?
|
|
||||||
old_details, old_key = @details.dup, @details_key
|
|
||||||
super
|
|
||||||
|
|
||||||
begin
|
|
||||||
yield
|
|
||||||
ensure
|
|
||||||
@details, @details_key = old_details, old_key
|
|
||||||
end
|
|
||||||
else
|
|
||||||
super
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def _set_detail(key, value)
|
def _set_detail(key, value)
|
||||||
|
@ -220,7 +195,7 @@ module ActionView
|
||||||
# add :html as fallback to :js.
|
# add :html as fallback to :js.
|
||||||
def formats=(values)
|
def formats=(values)
|
||||||
if values
|
if values
|
||||||
values.concat(_formats_defaults) if values.delete "*/*"
|
values.concat(default_formats) if values.delete "*/*"
|
||||||
values << :html if values == [:js]
|
values << :html if values == [:js]
|
||||||
end
|
end
|
||||||
super(values)
|
super(values)
|
||||||
|
@ -246,7 +221,7 @@ module ActionView
|
||||||
config.locale = value
|
config.locale = value
|
||||||
end
|
end
|
||||||
|
|
||||||
super(@skip_default_locale ? I18n.locale : _locale_defaults)
|
super(@skip_default_locale ? I18n.locale : default_locale)
|
||||||
end
|
end
|
||||||
|
|
||||||
# A method which only uses the first format in the formats array for layout lookup.
|
# A method which only uses the first format in the formats array for layout lookup.
|
||||||
|
|
|
@ -11,15 +11,13 @@ module ActionView
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
# Checks if the given path contains a format and if so, change
|
protected
|
||||||
# the lookup context to take this new format into account.
|
|
||||||
def wrap_formats(value)
|
|
||||||
return yield unless value.is_a?(String)
|
|
||||||
|
|
||||||
if value.sub!(formats_regexp, "")
|
def extract_format(value, details)
|
||||||
update_details(:formats => [$1.to_sym]){ yield }
|
if value.is_a?(String) && value.sub!(formats_regexp, "")
|
||||||
else
|
ActiveSupport::Deprecation.warn "Passing the format in the template name is deprecated. " \
|
||||||
yield
|
"Please pass render with :formats => #{$1} instead.", caller
|
||||||
|
details[:formats] ||= [$1.to_sym]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -27,8 +25,6 @@ module ActionView
|
||||||
@@formats_regexp ||= /\.(#{Mime::SET.symbols.join('|')})$/
|
@@formats_regexp ||= /\.(#{Mime::SET.symbols.join('|')})$/
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def instrument(name, options={})
|
def instrument(name, options={})
|
||||||
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
|
ActiveSupport::Notifications.instrument("render_#{name}.action_view", options){ yield }
|
||||||
end
|
end
|
||||||
|
|
|
@ -216,18 +216,15 @@ module ActionView
|
||||||
|
|
||||||
def render(context, options, block)
|
def render(context, options, block)
|
||||||
setup(context, options, block)
|
setup(context, options, block)
|
||||||
|
identifier = (@template = find_partial) ? @template.identifier : @path
|
||||||
|
|
||||||
wrap_formats(@path) do
|
if @collection
|
||||||
identifier = ((@template = find_partial) ? @template.identifier : @path)
|
instrument(:collection, :identifier => identifier || "collection", :count => @collection.size) do
|
||||||
|
render_collection
|
||||||
if @collection
|
end
|
||||||
instrument(:collection, :identifier => identifier || "collection", :count => @collection.size) do
|
else
|
||||||
render_collection
|
instrument(:partial, :identifier => identifier) do
|
||||||
end
|
render_partial
|
||||||
else
|
|
||||||
instrument(:partial, :identifier => identifier) do
|
|
||||||
render_partial
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -271,6 +268,7 @@ module ActionView
|
||||||
@options = options
|
@options = options
|
||||||
@locals = options[:locals] || {}
|
@locals = options[:locals] || {}
|
||||||
@block = block
|
@block = block
|
||||||
|
@details = options.slice(:formats, :locale, :handlers)
|
||||||
|
|
||||||
if String === partial
|
if String === partial
|
||||||
@object = options[:object]
|
@object = options[:object]
|
||||||
|
@ -299,6 +297,7 @@ module ActionView
|
||||||
"and is followed by any combinations of letters, numbers, or underscores.")
|
"and is followed by any combinations of letters, numbers, or underscores.")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
extract_format(@path, @details)
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -326,7 +325,7 @@ module ActionView
|
||||||
|
|
||||||
def find_template(path=@path, locals=@locals.keys)
|
def find_template(path=@path, locals=@locals.keys)
|
||||||
prefixes = path.include?(?/) ? [] : @lookup_context.prefixes
|
prefixes = path.include?(?/) ? [] : @lookup_context.prefixes
|
||||||
@lookup_context.find_template(path, prefixes, true, locals)
|
@lookup_context.find_template(path, prefixes, true, locals, @details)
|
||||||
end
|
end
|
||||||
|
|
||||||
def collection_with_template
|
def collection_with_template
|
||||||
|
|
|
@ -4,30 +4,28 @@ require 'active_support/core_ext/array/wrap'
|
||||||
module ActionView
|
module ActionView
|
||||||
class TemplateRenderer < AbstractRenderer #:nodoc:
|
class TemplateRenderer < AbstractRenderer #:nodoc:
|
||||||
def render(context, options)
|
def render(context, options)
|
||||||
@view = context
|
@view = context
|
||||||
|
@details = options.slice(:formats, :locale, :handlers)
|
||||||
wrap_formats(options[:template] || options[:file]) do
|
extract_format(options[:file] || options[:template], @details)
|
||||||
template = determine_template(options)
|
template = determine_template(options)
|
||||||
freeze_formats(template.formats, true)
|
freeze_formats(template.formats, true)
|
||||||
render_template(template, options[:layout], options[:locals])
|
render_template(template, options[:layout], options[:locals])
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Determine the template to be rendered using the given options.
|
# Determine the template to be rendered using the given options.
|
||||||
def determine_template(options) #:nodoc:
|
def determine_template(options) #:nodoc:
|
||||||
keys = options[:locals].try(:keys) || []
|
keys = options[:locals].try(:keys) || []
|
||||||
details = options.slice(:formats, :locale, :handlers)
|
|
||||||
|
|
||||||
if options.key?(:text)
|
if options.key?(:text)
|
||||||
Template::Text.new(options[:text], formats.try(:first))
|
Template::Text.new(options[:text], formats.try(:first))
|
||||||
elsif options.key?(:file)
|
elsif options.key?(:file)
|
||||||
with_fallbacks { find_template(options[:file], nil, false, keys, details) }
|
with_fallbacks { find_template(options[:file], nil, false, keys, @details) }
|
||||||
elsif options.key?(:inline)
|
elsif options.key?(:inline)
|
||||||
handler = Template.handler_for_extension(options[:type] || "erb")
|
handler = Template.handler_for_extension(options[:type] || "erb")
|
||||||
Template.new(options[:inline], "inline template", handler, :locals => keys)
|
Template.new(options[:inline], "inline template", handler, :locals => keys)
|
||||||
elsif options.key?(:template)
|
elsif options.key?(:template)
|
||||||
options[:template].respond_to?(:render) ?
|
options[:template].respond_to?(:render) ?
|
||||||
options[:template] : find_template(options[:template], options[:prefixes], false, keys)
|
options[:template] : find_template(options[:template], options[:prefixes], false, keys, @details)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -63,12 +61,11 @@ module ActionView
|
||||||
begin
|
begin
|
||||||
with_layout_format do
|
with_layout_format do
|
||||||
layout =~ /^\// ?
|
layout =~ /^\// ?
|
||||||
with_fallbacks { find_template(layout, nil, false, keys) } : find_template(layout, nil, false, keys)
|
with_fallbacks { find_template(layout, nil, false, keys, @details) } : find_template(layout, nil, false, keys, @details)
|
||||||
end
|
end
|
||||||
rescue ActionView::MissingTemplate
|
rescue ActionView::MissingTemplate
|
||||||
update_details(:formats => nil) do
|
all_details = @details.merge(:formats => @lookup_context.default_formats)
|
||||||
raise unless template_exists?(layout)
|
raise unless template_exists?(layout, nil, false, keys, all_details)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,12 +7,12 @@ module RenderPartial
|
||||||
self.view_paths = [ActionView::FixtureResolver.new(
|
self.view_paths = [ActionView::FixtureResolver.new(
|
||||||
"render_partial/basic/_basic.html.erb" => "BasicPartial!",
|
"render_partial/basic/_basic.html.erb" => "BasicPartial!",
|
||||||
"render_partial/basic/basic.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'basic' %><%= @test_unchanged %>",
|
"render_partial/basic/basic.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'basic' %><%= @test_unchanged %>",
|
||||||
"render_partial/basic/with_json.html.erb" => "<%= render 'with_json.json' %>",
|
"render_partial/basic/with_json.html.erb" => "<%= render :partial => 'with_json', :formats => [:json] %>",
|
||||||
"render_partial/basic/_with_json.json.erb" => "<%= render 'final' %>",
|
"render_partial/basic/_with_json.json.erb" => "<%= render :partial => 'final', :formats => [:json] %>",
|
||||||
"render_partial/basic/_final.json.erb" => "{ final: json }",
|
"render_partial/basic/_final.json.erb" => "{ final: json }",
|
||||||
"render_partial/basic/overriden.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'overriden' %><%= @test_unchanged %>",
|
"render_partial/basic/overriden.html.erb" => "<%= @test_unchanged = 'goodbye' %><%= render :partial => 'overriden' %><%= @test_unchanged %>",
|
||||||
"render_partial/basic/_overriden.html.erb" => "ParentPartial!",
|
"render_partial/basic/_overriden.html.erb" => "ParentPartial!",
|
||||||
"render_partial/child/_overriden.html.erb" => "OverridenPartial!"
|
"render_partial/child/_overriden.html.erb" => "OverridenPartial!"
|
||||||
)]
|
)]
|
||||||
|
|
||||||
def html_with_json_inside_json
|
def html_with_json_inside_json
|
||||||
|
|
|
@ -10,8 +10,8 @@ module RenderTemplate
|
||||||
"xml_template.xml.builder" => "xml.html do\n xml.p 'Hello'\nend",
|
"xml_template.xml.builder" => "xml.html do\n xml.p 'Hello'\nend",
|
||||||
"with_raw.html.erb" => "Hello <%=raw '<strong>this is raw</strong>' %>",
|
"with_raw.html.erb" => "Hello <%=raw '<strong>this is raw</strong>' %>",
|
||||||
"with_implicit_raw.html.erb" => "Hello <%== '<strong>this is also raw</strong>' %>",
|
"with_implicit_raw.html.erb" => "Hello <%== '<strong>this is also raw</strong>' %>",
|
||||||
"test/with_json.html.erb" => "<%= render :template => 'test/with_json.json' %>",
|
"test/with_json.html.erb" => "<%= render :template => 'test/with_json', :formats => [:json] %>",
|
||||||
"test/with_json.json.erb" => "<%= render :template => 'test/final' %>",
|
"test/with_json.json.erb" => "<%= render :template => 'test/final', :formats => [:json] %>",
|
||||||
"test/final.json.erb" => "{ final: json }",
|
"test/final.json.erb" => "{ final: json }",
|
||||||
"test/with_error.html.erb" => "<%= idontexist %>"
|
"test/with_error.html.erb" => "<%= idontexist %>"
|
||||||
)]
|
)]
|
||||||
|
@ -117,7 +117,7 @@ module RenderTemplate
|
||||||
assert_response "{ final: json }"
|
assert_response "{ final: json }"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "rendering a template with error properly exceprts the code" do
|
test "rendering a template with error properly excerts the code" do
|
||||||
get :with_error
|
get :with_error
|
||||||
assert_status 500
|
assert_status 500
|
||||||
assert_match "undefined local variable or method `idontexist'", response.body
|
assert_match "undefined local variable or method `idontexist'", response.body
|
||||||
|
|
|
@ -797,7 +797,9 @@ class RenderTest < ActionController::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_render_file
|
def test_render_file
|
||||||
get :hello_world_file
|
assert_deprecated do
|
||||||
|
get :hello_world_file
|
||||||
|
end
|
||||||
assert_equal "Hello world!", @response.body
|
assert_equal "Hello world!", @response.body
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -31,16 +31,6 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
assert @lookup_context.formats.frozen?
|
assert @lookup_context.formats.frozen?
|
||||||
end
|
end
|
||||||
|
|
||||||
test "allows me to change some details to execute an specific block of code" do
|
|
||||||
formats = Mime::SET
|
|
||||||
@lookup_context.update_details(:locale => :pt) do
|
|
||||||
assert_equal formats, @lookup_context.formats
|
|
||||||
assert_equal :pt, @lookup_context.locale
|
|
||||||
end
|
|
||||||
assert_equal formats, @lookup_context.formats
|
|
||||||
assert_equal :en, @lookup_context.locale
|
|
||||||
end
|
|
||||||
|
|
||||||
test "provides getters and setters for formats" do
|
test "provides getters and setters for formats" do
|
||||||
@lookup_context.formats = [:html]
|
@lookup_context.formats = [:html]
|
||||||
assert_equal [:html], @lookup_context.formats
|
assert_equal [:html], @lookup_context.formats
|
||||||
|
|
|
@ -38,6 +38,11 @@ module RenderTestCases
|
||||||
assert_equal "<error>No Comment</error>", @view.render(:file => "comments/empty", :formats => [:xml])
|
assert_equal "<error>No Comment</error>", @view.render(:file => "comments/empty", :formats => [:xml])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_template_with_format
|
||||||
|
assert_equal "<h1>No Comment</h1>", @view.render(:template => "comments/empty", :formats => [:html])
|
||||||
|
assert_equal "<error>No Comment</error>", @view.render(:template => "comments/empty", :formats => [:xml])
|
||||||
|
end
|
||||||
|
|
||||||
def test_render_file_with_localization
|
def test_render_file_with_localization
|
||||||
old_locale, @view.locale = @view.locale, :da
|
old_locale, @view.locale = @view.locale, :da
|
||||||
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
|
assert_equal "Hey verden", @view.render(:file => "test/hello_world")
|
||||||
|
|
Loading…
Reference in a new issue