我想扩展refinerycms的pagescontroller以在我们的项目中使用一些apotomo小部件。

我可能会对PagesController进行“覆盖”,它将其复制到我的项目中,但我使用另一个引擎扩展页面控制器(使用模块/猴子修补方法修改显示和家庭方法)i'd虽然避免这种情况。

我的初步方法是这样的:

在config / 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
. 不幸的是,这在Apotomo的Controller_Methods中的“Helper ActionViewMethods”线上吹来。添加包括Apotomo :: Rails :: ActionViewMethods没有帮助。

我假设我只是获得有关Rails依赖管理的基本细节,也许是Ruby Open Classes错误。是否有另一种方法,或者简单的东西我忽略了?

有帮助吗?

解决方案 2

Here's the solution. Remove the before_initialize stuff; there's simply no need for this to be in a module. In application.rb, do:

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

Then, override refinery's shared/_content_page.html.erb to include:

<%=render_widget :map %>

Done and done.

What was wrong before? Well, calling ::PagesController.send :include, Refspike::Extensions means that I'm actually "almost" in the scope of the class I'm trying to modify, but not quite. So, reopening the class is unnecessary, for one thing. But an ActiveSupport method, class_inheritable_array being called by apotomo, apparently isn't discoverable in my module scope, either, so I can't get away with doing something like:

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

Fortunately, the 4 lines of code in the application.rb are a simpler solution, and that does the trick for me.

其他提示

Is PagesController a derived ActionController?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top