Search code examples
ruby-on-railsrubyruby-on-rails-4deprecation-warningruby-on-rails-5.2

DEPRECATION WARNING does not show for module in rails 5.2 but works with class


While I was answering for this this question found

Rails 4.1.8 Ruby 2.2.0p0

module Fred
  extend self
  def aaa; end
  def bbb; end
  def ccc; end
  def ddd; end
  def eee; end
end

module Bar
  extend self
  def ccc; end
end

ActiveSupport::Deprecation.deprecate_methods(
  Fred, :aaa, bbb: :zzz, ccc: 'use Bar#ccc instead'
)

Fred.aaa

DEPRECATION WARNING: aaa is deprecated and will be removed
  from Rails 4.2. (called from \__pry__ at (pry):15)
#=> nil

Same code tried in rails 5.2.0 but no DEPRECATION WARNING.

So what i'm missing here, DEPRECATION WARNING has new update with rails 5.2.0 and will not warn for module?


Solution

  • FYI: Rails is open source, the code is publicly available here: https://github.com/rails/rails


    Rails5 is using Module#prepend.

    Rails4 is using alias_method_chain.

    Obviously, Module#prepend won’t work with module functions. You might do something like:

    ActiveSupport::Deprecation.deprecate_methods(
      Fred.singleton_class, :aaa
    )
    

    I did not test the code, but it should work. Also, I would consider this being a bug in Rails5.