3

故事模式

刚开始学习 RoR,但是在短时间内我需要在我们的项目中添加类似于Loading images from LDAP (incompatible version) 的功能。项目被放弃了,我找不到任何相关的信息/文档,所以我在这里寻求帮助。解决方案,教程,任何东西都可以工作。

错误日志

$ 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 插件的猴子补丁。只是无法让它工作:/

4

3 回答 3

2

这与这个插件有关吗?

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

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

于 2019-05-02T14:01:17.953 回答
1

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

于 2020-03-06T20:18:51.383 回答
0

您可以使用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'的那样,可以删除以防止警告,因为它已经包含在核心中。

于 2019-04-26T12:37:01.017 回答