วิธีอัปเกรดปลั๊กอิน redmine เป็น Rails 5 alias_method_chain เลิกใช้แล้ว

โหมดเนื้อเรื่อง

เพิ่งเริ่มเรียนรู้ 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

ความพยายามของฉัน / บทความ

ฉันพบบทความดีๆ ที่นี่ วิธีแทนที่ alias_method_chain แต่ฉันไม่ค่อยแน่ใจว่าจะใช้สไตล์ prepend กับแพทช์ลิงของปลั๊กอิน redmine ได้อย่างไร แค่ไม่สามารถทำงานได้ :/


person Norman Edance    schedule 26.04.2019    source แหล่งที่มา
comment
พบบทความอื่น Module.prepend: a super story ซึ่งอาจ ช่วยเหลือใครบางคน   -  person Norman Edance    schedule 26.04.2019


คำตอบ (3)


สิ่งนี้เกี่ยวข้องกับ ปลั๊กอินนี้ หรือไม่

ถ้าเป็นเช่นนั้นฉันจะทำอย่างไร:

  • ในไฟล์ 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' เพราะฉันไม่เข้าใจว่าทำไมจึงจำเป็น

person ste26054    schedule 02.05.2019
comment
ด้วยเหตุผลบางอย่างมันไม่ทำงาน โหลดเฉพาะภาพ Gravatar_default ดูเหมือนว่าจะไม่เคยเข้าสู่ Picture model และขอรูปภาพเป็น /people/avatar?id=1005 ในขณะที่มันควรจะเป็น smth เช่น /gemavatar/1005 - person Norman Edance; 03.05.2019
comment
อืม มันแปลก มันทำงานได้ดีสำหรับฉัน... คุณมีปลั๊กอินอื่นติดตั้งอยู่ เช่น redmine_people หรือไม่? - person ste26054; 03.05.2019
comment
นอกจากนี้ คุณจะได้อะไรหากคุณเปิด rails c จากไดเร็กทอรี redmine และเรียกใช้ ApplicationHelper.included_modules - person ste26054; 03.05.2019
comment
ใช่แล้ว ฉันใช้คน รวมไว้ที่นี่_โมดูล: [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] - person Norman Edance; 03.05.2019
comment
ตกลง ฉันได้ปิดการใช้งาน redmine_people โดยเติม .. ไว้หน้าชื่อโฟลเดอร์ :D มันยังไม่ทำอะไรเลย - person Norman Edance; 03.05.2019
comment
ต้องขอบคุณส่วนเสริม ทำให้มีการโหลดแพตช์ GemAvatarPlugin ก่อน ดังนั้นจึงควรทำงานได้อย่างถูกต้อง เมื่อคุณไปที่ /my/account คุณได้รับสิ่งนี้ในบันทึกของ Rails หรือไม่: Started GET "/gemavatar/:user_id" - person ste26054; 03.05.2019
comment
ไม่นะ มันเริ่มแล้ว "GET /people/avatar?id=1005&size=50x50 HTTP/1.1" 404 11160 http://localhost:3000/my/account -> /people/avatar?id=1005&size=50x50 - person Norman Edance; 03.05.2019
comment
อาจเกี่ยวข้อง: ในบันทึก Missing template, responding with 404: Missing template people/avatar, application/avatar with {:locale=>[:ru, :en], :formats=>[:html], : variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :axlsx, :rsb]}. - person Norman Edance; 03.05.2019
comment
ในการตั้งค่า/การแสดงผล Redmine คุณเปิดใช้งาน Use Gravatar user icons หรือไม่ (ดูเหมือนว่าจำเป็นต้องใช้โดยปลั๊กอิน) - person ste26054; 03.05.2019
comment
ใช่ฉันทำ. และจะแสดงเฉพาะรูปภาพ Gravatar เริ่มต้นเท่านั้น (ลักษณะการทำงานเหมือนกับไม่มีปลั๊กอิน) - person Norman Edance; 03.05.2019

ฉันพยายามอัปเดตปลั๊กอินนี้ด้วย ฉันใช้ 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

person nemBela    schedule 06.03.2020

คุณสามารถใช้ 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' สามารถลบได้ เพื่อป้องกันคำเตือนเนื่องจากรวมอยู่ในแกนหลักแล้ว

person Norman Edance    schedule 26.04.2019
comment
คุณไม่สามารถ require 'application_helper' จำเป็นขณะรันไทม์และคุณสามารถค้นหาได้ในโฟลเดอร์แอป /app rails คุณอาจพยายามขอไฟล์ผิด คุณสามารถแสดงเส้นทางการโหลดอัตโนมัติได้ด้วยการรัน echo $LOAD_PATH ในเทอร์มินัลของคุณและรับข้อมูลเพิ่มเติมเกี่ยวกับ วิธี require จาก Kernel โมดูลที่นี่ โปรดแบ่งปัน repository ของโครงการของคุณใน github และ URL สำหรับ plugins\redmine_gemavatar\lib\application_helper_gemavatar_patch.rb เพื่อให้ฉันสามารถโพสต์วิธีแก้ปัญหาของฉันได้ - person Fabrizio Bertoglio; 30.04.2019