2013-06-05 86 views
-2

我已經開發了兩個插件,他們都通過alias_method_chain導致成棧層次過深錯誤修改同一類的同方法。2 alias_method_chain - >堆棧層次過深

第一插件:

Rails.configuration.to_prepare do 
    require 'issue_patch' 
    Issue.send :include, IssuePatch 
end 

module IssuePatch 
    module InstanceMethods 
    def editable_custom_field_values_with_sort(user=nil) 
     editable_custom_field_values_without_sort(user).sort 
    end 
    end 

    def self.included(receiver) 
    receiver.send :include, InstanceMethods 
    receiver.class_eval do 
     alias_method_chain :editable_custom_field_values, :sort 
    end 
    end 
end 

二插件修改類相同,但具有不同的特徵:

Rails.configuration.to_prepare do 
    require 'issue_patch' 
    Issue.send     :include, IssuePatch 
end 

module IssuePatch 
    module InstanceMethods 
    def editable_custom_field_values_with_some_stuff(user=nil) 
     editable_custom_field_values_without_some_stuff(user).select { |c| c.have_stuff? } 
    end 
    end 
    def self.included(receiver) 
    receiver.send :include, InstanceMethods 
    receiver.class_eval do 
     alias_method_chain :editable_custom_field_values, :some_stuff 
    end 
    end 
end 

當我試圖調用這個方法我:

ActionView::Template::Error (stack level too deep): 
    plugins/my_plugin/lib/issue_patch.rb:8 

一個可能的,但黑客解決方案是簡單的猴子補丁的第一個插件功能的Redmine代碼,所以秒ond插件可以沒有任何錯誤地將其別名。
那麼我該如何解決這個錯誤?

+1

硬盤沒有看到你在做什麼更新 –

+0

@FrederickCheung問題的話。 – freemanoid

回答

0

的問題是:

我定義具有相同名稱IssuePatch兩個模塊,以便所述第一模塊將覆蓋第二但

Issue.send     :include, IssuePatch 

在兩個地方(對於每個插件)和相同的模塊仍然存在(哪一個覆蓋另一個並不重要),所以相同的模塊被包括2次,並且相同的alias_method_chain被調用2次。

解決辦法:我只需要添加單獨的模塊,爲每個插件,其中包括他們是這樣的:

Issue.send     :include, FirstPlugin::IssuePatch 
Issue.send     :include, SecondPlugin::IssuePatch