2011-05-18 27 views
2

我想擴展RefineryCMS的PagesController以在我們的項目中使用一些apotomo小部件。將apotomo與RefineryCMS集成頁面

我可能會對PagesController進行「覆蓋」,將其複製到我的項目中,但我使用了另一個引擎來擴展PagesController(使用模塊/猴子修補方法修改show和home方法)I '寧可避免這一點。

我最初的做法是這樣的:在配置/ application.rb中

config.before_initialize do 
    require 'pages_controller_extensions' 
end 

config.to_prepare do 
    PagesController.send :include, Refspike::Extensions 
end 

在pages_controller_extensions:

module Refspike 
    module Extensions 
    class << PagesController 
     include Apotomo::Rails::ControllerMethods 
     has_widgets do |root| 
     root << widget(:map) 
     end 
    end 
    end 
end 

很不幸,這打擊了就行了「幫手ActionViewMethods 「在apotomo的controller_methods。添加包括Apotomo :: Rails :: ActionViewMethods沒有幫助。

我認爲我只是獲得有關rails依賴管理的基本細節,或者ruby open classes錯誤。有沒有其他方法,或者我忽略了一些簡單的東西?

+0

具體的錯誤信息是'block in ':未定義的方法'helper'爲#(NoMethodError) ,FWIW。 – JasonTrue 2011-05-19 00:43:38

回答

0

下面是解決方案。刪除before_initialize的東西;這根本就不需要在模塊中。在application.rb中,這樣做:

config.to_prepare do 
    ::PagesController.send :include, Apotomo::Rails::ControllerMethods 
    ::PagesController.has_widgets do |root| 
    root << widget(:map) 
    end 
end 

然後,覆蓋煉油廠的共享/ _content_page.html.erb包括:

<%=render_widget :map %> 

完成和完成的。

之前出了什麼問題?那麼,打電話::PagesController.send :include, Refspike::Extensions意味着我實際上「差不多」在我試圖修改的類的範圍內,但並不完全。所以,重新開課是不必要的,一方面。但是,一個方法的ActiveSupport,class_inheritable_array被稱爲apotomo,顯然不是我的模塊範圍內發現的,要麼,所以我不能做這樣的事情脫身:

#doesn't work 
module Refspike 
    module Extensions 
    include Apotomo::Rails::ControllerMethods 
    has_widgets do |root| 
     root << widget(:map) 
    end 
    end 
end 

幸運的是,4行代碼application.rb是一個更簡單的解決方案,並且這對我來說是個訣竅。

0

PagesController是派生的ActionController嗎?

+0

是的。 (儘管您可能想將它重新命名爲註釋而不是答案:P) – JasonTrue 2011-05-19 14:32:14

+0

更具體地說,它是從ApplicationController派生而來的,派生自ActionController :: Base。 https://github.com/resolve/refinerycms/blob/master/pages/app/controllers/pages_controller.rb – JasonTrue 2011-05-19 16:44:49