Пользовательский помощник с alias_method_chain на Ruby on Rails в режиме разработки [REDMINE]

Я хотел бы настроить метод link_to_issue из application_helper Redmine по принципу alias_method_chain, чтобы сохранить код Redmine в плагине чистым, но я столкнулся с проблемой.

Прежде всего, вот файл патча, application_helper_patch.rb:

   require_dependency 'application_helper'

    module ApplicationtHelperPatch
     def self.included(base) # :nodoc:     
      base.send(:include, InstanceMethods)
      base.class_eval do
        unloadable
        alias_method_chain :link_to_issue, :custom_show
      end
     end

     module InstanceMethods
         def link_to_issue_with_custom_show(issue, options={})
          title = nil
          subject = nil
          if options[:subject] == false
           title = truncate(issue.subject, :length => 60)
          else
            subject = issue.subject
            if options[:truncate]
              subject = truncate(subject, :length => options[:truncate])
            end
          end
          s = link_to "#{h subject}", {:controller => "issues", :action => "show", :id => issue},      
                                          :class => issue.css_classes,
                                          :title => title
          s = "#{h issue.project} - " + s if options[:project]
      end
     end
   end

И init.rb из плагина:

require 'redmine'
require 'application_helper_patch'

Dispatcher.to_prepare do
  ApplicationHelper.send(:include, ApplicationtHelperPatch)  unless ApplicationHelper.included_modules.include? ApplicationtHelperPatch
end

Redmine::Plugin.register :redmine_dt_capture do
  name 'my plugin'
  author 'Remi'
  description 'This is a plugin for Redmine'
  version '0.0.1'
  permission :dt, :public => true
  menu :top_menu,
    :dt,
    { :controller => 'my_controller', :action => 'index' },
    :caption => ' my_plugin '

  if RAILS_ENV == 'development'
      ActiveSupport::Dependencies.load_once_paths.reject!{|x| x =~ /^#{Regexp.escape(File.dirname(__FILE__))}/}
  end

Это решение отлично работает в производственном режиме, но не в режиме разработки. Когда я запускаю приложение, я сталкиваюсь с этой проблемой:

NoMethodError in Issues#show
Showing app/views/issues/show.html.erb where line #47 raised:
undefined method `call_hook' for #<ActionView::Base:0x6b8b750>
Extracted source (around line #47): 

Почему метод call_hook не определено в режиме разработки?

Спасибо

1 ответ

Попробуйте более традиционный способ добавить патч, возможно, это решит вашу проблему.

поместите ваш патч в your_plugin/lib/plugin_name/patches/

и application_helper_patch.rb станет таким

require_dependency 'application_helper'

module PluginName
module Patches
module ApplicationtHelperPatch
 def self.included(base) # :nodoc:     
  base.send(:include, InstanceMethods)
  base.class_eval do
    unloadable
    alias_method_chain :link_to_issue, :custom_show
  end
 end

 module InstanceMethods
     def link_to_issue_with_custom_show(issue, options={})
      title = nil
      subject = nil
      if options[:subject] == false
       title = truncate(issue.subject, :length => 60)
      else
        subject = issue.subject
        if options[:truncate]
          subject = truncate(subject, :length => options[:truncate])
        end
      end
      s = link_to "#{h subject}", {:controller => "issues", :action => "show", :id => issue},      
                                      :class => issue.css_classes,
                                      :title => title
      s = "#{h issue.project} - " + s if options[:project]
  end
 end
end
end
end

И init.rb плагина:

require 'redmine'
require 'application_helper_patch'

Dispatcher.to_prepare do
  ApplicationHelper.send(:include, PluginName::Patches::ApplicationtHelperPatch)  unless    ApplicationHelper.included_modules.include? PluginName::Patches::ApplicationtHelperPatch
end

Redmine::Plugin.register :redmine_dt_capture do
  name 'my plugin'
  author 'Remi'
  description 'This is a plugin for Redmine'
  version '0.0.1'
permission :dt, :public => true
 menu :top_menu,
  :dt,
  { :controller => 'my_controller', :action => 'index' },
  :caption => ' my_plugin '

if RAILS_ENV == 'development'
  ActiveSupport::Dependencies.load_once_paths.reject!{|x| x =~ /^#{Regexp.escape(File.dirname(__FILE__))}/}
end
Другие вопросы по тегам