【问题标题】:How to upgrade redmine plugin to rails 5, alias_method_chain is deprecated now如何将 redmine 插件升级到 rails 5,现在不推荐使用 alias_method_chain
【发布时间】:2019-04-26 12:12:06
【问题描述】:

故事模式

刚开始学习RoR,但短期内需要在项目中添加类似于Loading images from 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 插件的猴子补丁。只是无法让它工作:/

【问题讨论】:

标签: ruby ruby-on-rails-5 redmine redmine-plugins alias-method-chain


【解决方案1】:

这和this plugin有关吗?

如果是这样,我会这样做:

  • 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',因为我不明白为什么需要它

【讨论】:

  • 由于某些原因它不起作用。仅加载 gravatar_default 图像。似乎它永远不会进入图片模型并要求图片为/people/avatar?id=1005,而它应该像/gemavatar/1005
  • 嗯,这很奇怪,它对我来说工作正常...您是否安装了另一个插件,例如redmine_people
  • 另外,如果你从redmine 目录启动rails c 并运行ApplicationHelper.included_modules,你会得到什么?
  • 是的,我在使用人。继承人included_modules:[GemAvatarPlugin::ApplicationAvatarPatch, RedminePeople::Patches::ApplicationHelperPatch::InstanceMethods, RedminePeople::Patches::ApplicationHelperPatch, Redmine::Helpers::URL, Redmine::Hook::Helper, Redmine::Themes::Helper, Redmine::SudoMode::Helper, Redmine::Paginatio n::Helper, GravatarHelper::PublicMethods, Redmine::I18n, Redmine::WikiFormatting::Macros::Definitions]
  • 好的,我已经禁用 redmine_people,方法是在其文件夹名称前添加 .. :D。它仍然没有做任何事情。
【解决方案2】:

我也尝试更新这个插件。我以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

【讨论】:

    【解决方案3】:

    您可以使用alias_method 代替alias_method_chain,但我正在寻找类似prepend 的解决方案

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

    统一更新: 但它会引发警告:

    /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
    

    统一更新: 作为ste26054 mentioned in his answerand commented here require 'application_helper' 可以删除以防止出现警告,因为它已经包含在核心中。

    【讨论】:

    • 你不能require 'application_helper'。它在运行时是必需的,您可以在 /app rails app 文件夹中找到它。您可能试图要求错误的文件。您可以通过在终端中运行 echo $LOAD_PATH 来显示自动加载路径,并获取有关 method require of Kernel module here 的更多信息。请在github 上分享您项目的repositoryplugins\redmine_gemavatar\lib\application_helper_gemavatar_patch.rb 的网址,以便我可以发布我对这个问题的解决方案。
    猜你喜欢
    • 2019-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多