0

I want to override the helper method with my plugin. I tried to create a new helper module with method that should override like this:

myplugin/app/helpers/issues_helper.rb

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

But this doesn't work. Core method still used in appropriate view.

Hack solution:

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

This is the hack because in normal way methods should be in InstanceMethods module of IssuesHelperPatch module.

freemanoid
  • 14,592
  • 6
  • 54
  • 77
  • `return` is implicit if your value is on the last line. Including it does tend to suggest it's there for a reason and can lead to confusion. – tadman May 31 '13 at 20:00
  • Are you sure this is the method being executed? When you say "doesn't work" you're not specific. – tadman May 31 '13 at 20:00
  • @tadman I just removed a lot of code to make example smaller so your suggestion doesn't matter. Thanks. – freemanoid May 31 '13 at 20:01
  • This method should be executed in issues/show view and it's result should be on page. So I tend to believe that this method being executed. – freemanoid May 31 '13 at 20:28

2 Answers2

3
IssuesHelper.class_eval do
  def render_custom_fields_rows(issus)
    'it works!'.html_safe
  end
end
usha
  • 28,973
  • 5
  • 72
  • 93
0

This is IMHO good solution for this problem:

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
freemanoid
  • 14,592
  • 6
  • 54
  • 77