mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #35661 from jhawthorn/lookup_context_validation
Validate types assigned to LookupContext#formats=
This commit is contained in:
commit
d369911478
4 changed files with 19 additions and 3 deletions
|
@ -280,7 +280,15 @@ 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 = values.dup
|
||||||
values.concat(default_formats) if values.delete "*/*"
|
values.concat(default_formats) if values.delete "*/*"
|
||||||
|
values.uniq!
|
||||||
|
|
||||||
|
invalid_values = (values - Template::Types.symbols)
|
||||||
|
unless invalid_values.empty?
|
||||||
|
raise ArgumentError, "Invalid formats: #{invalid_values.map(&:inspect).join(", ")}"
|
||||||
|
end
|
||||||
|
|
||||||
if values == [:js]
|
if values == [:js]
|
||||||
values << :html
|
values << :html
|
||||||
@html_fallback_for_js = true
|
@html_fallback_for_js = true
|
||||||
|
|
|
@ -127,7 +127,7 @@ module ActionView
|
||||||
# Assign the rendered format to look up context.
|
# Assign the rendered format to look up context.
|
||||||
def _process_format(format)
|
def _process_format(format)
|
||||||
super
|
super
|
||||||
lookup_context.formats = [format.to_sym]
|
lookup_context.formats = [format.to_sym] if format.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
# Normalize args by converting render "foo" to render :action => "foo" and
|
# Normalize args by converting render "foo" to render :action => "foo" and
|
||||||
|
|
|
@ -67,7 +67,7 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
test "handles explicitly defined */* formats fallback to :js" do
|
test "handles explicitly defined */* formats fallback to :js" do
|
||||||
@lookup_context.formats = [:js, Mime::ALL]
|
@lookup_context.formats = [:js, Mime::ALL]
|
||||||
assert_equal [:js, *Mime::SET.symbols], @lookup_context.formats
|
assert_equal [:js, *Mime::SET.symbols].uniq, @lookup_context.formats
|
||||||
end
|
end
|
||||||
|
|
||||||
test "adds :html fallback to :js formats" do
|
test "adds :html fallback to :js formats" do
|
||||||
|
@ -75,6 +75,14 @@ class LookupContextTest < ActiveSupport::TestCase
|
||||||
assert_equal [:js, :html], @lookup_context.formats
|
assert_equal [:js, :html], @lookup_context.formats
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "raises on invalid format assignment" do
|
||||||
|
ex = assert_raises ArgumentError do
|
||||||
|
@lookup_context.formats = [:html, :invalid, "also bad"]
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal 'Invalid formats: :invalid, "also bad"', ex.message
|
||||||
|
end
|
||||||
|
|
||||||
test "provides getters and setters for locale" do
|
test "provides getters and setters for locale" do
|
||||||
@lookup_context.locale = :pt
|
@lookup_context.locale = :pt
|
||||||
assert_equal :pt, @lookup_context.locale
|
assert_equal :pt, @lookup_context.locale
|
||||||
|
|
|
@ -38,7 +38,7 @@ class Rails::MailersController < Rails::ApplicationController # :nodoc:
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@part = find_preferred_part(request.format, Mime[:html], Mime[:text])
|
@part = find_preferred_part(request.format, Mime[:html], Mime[:text])
|
||||||
render action: "email", layout: false, formats: %w[html]
|
render action: "email", layout: false, formats: [:html]
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
|
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
|
||||||
|
|
Loading…
Reference in a new issue