2013-06-20 69 views
1

我想擴展我的導航應用中定義的模型調用配置。出於某種架構原因,如果我可以將它擴展到寶石中,那將是非常棒的。在gem中擴展導軌模型

但在我的寶石foo.rb文件,如果我把我的配置類:

Configuration.class_eval do ... end 

這回我這個錯誤:

configuration: undefined method 'config' for nil:NilClass (NoMethodError)

如果我想這:

class Configuration 
    TEST = [:foo, :foo2].freeze 
end 

我無法訪問我的rails應用程序中定義的activerecord類。

有沒有辦法重載gem中的某些rails類?

編輯: 事情是這樣的工作:)

module m 
    module ConfigurationExtension 

    extend ActiveSupport::Concern 

    included do 
     CONSTAZ = [:foo].freeze 
    end 

    module ClassMethods 
     def foo1 
     "foo1" 
     end 
    end 

    module InstanceMethods 
     def foo2 
     "foo2" 
     end 
    end 
    end 
end 

require 'm/mailtie.rb' if defined?(Rails) 

在我railtie文件 模數m 類mRailtie < ::滑軌:: Railtie config.after_initialize做 :: Configuration.send(:包括,ConfigurationExtension) 結束 結束 結束

回答

1

這是非常錯誤的假設你在哪裏使用你的寶石將是具體的課程。相反,你應該創建一個寶石模塊,並在你想要的模型中包含/擴展/關注它(Configuration在這種情況下)