【问题标题】:Custom Helper with alias_method_chain on Ruby on Rails in development mode [REDMINE]在开发模式下在 Ruby on Rails 上使用 alias_method_chain 的自定义助手 [REDMINE]
【发布时间】:2013-02-21 00:20:18
【问题描述】:

我想用 alias_method_chain 的原理自定义 Redmine 的 application_helper 的方法 link_to_issue 以保持插件中的 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方法在开发模式下是未定义的?

谢谢

【问题讨论】:

  • 奇怪!解决方案看起来正确。

标签: ruby-on-rails development-environment redmine helper alias-method-chain


【解决方案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

【讨论】:

  • 我尝试了您的解决方案,但似乎无法解决我的问题。感谢您的帮助。
  • @Rem's 你能解决这个问题吗?
猜你喜欢
  • 2018-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-03
  • 2011-07-13
  • 1970-01-01
  • 2012-08-29
  • 2013-11-06
相关资源
最近更新 更多