As we upgrade our application’s Rails version, we often come across deprecation warnings.
For example:
In the above case, we remove these deprecation warnings by replacing update_attributes
to update
.
But, when developing new features after it, we might end up using deprecated update_attributes
again.
We might ignore these deprecation warnings and on next Rails upgrade we will
encounter new issues.
To avoid the above case, Rails has introduced
disallowed deprecations in ActiveSupport.
Once we have removed deprecations from codebase, we configure these deprecations
as disallowed
. If a disallowed deprecation is used, it will be treated as a failure
and raise an exception in development or test environment. In production, we log the
deprecations as error.
We can configure the disallowed_warnings
in our config
file as below:
Configuration rules as seen above are defined in an array. Array elements can be String, Symbol or Regexp. In each case we try to match the array element as a substring or regex match in our warning message.
Disallowed configuration can be set to raise exception
for all deprecation warning by using :all
symbol as below:
We can set the behavior for disallowed messages to either :raise
, :log
or
:notify
.
By default, it is set to :raise
in development and test environment and :log
in production environment.
We can temporarily re-allow disallowed deprecation using the .allow
method.
We can also pass an array of string, symbol or regular expression elements to this method.
The .allow
method call also be called conditionally as shown below