gpt4 book ai didi

ruby-on-rails - 在开发模式下在 Ruby on Rails 上使用 alias_method_chain 自定义 Helper [REDMINE]

转载 作者:行者123 更新时间:2023-12-01 22:55:11 25 4
gpt4 key购买 nike

我想自定义方法link_to_issueapplication_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在开发模式下是 undefined 吗?

谢谢

最佳答案

尝试使用更常规的方式添加补丁,也许这会解决您的问题。

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

关于ruby-on-rails - 在开发模式下在 Ruby on Rails 上使用 alias_method_chain 自定义 Helper [REDMINE],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15243453/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com