Skip to content

Commit a3061e9

Browse files
committed
Document deprecators for library authors
1 parent c8eeac5 commit a3061e9

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

activesupport/CHANGELOG.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
* Deprecate usage of the singleton `ActiveSupport::Deprecation`.
2+
3+
All usage of `ActiveSupport::Deprecation` as a singleton is deprecated, the most common one being
4+
`ActiveSupport::Deprecation.warn`. Gem authors should now create their own deprecator (`ActiveSupport::Deprecation`
5+
object), and use it to emit deprecation warnings.
6+
7+
Calling any of the following without specifying a deprecator argument is also deprecated:
8+
* Module.deprecate
9+
* deprecate_constant
10+
* DeprecatedObjectProxy
11+
* DeprecatedInstanceVariableProxy
12+
* DeprecatedConstantProxy
13+
* deprecation-related test assertions
14+
15+
Use of `ActiveSupport::Deprecation.silence` and configuration methods like `behavior=`, `disallowed_behavior=`,
16+
`disallowed_warnings=` should now be aimed at the [application's deprecators](https://api.rubyonrails.org/classes/Rails/Application.html#method-i-deprecators).
17+
18+
```ruby
19+
Rails.application.deprecators.silence do
20+
# code that emits deprecation warnings
21+
end
22+
```
23+
24+
If your gem has a Railtie or Engine, it's encouraged to add your deprecator to the application's deprecators, that
25+
way the deprecation related configuration options will apply to it as well, e.g.
26+
`config.active_support.report_deprecations` set to `false` in the production environment will also disable your
27+
deprecator.
28+
29+
```ruby
30+
initializer "my_gem.deprecator" do |app|
31+
app.deprecators[:my_gem] = MyGem.deprecator
32+
end
33+
```
34+
35+
*Étienne Barrié*
36+
137
* Add `Object#with` to set and restore public attributes around a block
238

339
```ruby

activesupport/lib/active_support/deprecation.rb

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,33 @@
33
require "singleton"
44

55
module ActiveSupport
6-
# \Deprecation specifies the API used by Rails to deprecate methods, instance
7-
# variables, objects, and constants.
6+
# \Deprecation specifies the API used by Rails to deprecate methods, instance variables, objects, and constants. It's
7+
# also available for gems or applications.
8+
#
9+
# For a gem, use Deprecation.new to create a Deprecation object and store it in your module or class (in order for
10+
# users to be able to configure it).
11+
#
12+
# module MyLibrary
13+
# def self.deprecator
14+
# @deprecator ||= ActiveSupport::Deprecation.new("2.0", "MyLibrary")
15+
# end
16+
# end
17+
#
18+
# For a Railtie or Engine, you may also want to add it to the application's deprecators, so that the application's
19+
# configuration can be applied to it.
20+
#
21+
# module MyLibrary
22+
# class Railtie < Rails::Railtie
23+
# initializer "deprecator" do |app|
24+
# app.deprecators[:my_library] = MyLibrary.deprecator
25+
# end
26+
# end
27+
# end
28+
#
29+
# With the above initializer, configuration settings like the following will affect +MyLibrary.deprecator+:
30+
#
31+
# # in config/environments/test.rb
32+
# config.active_support.deprecation = :raise
833
class Deprecation
934
# active_support.rb sets an autoload for ActiveSupport::Deprecation.
1035
#

0 commit comments

Comments
 (0)