Repository: rails/rails
File: actionview/lib/action_view/helpers/url_helper.rb (Ruby)

Code before:
      #
      #   current_page?(controller: 'product', action: 'index')
      #   # => false
      #
      #   current_page?(controller: 'product', action: 'create')
      #   # => false
      #
      #   current_page?(controller: 'product', action: 'create', method: :post)
      #   # => true
      #
      #   current_page?(controller: 'product', action: 'index', method: [:get, :post])
      #   # => true
      #
      # We can also pass in the symbol arguments instead of strings.
      #
      def current_page?(options = nil, check_parameters: false, method: :get, **options_as_kwargs)
        unless request
          raise "You cannot use helpers that need to determine the current " \
                "page unless your view context provides a Request object " \
                "in a #request method"
        end

        options ||= options_as_kwargs
        if options.is_a?(Hash)
          check_parameters ||= options.delete(:check_parameters)
          method ||= options.delete(:method)
        end

        method_matches = if method == :get
          request.get? || request.head?
        else
          request_method = request.method.downcase.to_sym
          if method.is_a?(Array)
            method << :head if method.include?(:get)
            method.include?(request_method)
          else
            method == request_method
          end
        end
        return false unless method_matches

        url_string = URI::RFC2396_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)

        # We ignore any extra parameters in the request_uri if the
        # submitted URL doesn't have any either. This lets the function
        # work with things like ?order=asc
        # the behavior can be disabled with check_parameters: true
        request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
        request_uri = URI::RFC2396_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)

        if %r{^\w+://}.match?(url_string)

Review comment (suggestion):
I don't think you can do `||=` here. it works for `check_parameters` because it defaults to `false`, bit `method` defaults to a truthy value.

Code after:
      #
      #   current_page?(controller: 'product', action: 'index')
      #   # => false
      #
      #   current_page?(controller: 'product', action: 'create')
      #   # => false
      #
      #   current_page?(controller: 'product', action: 'create', method: :post)
      #   # => true
      #
      #   current_page?(controller: 'product', action: 'index', method: [:get, :post])
      #   # => true
      #
      # We can also pass in the symbol arguments instead of strings.
      #
      def current_page?(options = nil, check_parameters: false, method: :get, **options_as_kwargs)
        unless request
          raise "You cannot use helpers that need to determine the current " \
                "page unless your view context provides a Request object " \
                "in a #request method"
        end

        if options.is_a?(Hash)
          check_parameters = options.delete(:check_parameters) { check_parameters }
          method = options.delete(:method) { method }
        else
          options ||= options_as_kwargs
        end

        method_matches = case method
        when :get
          request.get? || request.head?
        when Array
          method.include?(request.method_symbol) || (method.include?(:get) && request.head?)
        else
          method == request.method_symbol
        end
        return false unless method_matches

        url_string = URI::RFC2396_PARSER.unescape(url_for(options)).force_encoding(Encoding::BINARY)

        # We ignore any extra parameters in the request_uri if the
        # submitted URL doesn't have any either. This lets the function
        # work with things like ?order=asc
        # the behavior can be disabled with check_parameters: true
        request_uri = url_string.index("?") || check_parameters ? request.fullpath : request.path
        request_uri = URI::RFC2396_PARSER.unescape(request_uri).force_encoding(Encoding::BINARY)

        if %r{^\w+://}.match?(url_string)
          request_uri = +"#{request.protocol}#{request.host_with_port}#{request_uri}"
        end