簡體   English   中英

在開發模式下使用Ruby on Rails上的alias_method_chain自定義助手[REDMINE]

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

我想使用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方法在開發模式下是未定義的?

謝謝

嘗試更常規的方式添加補丁,可能會解決您的問題。

把你的補丁放在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM