如何让Rails停止重新加载我的宝石?

时间:2021-11-05 07:16:29

I have a gem:

我有一个宝石:

# in /Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib/my_gem.rb
module MyGem
  def do_stuff
    ..
  end
end

And I loaded it in Rails:

我在Rails中加载它:

# in [rails_root]/config/environment.rb:
config.gem 'my_gem', :version => '1.0.0'

And used it:

并用它:

# in [rails_root]/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  include MyGem
end

But I need to monkey-patch it a bit in an environment-specific way:

但我需要以特定于环境的方式对其进行修补:

# in [rails_root]/config/environments/development.rb:
MyGem.class_eval do
  def do_stuff
    raise 'Ack! - just testing'
  end
end

Unfortunately, MyGem reloads on every request, so my monkey-patching is useless.

不幸的是,MyGem会在每次请求时重新加载,所以我的猴子修补是没用的。

I've checked load_once_paths:

我检查了load_once_paths:

ActiveSupport::Dependencies.load_once_paths
# => ["/Library/Ruby/Gems/1.8/gems/my_gem-1.0.0/lib", "other stuff"]

Any idea how I can get the effect I want?

知道我怎么能得到我想要的效果吗?

2 个解决方案

#1


Seems you are working in development mode where Rails loads all classes in every request to help the developer to reflect the code changes + you have included the gem in your controller. To overcome this go to project_path/config/environments/development.rb and add this line

似乎你在开发模式下工作,其中Rails加载每个请求中的所有类,以帮助开发人员反映代码更改+你已经在控制器中包含了gem。要解决这个问题,请转到project_path / config / environments / development.rb并添加此行

config.cache_classes = true 

Notice that you won't have your code changes reflected unless you restart the server.

请注意,除非重新启动服务器,否则不会反映代码更改。

Update 1: Also as a second solution you can add the moneky patch after you you include the gem in that controller. You can add it to the bottom of your controller file.

更新1:另外作为第二种解决方案,您可以在将控制器中包含gem后添加moneky补丁。您可以将其添加到控制器文件的底部。

Update 2: A third solution (recommended) If you can add the following snippet to my_gem.rb

更新2:第三种解决方案(推荐)如果您可以将以下代码段添加到my_gem.rb

class ActionController::Base
  include MyGem
end

then things should work as you won't need to manually include it in your application controller.

事情应该工作,因为你不需要手动将它包含在你的应用程序控制器中。

#2


If you're including the gem in your environment.rb you shouldn't need to include it in your controller.

如果您在环境中包含gem.rb,则不需要将其包含在控制器中。

You might want to think about how the gem hooks into ActionController - it sounds like you want to add class methods to ActionController::Base, which is quite straightforward. Have a look at how many common gems implement this.

您可能想要考虑gem如何挂钩到ActionController中 - 听起来您想要将类方法添加到ActionController :: Base,这非常简单。看看有多少常见的宝石实现了这一点。

It sounds too, like you might even want to check for the existence & value of RAILS_ENV to ensure different behaviour for different environments.

这听起来也像你甚至想要检查RAILS_ENV的存在和价值,以确保不同环境的不同行为。

#1


Seems you are working in development mode where Rails loads all classes in every request to help the developer to reflect the code changes + you have included the gem in your controller. To overcome this go to project_path/config/environments/development.rb and add this line

似乎你在开发模式下工作,其中Rails加载每个请求中的所有类,以帮助开发人员反映代码更改+你已经在控制器中包含了gem。要解决这个问题,请转到project_path / config / environments / development.rb并添加此行

config.cache_classes = true 

Notice that you won't have your code changes reflected unless you restart the server.

请注意,除非重新启动服务器,否则不会反映代码更改。

Update 1: Also as a second solution you can add the moneky patch after you you include the gem in that controller. You can add it to the bottom of your controller file.

更新1:另外作为第二种解决方案,您可以在将控制器中包含gem后添加moneky补丁。您可以将其添加到控制器文件的底部。

Update 2: A third solution (recommended) If you can add the following snippet to my_gem.rb

更新2:第三种解决方案(推荐)如果您可以将以下代码段添加到my_gem.rb

class ActionController::Base
  include MyGem
end

then things should work as you won't need to manually include it in your application controller.

事情应该工作,因为你不需要手动将它包含在你的应用程序控制器中。

#2


If you're including the gem in your environment.rb you shouldn't need to include it in your controller.

如果您在环境中包含gem.rb,则不需要将其包含在控制器中。

You might want to think about how the gem hooks into ActionController - it sounds like you want to add class methods to ActionController::Base, which is quite straightforward. Have a look at how many common gems implement this.

您可能想要考虑gem如何挂钩到ActionController中 - 听起来您想要将类方法添加到ActionController :: Base,这非常简单。看看有多少常见的宝石实现了这一点。

It sounds too, like you might even want to check for the existence & value of RAILS_ENV to ensure different behaviour for different environments.

这听起来也像你甚至想要检查RAILS_ENV的存在和价值,以确保不同环境的不同行为。