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

Replace alias_method_chain with alias_method to be compatible with Rails >= 5.1 #1

Merged
merged 1 commit into from
Oct 25, 2021

Conversation

thomasjlee
Copy link

@thomasjlee thomasjlee commented Oct 23, 2021

alias_method_chain has been deprecated and removed in rails 5.1
(see: rails/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.

This article was also helpful: https://littlelines.com/blog/2018/01/31/replace-alias-method-chain

…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.
Copy link

@markhilkert markhilkert left a comment

Choose a reason for hiding this comment

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

@thomasjlee thomasjlee merged commit adc188a into master Oct 25, 2021
@thomasjlee thomasjlee deleted the replace_deprecated_alias_method_chain branch October 25, 2021 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants