簡體   English   中英

如何將 redmine 插件升級到 rails 5,現在不推薦使用 alias_method_chain

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

故事模式

剛開始學習 RoR,但在短時間內我需要將類似於從 LDAP(不兼容版本)加載圖像的功能添加到我們的項目中。 項目被放棄,我找不到任何相關信息/文檔,所以我在這里尋求幫助。 解決方案、教程,任何東西都可以。

錯誤日志

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

需要更新的猴子補丁

插件\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

我的嘗試/文章

我在這里找到了很好的文章How To Replace alias_method_chain ,但我不太確定如何將prepend樣式應用於 redmine 插件的猴子補丁。 只是無法讓它工作:/

這與這個插件有關嗎?

如果是這樣,我會這樣做:

  • init.rb文件中,更改此內容:
RedmineApp::Application.config.after_initialize do
  ApplicationHelper.send(:include, GemAvatarPlugin::ApplicationAvatarPatch)
end

對此:

RedmineApp::Application.config.after_initialize do
  ApplicationHelper.prepend(GemAvatarPlugin::ApplicationAvatarPatch)
end
  • 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

對此:

module GemAvatarPlugin
  module ApplicationAvatarPatch

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

  end
end

我會刪除require 'application_helper'因為我不明白為什么需要它

我也嘗試更新這個插件。 我以https://github.com/alexandermeindl/redmine_local_avatars為例。

在 init.rb 中改變了這個:

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

對此:

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

打補丁的 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

我只在 Redmine 4.0.3 上嘗試過,但它似乎可以正常工作。 但是網絡服務器日志中有警告:

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

在 redmine_local_avatars 插件中有一個針對 Redmine 4.1 的不同補丁。

更新:我設置了一個包含更改的 github 存儲庫: https ://github.com/pentekl/redmine_gemavatar

您可以使用alias_method而不是alias_method_chain ,但我正在尋找類似prepend解決方案的東西

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

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:正如ste26054 在他的回答中提到並在此處評論的那樣,可以刪除require 'application_helper'以防止警告,因為它已經包含在核心中。

暫無
暫無

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

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