Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rails.application.deprecators #46049

Merged

Conversation

jonathanhefner
Copy link
Member

@jonathanhefner jonathanhefner commented Sep 15, 2022

This adds a Rails.application.deprecators method that returns a managed collection of deprecators.

Individual deprecators can be added and retrieved from the collection:

Rails.application.deprecators[:my_gem] = ActiveSupport::Deprecation.new("2.0", "MyGem")
Rails.application.deprecators[:other_gem] = ActiveSupport::Deprecation.new("3.0", "OtherGem")

And the collection's configuration methods affect all deprecators in the collection:

Rails.application.deprecators.debug = true

Rails.application.deprecators[:my_gem].debug
# => true
Rails.application.deprecators[:other_gem].debug
# => true

Additionally, all deprecators in the collection can be silenced for the duration of a given block:

Rails.application.deprecators.silence do
  Rails.application.deprecators[:my_gem].warn    # => silenced (no warning)
  Rails.application.deprecators[:other_gem].warn # => silenced (no warning)
end


#
def [](name)
# TODO? automatically instantiate a deprecator when it does not exist?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, but I'd not do it now and only do it later if it makes sense for the framework's own usage.


#
def []=(name, deprecator)
# TODO? save configuration from setters (e.g. `#silenced=`) and apply
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense.

railties/lib/rails/application.rb Show resolved Hide resolved
@jonathanhefner jonathanhefner force-pushed the add-rails_application_deprecators branch 4 times, most recently from 01add7e to c4e99ae Compare September 26, 2022 21:44
@jonathanhefner jonathanhefner marked this pull request as ready for review September 26, 2022 22:03
@jonathanhefner jonathanhefner force-pushed the add-rails_application_deprecators branch from f3b53c7 to e9ba80e Compare October 5, 2022 19:26
This adds a `Rails.application.deprecators` method that returns a
managed collection of deprecators.

Individual deprecators can be added and retrieved from the collection:

  ```ruby
  Rails.application.deprecators[:my_gem] = ActiveSupport::Deprecation.new("2.0", "MyGem")
  Rails.application.deprecators[:other_gem] = ActiveSupport::Deprecation.new("3.0", "OtherGem")
  ```

And the collection's configuration methods affect all deprecators in the
collection:

  ```ruby
  Rails.application.deprecators.debug = true

  Rails.application.deprecators[:my_gem].debug
  # => true
  Rails.application.deprecators[:other_gem].debug
  # => true
  ```

Additionally, all deprecators in the collection can be silenced for the
duration of a given block:

  ```ruby
  Rails.application.deprecators.silence do
    Rails.application.deprecators[:my_gem].warn    # => silenced (no warning)
    Rails.application.deprecators[:other_gem].warn # => silenced (no warning)
  end
  ```
@jonathanhefner jonathanhefner force-pushed the add-rails_application_deprecators branch from e9ba80e to d240e8a Compare October 5, 2022 19:37
@jonathanhefner jonathanhefner merged commit 32aaf11 into rails:main Oct 5, 2022
@p8
Copy link
Member

p8 commented Oct 8, 2022

Hi @jonathanhefner.
I'm working on this weeks newsletter and want to include this PR.
Could you explain the use-case for this?
Is it for deprecating gem versions used by Rails?
So for example if we stop supporting dalli version 2, we add:
Rails.application.deprecators[:dalli] = ActiveSupport::Deprecation.new("2.0", "dalli")

@jonathanhefner
Copy link
Member Author

@p8 Close, but it would be Dalli registering Rails.application.deprecators[:dalli] and generating warnings using that deprecator instance.

Before this PR, if a library created its own deprecator instance, users of the library would have to seek out and configure that instance separately from the global ActiveSupport::Deprecation instance. For example, if a library had:

MyGem.deprecator = ActiveSupport::Deprecation.new("2.0", "my_gem")

and a user wanted to set behavior = :raise for all deprecators, they would have to write:

ActiveSupport::Deprecation.behavior = :raise
MyGem.deprecator.behavior = :raise

And so on for each library with its own deprecator instance.

With this PR, each library can have an initializer like:

module MyGem
  class Railtie < ::Rails::Railtie
    initializer :deprecator do |app|
      app.deprecators[:my_gem] = MyGem.deprecator
    end
  end
end

And then the user will only have to write:

Rails.application.deprecators.behavior = :raise

Alternatively, if the user wants to change the behavior for a single deprecator, they now have a standard location to do so:

Rails.application.deprecators[:my_gem].behavior = :raise

And all the above also applies to the debug, disallowed_behavior, and silenced settings, as well as silence blocks.

@p8
Copy link
Member

p8 commented Oct 9, 2022

Ok clear. Thanks @jonathanhefner !

jonathanhefner added a commit to jonathanhefner/rails that referenced this pull request Apr 11, 2023
Follow-up to rails#46049 and rails#47354.

Since `Rails.deprecator` is now a new `ActiveSupport::Deprecation`
instance instead of `ActiveSupport::Deprecation.instance`, it makes
sense to register it as `Rails.application.deprecators[:railties]`
instead of `Rails.application.deprecators[:rails]`.
danielvdao pushed a commit to danielvdao/rails that referenced this pull request May 1, 2023
Follow-up to rails#46049 and rails#47354.

Since `Rails.deprecator` is now a new `ActiveSupport::Deprecation`
instance instead of `ActiveSupport::Deprecation.instance`, it makes
sense to register it as `Rails.application.deprecators[:railties]`
instead of `Rails.application.deprecators[:rails]`.
danielvdao pushed a commit to danielvdao/rails that referenced this pull request May 1, 2023
Follow-up to rails#46049 and rails#47354.

Since `Rails.deprecator` is now a new `ActiveSupport::Deprecation`
instance instead of `ActiveSupport::Deprecation.instance`, it makes
sense to register it as `Rails.application.deprecators[:railties]`
instead of `Rails.application.deprecators[:rails]`.
danielvdao pushed a commit to danielvdao/rails that referenced this pull request May 1, 2023
Follow-up to rails#46049 and rails#47354.

Since `Rails.deprecator` is now a new `ActiveSupport::Deprecation`
instance instead of `ActiveSupport::Deprecation.instance`, it makes
sense to register it as `Rails.application.deprecators[:railties]`
instead of `Rails.application.deprecators[:rails]`.
zzak referenced this pull request in carrierwaveuploader/carrierwave Jul 2, 2024
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.

None yet

3 participants