From d2ea36a4ca39386b51cd0c469113a171e64e99a7 Mon Sep 17 00:00:00 2001 From: Matt Brictson Date: Fri, 28 Apr 2017 11:41:38 -0700 Subject: [PATCH] Remove alias_method_chain This commit removes `alias_method_chain` and replaces it with somewhat of a hack. We cannot right out switch to `Module.prepend` without sacrificing Ruby 1.9 compatibility and rewriting our specs. This ensures we can support Rails 5.1, which no longer has `alias_method_chain` at all. --- CHANGELOG.md | 1 + lib/xray-rails.rb | 1 + lib/xray/aliasing.rb | 38 ++++++++++++++++++++++++++++++++++++++ lib/xray/engine.rb | 4 +++- 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 lib/xray/aliasing.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index abe5f18..70b3c9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ lesser changes or bug fixes. ## [Unreleased][] * Your contribution here! +* Remove `alias_method_chain` and make xray-rails compatible with Rails 5.1. ## [0.2.0][] (2016-09-23) diff --git a/lib/xray-rails.rb b/lib/xray-rails.rb index 91ec5f6..50aabe7 100644 --- a/lib/xray-rails.rb +++ b/lib/xray-rails.rb @@ -1,6 +1,7 @@ require "json" require "active_support/all" require_relative "xray/version" +require_relative "xray/aliasing" require_relative "xray/config" require_relative "xray/middleware" diff --git a/lib/xray/aliasing.rb b/lib/xray/aliasing.rb new file mode 100644 index 0000000..2430130 --- /dev/null +++ b/lib/xray/aliasing.rb @@ -0,0 +1,38 @@ +module Xray + # This module implements the old ActiveSupport alias_method_chain feature + # with a new name, and without the deprecation warnings. In ActiveSupport 5+, + # this style of patching was deprecated in favor of Module.prepend. But + # Module.prepend is not present in Ruby 1.9, which we would still like to + # support. So we continue to use of alias_method_chain, albeit with a + # different name to avoid collisions. + # + # TODO: remove this and drop support for Ruby 1.9. + # + module Aliasing + # This code is copied and pasted from ActiveSupport, but with :xray + # hardcoded as the feature name, and with the deprecation warning removed. + def xray_method_alias(target) + feature = :xray + + # Strip out punctuation on predicates, bang or writer methods since + # e.g. target?_without_feature is not a valid method name. + aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1 + yield(aliased_target, punctuation) if block_given? + + with_method = "#{aliased_target}_with_#{feature}#{punctuation}" + without_method = "#{aliased_target}_without_#{feature}#{punctuation}" + + alias_method without_method, target + alias_method target, with_method + + case + when public_method_defined?(without_method) + public target + when protected_method_defined?(without_method) + protected target + when private_method_defined?(without_method) + private target + end + end + end +end diff --git a/lib/xray/engine.rb b/lib/xray/engine.rb index 4011eea..fdac4fe 100644 --- a/lib/xray/engine.rb +++ b/lib/xray/engine.rb @@ -19,6 +19,8 @@ class Engine < ::Rails::Engine # Monkey patch ActionView::Template to augment server-side templates # with filepath information. See `Xray.augment_template` for details. ActionView::Template.class_eval do + extend Xray::Aliasing + def render_with_xray(*args, &block) path = identifier view = args.first @@ -37,7 +39,7 @@ def render_with_xray(*args, &block) source end end - alias_method_chain :render, :xray + xray_method_alias :render end # Sprockets preprocessor interface which supports all versions of Sprockets.