简体   繁体   English

如何将 redmine 插件升级到 rails 5,现在不推荐使用 alias_method_chain

[英]How to upgrade redmine plugin to rails 5, alias_method_chain is deprecated now

Story mode故事模式

Just started learning RoR, but in short period of time I need to add functionality similar to Loading images from LDAP (incompatible version) into our project.刚开始学习 RoR,但在短时间内我需要将类似于从 LDAP(不兼容版本)加载图像的功能添加到我们的项目中。 Project is abondoned, and I can't find any related info/docs, so I'm asking for help here.项目被放弃,我找不到任何相关信息/文档,所以我在这里寻求帮助。 Solution, tutorial, anything could work.解决方案、教程,任何东西都可以。

Error log错误日志

$ ruby bin/rake redmine:plugins RAILS_ENV="production"
rake aborted!
NoMethodError: undefined method `alias_method_chain' for ApplicationHelper:Module
Did you mean?  alias_method
...

Monkey patch that needs update需要更新的猴子补丁

plugins\redmine_gemavatar\lib\application_helper_gemavatar_patch.rb :插件\redmine_gemavatar\lib\application_helper_gemavatar_patch.rb :

require 'application_helper'

module GemAvatarPlugin
    module ApplicationAvatarPatch
        def self.included(base)
            base.send(:include, InstanceMethods)
            base.class_eval do
                alias_method_chain :avatar, :gemavatar
            end
        end
        module InstanceMethods
            def avatar_with_gemavatar(user, options = { })
                if Setting.gravatar_enabled? && user.is_a?(User)
                    options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
                    options[:size] = "64" unless options[:size]
                    avatar_url = url_for :controller => :pictures, :action => :delete, :user_id => user
                    return "<img class=\"gravatar\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{avatar_url}\" />".html_safe
                else
                    ''
                end
            end
        end
    end
end

My attempts / Articles我的尝试/文章

I've found good article here How To Replace alias_method_chain , but I'm not quite sure how to apply prepend style to redmine plugin's monkey patch.我在这里找到了很好的文章How To Replace alias_method_chain ,但我不太确定如何将prepend样式应用于 redmine 插件的猴子补丁。 Just can't get it work:/只是无法让它工作:/

Is this related to this plugin ?这与这个插件有关吗?

If so, here is how I would do it:如果是这样,我会这样做:

  • In the init.rb file, change this:init.rb文件中,更改此内容:
RedmineApp::Application.config.after_initialize do
  ApplicationHelper.send(:include, GemAvatarPlugin::ApplicationAvatarPatch)
end

To this:对此:

RedmineApp::Application.config.after_initialize do
  ApplicationHelper.prepend(GemAvatarPlugin::ApplicationAvatarPatch)
end
  • In lib/application_helper_gemavatar_patch.rb , change this:lib/application_helper_gemavatar_patch.rb中,更改此内容:
require 'application_helper'

module GemAvatarPlugin
  module ApplicationAvatarPatch

    def self.included(base)
      base.send(:include, InstanceMethods)
      base.class_eval do
        alias_method_chain :avatar, :gemavatar
      end
    end

    module InstanceMethods

      def avatar_with_gemavatar(user, options = { })
        # method content omitted for clarity
      end

    end
  end
end

to this:对此:

module GemAvatarPlugin
  module ApplicationAvatarPatch

    def avatar(user, options = { })
      # method content omitted for clarity
    end

  end
end

I would remove the require 'application_helper' because I don't see why it's needed我会删除require 'application_helper'因为我不明白为什么需要它

I also tried to update this plugin.我也尝试更新这个插件。 I used https://github.com/alexandermeindl/redmine_local_avatars as an example.我以https://github.com/alexandermeindl/redmine_local_avatars为例。

In init.rb changed this:在 init.rb 中改变了这个:

RedmineApp::Application.config.after_initialize do
  ApplicationHelper.send(:include, GemAvatarPlugin::ApplicationAvatarPatch)
end

to this:对此:

RedmineApp::Application.config.after_initialize do    
  ApplicationHelper.include ApplicationAvatarPatch
end

the patched lib/application_helper_gemavatar_patch.rb, looks like this:打补丁的 lib/application_helper_gemavatar_patch.rb 看起来像这样:

module ApplicationAvatarPatch    
def self.included(base)
    base.send(:include, InstanceMethods)

    base.class_eval do
        alias_method :avatar_without_gemavatar, :avatar
        alias_method :avatar, :avatar_with_gemavatar
    end
end

module InstanceMethods
    def avatar_with_gemavatar(user, options = { })
        if Setting.gravatar_enabled? && user.is_a?(User)
            options.merge!({:ssl => (defined?(request) && request.ssl?), :default => Setting.gravatar_default})
            options[:size] = "64" unless options[:size]
            avatar_url = url_for :controller => :pictures, :action => :delete, :user_id => user
            return "<img class=\"gravatar\" width=\"#{options[:size]}\" height=\"#{options[:size]}\" src=\"#{avatar_url}\" alt=\"Gemavatar text\" />".html_safe
        else
            avatar_without_gemavatar(user, options)
        end
    end
end
end

I tried it only with Redmine 4.0.3 but it seems to be working.我只在 Redmine 4.0.3 上尝试过,但它似乎可以正常工作。 However there are warnings in the webserver log:但是网络服务器日志中有警告:

127.0.0.1 - - [06/Mar/2020:20:58:23 CET] "GET /gemavatar/164 HTTP/1.1" 200 3545
http://tredmine1:3000/users/164 -> /gemavatar/164
[2020-03-06 20:58:23] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

In the redmine_local_avatars plugin there was a different patch for Redmine 4.1.在 redmine_local_avatars 插件中有一个针对 Redmine 4.1 的不同补丁。

Update: I set a up a github repository with the changes: https://github.com/pentekl/redmine_gemavatar更新:我设置了一个包含更改的 github 存储库: https ://github.com/pentekl/redmine_gemavatar

You can use alias_method instead of alias_method_chain , but I'm searching for something like prepend solution您可以使用alias_method而不是alias_method_chain ,但我正在寻找类似prepend解决方案的东西

                alias_method :avatar_without_gemavatar, :avatar
                alias_method :avatar, :avatar_with_gemavatar

UPD: But it throws warnings: UPD:但它会发出警告:

/app/helpers/application_helper.rb:180: warning: already initialized constant ApplicationHelper
::RECORD_LINK
/app/helpers/application_helper.rb:180: warning: previous definition of RECORD_LINK was here
/app/helpers/application_helper.rb:199: warning: already initialized constant ApplicationHelper
::ATTACHMENT_CONTAINER_LINK
/app/helpers/application_helper.rb:199: warning: previous definition of ATTACHMENT_CONTAINER_LI
NK was here
/app/helpers/application_helper.rb:1053: warning: already initialized constant ApplicationHelpe
r::LINKS_RE
/app/helpers/application_helper.rb:1053: warning: previous definition of LINKS_RE was here
Exiting

UPD: As ste26054 mentioned in his answer and commented here require 'application_helper' can be deleted to prevent warnings, since it already included in the core. UPD:正如ste26054 在他的回答中提到并在此处评论的那样,可以删除require 'application_helper'以防止警告,因为它已经包含在核心中。

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

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