The Rise of alias_method_chain
In The Include-and-Extend Trick, I showed you a snippet of code from an old version of Rails…minus a few interesting lines. Here is the same code again, with those lines now visible and marked with arrows:
| module ActiveRecord |
| module Validations |
| |
| def self.included(base) |
| base.extend ClassMethods |
» | base.class_eval do |
» | alias_method_chain :save, :validation |
» | alias_method_chain :save!, :validation |
» | end |
| |
| # ... |
| |
| end |
When ActiveRecord::Base includes the Validations module, the marked lines reopen Base and call a method named alias_method_chain. Let me show you a quick example to explain what alias_method_chain does.
The Reason ...
Get Metaprogramming Ruby 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.