简体   繁体   English

在开发模式下使用Ruby on Rails上的alias_method_chain自定义助手[REDMINE]

[英]Custom Helper with alias_method_chain on Ruby on Rails in development mode [REDMINE]

I would like to custom the method link_to_issue of the application_helper of Redmine with the principle of the alias_method_chain in order to keep the Redmine code clean in a plugin but I encounter a problem. 我想使用alias_method_chain的原则自定义Redmine的application_helper的方法link_to_issue ,以便在插件中保持Redmine代码清洁,但是我遇到了问题。

First of all, here is the patch file, application_helper_patch.rb : 首先,这是补丁文件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

And the init.rb of the plugin : 和插件的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

This solution runs perfectly in production mode, but no in development mode. 此解决方案在生产模式下完美运行,但在开发模式下不运行。 When I launch the application I encounter this problem : 当我启动应用程序时遇到此问题:

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): 

Why does the method call_hook is undefined in development mode ? 为什么call_hook方法在开发模式下是未定义的?

Thanks 谢谢

Try more conventional way to add patch, may be this will resolve your issue. 尝试更常规的方式添加补丁,可能会解决您的问题。

put your patch in your_plugin/lib/plugin_name/patches/ 把你的补丁放在your_plugin / lib / plugin_name / patches /

and application_helper_patch.rb will become like this 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

And the init.rb of the plugin : 和插件的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

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

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