Skip to content

Commit

Permalink
alias_method_chain has been deprecated and removed in rails 5.1 (see r…
Browse files Browse the repository at this point in the history
…ails/rails#27035)

Apparently, defining methods such as :a_with_b and :a_without_b used to be a
common pattern in rails, so alias_method_chain was introduced to encapsulate
this (see: https://apidock.com/rails/ActiveSupport/CoreExtensions/Module/alias_method_chain).

Here is an example of how this works and how we can replace
alias_method_chain with alias_method:

class DeprecatedPerson
  def greet
    "hi"
  end

  def greet_with_excitement
    "#{greet_without_excitement}!!!!!"
  end

  alias_method_chain :greet, :excitement
end

class Person
  def greet
    "hi"
  end

  def greet_with_excitement
    "#{greet_without_excitement}!!!!!"
  end

  alias_method :greet_without_excitement, :greet
  alias_method :greet, :greet_with_excitement
end

tracy = DeprecatedPerson.new
tracy.greet # => "hi!!!!!"
tracy.greet_with_excitement # => "hi!!!!!"
tracy.greet_without_excitement # => "hi"

june = Person.new
june.greet # => "hi!!!!!"
june.greet_with_excitement # => "hi!!!!!"
june.greet_without_excitement # => "hi"

It is necessary to replace alias_method_chain because we use this gem in the
inventables/fbolt repo, and it may take some time to rewrite fbolt such that
this gem is no longer be a dependency.

Meanwhile, we are encountering deprecation warnings which are quite noisy.

Before upgrading from rails 5.0 to 5.1, we will have to address each of the
dependencies that still use alias_method_chain.
  • Loading branch information
thomasjlee committed Oct 23, 2021
1 parent 28dd197 commit 1f69575
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/nested_has_many_through/reflection.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module NestedHasManyThrough
module Reflection # :nodoc:
def self.included(base)
base.send :alias_method_chain, :check_validity!, :nested_has_many_through
base.send :alias_method :check_validity_without_nested_has_many_through!, :check_validity!
base.send :alias_method :check_validity!, :check_validity_with_nested_has_many_through!
end

def check_validity_with_nested_has_many_through!
Expand Down

0 comments on commit 1f69575

Please sign in to comment.