2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-02-26 04:27:06 -05:00
|
|
|
require "action_dispatch/http/mime_type"
|
|
|
|
|
2010-01-23 04:23:06 -05:00
|
|
|
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
|
2015-10-05 01:14:04 -04:00
|
|
|
def #{sym}(*args, &block)
|
|
|
|
custom(Mime[:#{sym}], *args, &block)
|
|
|
|
end
|
2010-01-23 04:23:06 -05:00
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
|
|
|
|
Mime::SET.each do |mime|
|
|
|
|
generate_method_for_mime(mime)
|
|
|
|
end
|
|
|
|
|
2012-07-06 11:19:20 -04:00
|
|
|
Mime::Type.register_callback do |mime|
|
2016-08-07 19:05:28 -04:00
|
|
|
generate_method_for_mime(mime) unless instance_methods.include?(mime.to_sym)
|
2012-07-06 11:19:20 -04:00
|
|
|
end
|
|
|
|
|
2016-12-22 04:48:24 -05:00
|
|
|
private
|
2010-01-23 04:23:06 -05:00
|
|
|
|
|
|
|
def method_missing(symbol, &block)
|
2015-10-05 01:14:04 -04:00
|
|
|
unless mime_constant = Mime[symbol]
|
2013-12-03 19:23:11 -05:00
|
|
|
raise NoMethodError, "To respond to a custom format, register it as a MIME type first: " \
|
|
|
|
"http://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
|
2013-12-03 05:17:01 -05:00
|
|
|
|
2010-01-23 04:23:06 -05:00
|
|
|
if Mime::SET.include?(mime_constant)
|
|
|
|
AbstractController::Collector.generate_method_for_mime(mime_constant)
|
|
|
|
send(symbol, &block)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-06-04 16:24:55 -04:00
|
|
|
end
|