Skip to content

Commit

Permalink
Merge pull request #35661 from jhawthorn/lookup_context_validation
Browse files Browse the repository at this point in the history
Validate types assigned to LookupContext#formats=
  • Loading branch information
eileencodes committed Mar 20, 2019
2 parents c11d115 + 0756733 commit d369911
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
8 changes: 8 additions & 0 deletions actionview/lib/action_view/lookup_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,15 @@ def initialize_details(target, details)
# add :html as fallback to :js.
def formats=(values)
if values
values = values.dup
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]
values << :html
@html_fallback_for_js = true
Expand Down
2 changes: 1 addition & 1 deletion actionview/lib/action_view/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def _render_template(options)
# Assign the rendered format to look up context.
def _process_format(format)
super
lookup_context.formats = [format.to_sym]
lookup_context.formats = [format.to_sym] if format.to_sym
end

# Normalize args by converting render "foo" to render :action => "foo" and
Expand Down
10 changes: 9 additions & 1 deletion actionview/test/template/lookup_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,22 @@ def teardown

test "handles explicitly defined */* formats fallback to :js" do
@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

test "adds :html fallback to :js formats" do
@lookup_context.formats = [:js]
assert_equal [:js, :html], @lookup_context.formats
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
@lookup_context.locale = :pt
assert_equal :pt, @lookup_context.locale
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/mailers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def preview
end
else
@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
else
raise AbstractController::ActionNotFound, "Email '#{@email_action}' not found in #{@preview.name}"
Expand Down

0 comments on commit d369911

Please sign in to comment.