How to monkey-patch code that gets auto-loaded in Rails?

前端 未结 6 1327
悲&欢浪女
悲&欢浪女 2020-12-14 18:39

I\'m monkey-patching a Rails engine with something like:

SomeClass.class_eval do
  # ...
end

The first time I hit the web site, on developm

6条回答
  •  醉梦人生
    2020-12-14 19:12

    EDIT: This solution only works for Rails 3+ since it's dependent on some functionality in Rails::Railtie. Put this code in an initializer.

    This question is quite old, but here's a solution I found:

    Rails.configuration.to_prepare do
      SomeClass.class_eval do
        # ...
      end
    end
    

    This forces Rails to reload the class on every request in development mode, but only once in production.

提交回复
热议问题