如何通过重新加载开发环境在另一个可安装引擎中扩展可安装引擎的模型 [英] How to extend a mountable engine's model inside another mountable engine with development environment reloading

查看:21
本文介绍了如何通过重新加载开发环境在另一个可安装引擎中扩展可安装引擎的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Rails 3.2.2 和 Ruby 1.9.2.

Using Rails 3.2.2 and Ruby 1.9.2.

我有一个 Rails 可安装引擎 EngineA,它声明了一个继承形式 ActiveRecord::BaseUser 类.我有另一个引擎 EngineB 想要将功能注入 EngineA::User.现在我所做的如下所示:

I have a rails mountable engine EngineA that declares a User class inheriting form ActiveRecord::Base. I have another engine EngineB that wants to inject functionality into EngineA::User. Right now what I have done is shown below:

方法一:

#EngineA app/models/engine_a/user.rb
module EngineA
  class User < ActiveRecord::Base
    has_attached_file :avatar
    has_many :somethings
  end
end

#EngineB lib/engine_b/user.rb
module EngineB
  module User
    def self.extended obj
      obj.class_eval do
        has_many :something_elses
      end
    end
  end
end

EngineA::User.extend EngineB::User

这给了我一个 uninitialized constant EngineA::User 错误.即使我需要那个特定文件,我也会遇到 EngineA 需要回形针的问题,以便理解 has_attached_file.当我意识到我必须知道并需要 EngineB 内部的 EngineA 依赖项时,这条路就结束了.

This gives me an uninitialized constant EngineA::User error. Even when I require that specific file I run into the problem of EngineA needing paperclip so that has_attached_file is understood. That road ended when I realized I would have to know and require the dependencies for EngineA inside EngineB.

方法二:

我使用了与之前相同的代码,只是我从 EngineB user.rb 文件中删除了最后一行 EngineA::User.extend EngineB::User.然后我将该调用移动到 EngineB 中的初始化程序.

I used the same code as before except I removed the last line EngineA::User.extend EngineB::User from the EngineB user.rb file. I then moved that call to an initializer inside EngineB.

#EngineB config/initializers/my_mixin.rb
EngineA::User.extend EngineB::User

这非常有效!!!除非在开发模式下我会更改代码并且模型会刷新.唯一刷新的是 EngineA::User 而不是我作为初始值设定项的 mixin.因此,一旦我更改了代码,我就失去了所有扩展功能.

This worked perfectly!!! Except in development mode when I would change code and the models would refresh. The only thing that was refreshed was the EngineA::User and not the mixin that I had put as an initializer. So once I changed code, I lost all of my extended functionality.

我什至不认为这是最有效"的方法……任何帮助将不胜感激.提前致谢.

I'm not even positive this is the most 'efficient' way to do this... any help would be greatly appreciated. Thanks in advance.

推荐答案

根据配置文档,您可以使用 ActionDispatch 回调来加载项目.如果 cache_classes 设置为 false,这些回调将在每次请求时运行,就像在开发模式下一样.

According to the configuration documentation, you can use an ActionDispatch callback to load items. These callbacks will run when at every request if cache_classes is set to false, like in development mode.

在您的 EngineB.rb 文件中,您可以尝试这样的操作:

Inside of your EngineB.rb file, you might try something like this:

if Rails.env.development?
    ActionDispatch::Callbacks.to_prepare do
        load "#{File.expand_path(File.dirname(__FILE__))}/../config/initializers/my_mixin.rb"
    end
end

这篇关于如何通过重新加载开发环境在另一个可安装引擎中扩展可安装引擎的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆