mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
0740772653
Without this fix, generated MIME methods can't recognize kwargs: ``` % bin/test -w test/abstract/collector_test.rb:53 Running 5 tests in a single process (parallelization threshold is 50) Run options: --seed 56716 # Running: F Failure: AbstractController::Testing::TestCollector#test_generated_methods_call_custom_with_arguments_received [/Users/kamipo/src/github.com/rails/rails/actionpack/test/abstract/collector_test.rb:59]: --- expected +++ actual @@ -1 +1 @@ -[#<Mime::Type:0xXXXXXX @synonyms=[], @symbol=:text, @string="text/plain", @hash=-3828881190283196326>, [:foo], {:bar=>:baz}, nil] +[#<Mime::Type:0xXXXXXX @synonyms=[], @symbol=:text, @string="text/plain", @hash=-3828881190283196326>, [:foo, {:bar=>:baz}], {}, nil] ```
44 lines
1.4 KiB
Ruby
44 lines
1.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "action_dispatch/http/mime_type"
|
|
|
|
module AbstractController
|
|
module Collector
|
|
def self.generate_method_for_mime(mime)
|
|
sym = mime.is_a?(Symbol) ? mime : mime.to_sym
|
|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
def #{sym}(*args, &block)
|
|
custom(Mime[:#{sym}], *args, &block)
|
|
end
|
|
ruby2_keywords(:#{sym})
|
|
RUBY
|
|
end
|
|
|
|
Mime::SET.each do |mime|
|
|
generate_method_for_mime(mime)
|
|
end
|
|
|
|
Mime::Type.register_callback do |mime|
|
|
generate_method_for_mime(mime) unless instance_methods.include?(mime.to_sym)
|
|
end
|
|
|
|
private
|
|
def method_missing(symbol, *args, &block)
|
|
unless mime_constant = Mime[symbol]
|
|
raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
|
|
"https://guides.rubyonrails.org/action_controller_overview.html#restful-downloads. " \
|
|
"If you meant to respond to a variant like :tablet or :phone, not a custom format, " \
|
|
"be sure to nest your variant response within a format response: " \
|
|
"format.html { |html| html.tablet { ... } }"
|
|
end
|
|
|
|
if Mime::SET.include?(mime_constant)
|
|
AbstractController::Collector.generate_method_for_mime(mime_constant)
|
|
public_send(symbol, *args, &block)
|
|
else
|
|
super
|
|
end
|
|
end
|
|
ruby2_keywords(:method_missing)
|
|
end
|
|
end
|