简体   繁体   English

在已安装的导轨引擎中扩展模型的功能

[英]Extending the functionality of a model in a mounted rails engine

UPDATE 3: 更新3:

This solution seems ugly but I moved the decorator back to the decorators directory and then just added this to my application controller: 这个解决方案看起来很丑,但是我将装饰器移回了decorators目录,然后将其添加到我的应用程序控制器:

require './app/decorators/models/monologue/post_decorator.rb'
include ExtendPost
require './app/decorators/controllers/monologue/admin/posts_controller_decorator.rb'
include ExtendAdminPostsController

Thoughts for how to do this properly? 关于如何正确做到这一点的想法? Thanks. 谢谢。

But at least this works. 但至少这是有效的。

UPDATE 2: 更新2:

Well the following worked, but could someone please explain how to put this code in the "right" place and not where I stuck it: 以下工作,但有人可以解释如何将此代码放在“正确”的地方,而不是我坚持下去的地方:

In my application_controller.rb in my main_app, I now have: 在我的main_app中的application_controller.rb中,我现在有:

class ApplicationController < ActionController::Base 

end

Monologue::PostsRevision.class_eval do
   validates :title, :length => {:maximum => 25} 
end

This works properly, but is in a bad location from an organizational / code readability standpoint. 这可以正常工作,但从组织/代码可读性的角度来看,它位置不好。

UPDATE: 更新:

I also tried something different based on the SO answer here and ended up with this code, which also did not work: 我也根据这里的答案尝试了不同的东西,最后得到了这个代码,这也没有用:

In app/models/post_revision.rb (again, in main_app's app folder) 在app / models / post_revision.rb中(同样在main_app的app文件夹中)

require Monologue::PostRevisions::Engine.config.root + 'app' + 'models' + 'page'
class PostRevision
    validates :title, :length => {:maximum => 25} 
end

This did not work either. 这也不起作用。

When attempting to modify the functionality of a mounted engine in Rails 3 using decorators and the class_eval method, are the decorators supposed to go in the app folder in your main_app or in the app folder in your engine? 当尝试使用装饰器和class_eval方法修改Rails 3中挂载的引擎的功能时,装饰器是应该放在main_app中的app文件夹中还是放在引擎的app文件夹中?

I am attempting to modify some of the models in an engine called Monologue that I got here . 我试图修改某些型号的叫独白的引擎,我在这里 I found a good explanation of how to do that on the site of a different product (Refinery) here . 我在这里找到了一个很好的解释,说明如何在不同产品(炼油厂)的网站上做到这一点 And I also followed the instructions at this Rails Guide . 我也按照Rails指南中的说明进行操作。

So the code I ended up with was: 所以我最终得到的代码是:

In lib/monologue/engine.rb (is this supposed to be in the lib folder in the main app or the engine? I put it in the lib folder in my main_app): 在lib / monologue / engine.rb中(这应该是在主应用程序或引擎的lib文件夹中吗?我把它放在main_app的lib文件夹中):

module Monologue
    class Engine < ::Rails::Engine
        isolate_namespace Monologue

        config.to_prepare do
            Dir.glob(Rails.root + "app/decorators/**/*_decorator*.rb").each do |c|
                require_dependency(c)
            end
        end
   end
end  

And in app/decorators/monologue/posts_revisions_decorator.rb (and again, I put this in the app folder in the main_app, not the Monologue engine app folder): 在app / decorators / monologue / posts_revisions_decorator.rb中(再次,我把它放在main_app的app文件夹中,而不是Monologue引擎app文件夹中):

Monologue::PostsRevision.class_eval do
   validates :title, :length => {:maximum => 25} 
end

But this does not work. 但这不起作用。 The additional validation does nothing. 额外的验证没有任何作用。

If this is because it is supposed to go within the folders in the Monologue mounted engine and not the main_app, than I don't know how to do that since those files are not in my main_app directory. 如果这是因为它应该在Monologue挂载的引擎中而不是main_app中的文件夹中,那么我不知道如何做到这一点,因为这些文件不在我的main_app目录中。

Thoughts? 思考? Thanks. 谢谢。

I was able to overcome a similar challenge by loading my decorator via to_prepare in an initializer. 通过在初始化程序中通过to_prepare加载我的装饰器,我能够克服类似的挑战。 Try something like this: 尝试这样的事情:

# /config/initializers/my_engine.rb
Rails.application.config.to_prepare do
  require_dependency("./app/decorators/models/my_engine/my_model_decorator.rb")
end

另一种方法是activesupport-decorators gem。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM