Sha256: b131180809460dd62d93b0d1f114bf83847be2454713e764a315d5bce363f24f

Contents?: true

Size: 1.29 KB

Versions: 25

Compression:

Stored size: 1.29 KB

Contents

# 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
      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, &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)
        send(symbol, &block)
      else
        super
      end
    end
  end
end

Version data entries

25 entries across 23 versions & 5 rubygems

Version Path
actionpack-6.0.2.2 lib/abstract_controller/collector.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/abstract_controller/collector.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/actionpack-6.0.2.1/lib/abstract_controller/collector.rb
actionpack-6.0.2.1 lib/abstract_controller/collector.rb
actionpack-6.0.2 lib/abstract_controller/collector.rb
actionpack-6.0.2.rc2 lib/abstract_controller/collector.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/abstract_controller/collector.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
actionpack-6.0.2.rc1 lib/abstract_controller/collector.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/actionpack-6.0.1/lib/abstract_controller/collector.rb
actionpack-6.0.1 lib/abstract_controller/collector.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
actionpack-6.0.1.rc1 lib/abstract_controller/collector.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
zuora_connect_ui-0.9.1 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
zuora_connect_ui-0.9.0 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/collector.rb
actionpack-6.0.0 lib/abstract_controller/collector.rb