我想用我的插件覆盖helper方法。我尝试创建一个新的辅助模块,其方法应该覆盖如下:
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
模块中。
答案 0 :(得分:3)
IssuesHelper.class_eval do
def render_custom_fields_rows(issus)
'it works!'.html_safe
end
end
答案 1 :(得分: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