How can I keep my initializer configuration from being lost in development mode?

核能气质少年 提交于 2019-12-03 22:36:25

You can add as many to_prepare blocks as you want - when you do config.to_prepare, Rails is doing (in configuration.rb in railties)

def to_prepare(&blk)
  to_prepare_blocks << blk if blk
end

and then iterates over those blocks handing them over to ActionDispatch::Reloader, where to_prepare is implemented using ActiveSupport::Callbacks (i.e. the same thing that is used for before_save and so on). Multiple to_prepare blocks are fine.

Currently it looks like Rails iterates over to_prepare_blocks after reading application initialisers so adding to Rails.application.configuration.to_prepare should work. You may prefer to use ActionDispatch::Reloader.to_prepare.

There's nothing to stop you from doing initializer code in a file that lives in app/models.

for example

class MyClass
  def self.run_me_when_the_class_is_loaded
  end
end

MyClass.run_me_when_the_class_is_loaded

MyClass.run_me... will run when the class is loaded .... which is what we want, right?

Not sure if its the Rails way.... but its extremely straightforward, and does not depend on the shifting winds of Rails.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!