0

我想用我的插件覆盖辅助方法。我试图创建一个新的辅助模块,其方法应该像这样覆盖:

myplugin/app/helpers/issues_helper.rb

module IssuesHelper
  def render_custom_fields_rows(issus)
    'it works!'.html_safe
  end
end

但这不起作用。核心方法仍然在适当的视图中使用。

破解解决方案:

issues_helper_patch.rb

module IssuesHelperPatch
  def self.included(receiver)
    receiver.send :include, InstanceMethods

    receiver.class_eval do
      def render_custom_fields_rows(issue)
        "It works".html_safe
      end
    end
  end
end

init.rb

Rails.configuration.to_prepare do
  require 'issues_helper_patch'
  IssuesHelper.send     :include, IssuesHelperPatch
end

这是 hack,因为在正常情况下,方法应该在InstanceMethods模块的IssuesHelperPatch模块中。

4

2 回答 2

3
IssuesHelper.class_eval do
  def render_custom_fields_rows(issus)
    'it works!'.html_safe
  end
end
于 2013-05-31T21:07:54.870 回答
0

恕我直言,这是解决此问题的好方法:

issues_helper_patch.rb
module IssuesHelperPatch
  module InstanceMethods
    def render_custom_fields_rows_with_message(issue)
      "It works".html_safe
    end
  end

  def self.included(receiver)
    receiver.send :include, InstanceMethods

    receiver.class_eval do
      alias_method_chain :render_custom_fields_rows, :message
    end
  end
end

init.rb

Rails.configuration.to_prepare do
  require 'issues_helper_patch'
  IssuesHelper.send     :include, IssuesHelperPatch
end
于 2013-06-02T18:39:18.587 回答