Repository: rails/rails
File: actionpack/lib/action_dispatch/http/mime_negotiation.rb (Ruby)

Code before:

      def formats
        fetch_header("action_dispatch.request.formats") do |k|
          v = if params_readable?
            Array(Mime[parameters[:format]])
          elsif use_accept_header && valid_accept_header
            accepts.dup
          elsif extension_format = format_from_path_extension
            [extension_format]
          elsif xhr?
            [Mime[:js]]
          else
            [Mime[:html]]
          end

          v.select! do |format|
            format.symbol || format.ref == "*/*"
          end

          set_header k, v
        end
      end

      # Sets the \variant for the response template.
      #
      # Action View will incorporate the request's variants when determining which template to render. For example, if an <tt>ArticlesController</tt> needs to respond to an <tt>#index</tt> action request with <tt>request.variant = [:ios, :turbo_native]</tt>, it will render the first template file it can find in the following list:
      #
      # - `app/views/articles/index.html+ios.erb`
      # - `app/views/articles/index.html+turbo_native.erb`
      # - `app/views/articles/index.html.erb`
      #
      # Variant names can refer to any aspect of a request, from the type of
      # User Agent (e.g. iOS, Android, etc.), to the version of the requesting
      # application (e.g. v1, v2, etc.) to the level of permissions available to
      # the requesting User (e.g. administrator, contributor, guest, etc.).
      #
      # A word of caution: be stingy when adding new variants. Only reach for
      # variants when groups of conditionals (that share a theme) or other
      # more traditional templating techniques become unmanageable.
      #
      # ==== Parameters
      #
      # * +variant+ - a Symbol name or an Array of Symbol names for variants
      #   used to render the response template
      #
      # ==== Examples
      #
      #   class ApplicationController < ActionController::Base
      #     before_action :determine_variants
      #
      #     private

Review comment (suggestion):
```suggestion
      # When determining which template to render, Action View will incorporate all variants from the request.
      # For example, if an <tt>ArticlesController#index</tt> action needs to respond to <tt>request.variant = [:ios, :turbo_native]</tt>, it will render the first template file it can find in the following list:
```

Code after:
      #
      #     # GET /posts/5
      #     request.format # => Mime[:html] or Mime[:js], or request.accepts.first
      #
      def format(_view_path = nil)
        formats.first || Mime::NullType.instance
      end

      def formats
        fetch_header("action_dispatch.request.formats") do |k|
          v = if params_readable?
            Array(Mime[parameters[:format]])
          elsif use_accept_header && valid_accept_header
            accepts.dup
          elsif extension_format = format_from_path_extension
            [extension_format]
          elsif xhr?
            [Mime[:js]]
          else
            [Mime[:html]]
          end

          v.select! do |format|
            format.symbol || format.ref == "*/*"
          end

          set_header k, v
        end
      end

      # Sets the \variant for the response template.
      #
      # When determining which template to render, Action View will incorporate
      # all variants from the request. For example, if an
      # `ArticlesController#index` action needs to respond to
      # `request.variant = [:ios, :turbo_native]`, it will render the
      # first template file it can find in the following list:
      #
      # - `app/views/articles/index.html+ios.erb`
      # - `app/views/articles/index.html+turbo_native.erb`
      # - `app/views/articles/index.html.erb`
      #
      # Variants add context to the requests that views render appropriately.
      # Variant names are arbitrary, and can communicate anything from the
      # request's platform (`:android`, `:ios`, `:linux`, `:macos`, `:windows`)
      # to its browser (`:chrome`, `:edge`, `:firefox`, `:safari`), to the type
      # of user (`:admin`, `:guest`, `:user`).
      #
      # Note: Adding many new variant templates with similarities to existing
      # template files can make maintaining your view code more difficult.
      #