简体   繁体   中英

(ruby on rails) how to override module method placed in lib/

I'm developing a web application in Rails 2.3.5. I defined the module in lib/ folder as follows.

module TestModule
  class Basic < ApplicationController
    def show
      p "module method"
    end
  end
end

and the load_paths are described in the environment.rb for this library as follows

Rails::Initializer.run do |config|
  config.load_paths += %W[ #{RAILS_ROOT}/lib/test_module ]
end

but temporally, I want to override the above method without editing the lib method. So, I put the override method in the "config/initializers/override_show.rb" as follows.

require_dependency "lib/test_module/basic.rb"
module TestModule
  class Basic
    def show
      p "new method"
    end
  end
end

without "require_dependency", I get an error, because the original method located in lib/ folder wasn't loaded, so I put the "require_dependency" before overriding the TestModule.

In the above code, the new method works fine only once just after activating the server. However, the new method is never called again, and the old method is called. When I restart the rails server, the new method will be called just once.

Please give me some advice on how to override the method in the lib folder. Thank you very much in advance.

您是否可能只是将config / initializers / override_show.rb中的代码附加到lib / test_module / basic.rb中?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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