Skip to content

Deprecate mutating the default_url_options: - #63

Open
Edouard-chin wants to merge 1 commit into
mainfrom
ec-default-url-options
Open

Deprecate mutating the default_url_options:#63
Edouard-chin wants to merge 1 commit into
mainfrom
ec-default-url-options

Conversation

@Edouard-chin

Copy link
Copy Markdown
Member
  • Context

    This patch is about the default_url_options used to tweak the generated urls when calling for instance my_route_path inside controllers/views. There is another default_url_options at the RouteSet but this patch is not about this one.

    Problem

    Mutating the default_url_options is nowhere documented and the recommended way to customize generated urls is to override the default_url_options method and override it.

    The issue being that the Rails tests suite uses that pattern, and while I don't know whether this is also done in the wild, I picked the precautious path.

    Mutating the default_url_options can create super weird behaviour:

    class ApplicationController
      def hello_world
        redirect_to(root_path)
      end
    end
    
    class MyController < ApplicationController
      default_url_options[:lang] = "en"
    end

    In a autoloaded context, if a user hit the /hello_world path it will get redirected to /. Once the MyController is autoloaded , revisiting /hello_world will this time redirect to /?lang=en.

    Solution

    This patch freeze the default_url_options by default but still allows the hash to be mutated and trigger a deprecation message.

    This is done using a proxy which rescue from FrozenError anytime a mutation happens and duplicate the underlying hash.

- ### Context

  This patch is about the `default_url_options` used to tweak the generated
  urls when calling for instance `my_route_path` inside
  *controllers/views*. There is another `default_url_options` at the RouteSet
  but this patch is not about this one.

  ### Problem

  Mutating the `default_url_options` is nowhere documented and the
  recommended way to customize generated urls is to override the
  `default_url_options` method and override it.

  The issue being that the Rails tests suite uses that pattern, and
  while I don't know whether this is also done in the wild, I picked
  the precautious path.

  Mutating the `default_url_options` can create super weird behaviour:

  ```ruby
  class ApplicationController
    def hello_world
      redirect_to(root_path)
    end
  end

  class MyController < ApplicationController
    default_url_options[:lang] = "en"
  end
  ```

  In a autoloaded context, if a user hit the `/hello_world` path
  it will get redirected to `/`. Once the `MyController` is autoloaded
  , revisiting `/hello_world` will this time redirect to `/?lang=en`.

  ### Solution

  This patch freeze the default_url_options by default but still allows
  the hash to be mutated and trigger a deprecation message.

  This is done using a proxy which rescue from FrozenError anytime a
  mutation happens and duplicate the underlying hash.

  The `default_url_options` w
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant