简体   繁体   English

Rails:扩展alias_method_chain

[英]Rails: extending alias_method_chain

There is a Rails 4.2.6 application that has a core plugin extending ActiveRecord reload method via alias_method_chain . 有一个Rails 4.2.6应用程序,该应用程序具有一个核心插件,可以通过alias_method_chain扩展ActiveRecord reload方法。
I need to write another plugin extending the same base functionality and keeping what was added by the core plugin. 我需要编写另一个扩展相同基本功能的插件,并保留核心插件添加的内容。
As far as I get it, I can call alias_method_chain only once but I wish to get something like this: 就我所知,我只能调用alias_method_chain一次,但我希望得到这样的东西:

class Klass
  def foo
end

core_plugin:
  base.send :alias_method_chain, :foo, :bar

my_plugin:
  base.send :alias_method_chain, :foo, :baz

Where a call to Klass.foo results in calls to :baz , :bar and :foo 调用Klass.foo会导致调用:baz:bar:foo

So what are my options assuming I cannot edit core_plugin? 那么,假设我无法编辑core_plugin,我有哪些选择?

You need something like this 你需要这样的东西

class Klass 克拉斯班

def foo
    puts "foo"
    end
def baz
    puts "printing baz..."
bar
    puts "...printing bar"
  end

  alias_method :bar, :foo
  alias_method :foo, :baz
end

Klass.new.foo

prints 版画

printing baz... foo ...printing bar 打印baz ... foo ... print bar

Is this ur expectation? 这是您的期望吗? If not pls explain 如果没有请解释

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM