Note that there are some explanatory texts on larger screens.

plurals
  1. PORe-opening a class located in lib folder (Rails 3)
    primarykey
    data
    text
    <p>Similar questions have been asked many times. However, I have slightly different scenario and I still to find a clean solution to the following problem while developing a Rails 3 app:</p> <p>I have a ruby code located in a single file. This code is one module <code>MyModule</code> which contains a bunch of classes. I can't modify this code because it's auto-generated by an external tool. The file name is <code>my_module.rb</code> and it resides in the <code>lib</code> folder. This is the usual place for a code which doesn't belong to the main web app. I made sure it is auto-loaded once I access MyModule in one of my app controllers by modifying <code>config.autoload_paths</code> setting in <code>application.rb</code>.</p> <p>Now I need to re-open one of the classes in <code>MyModule</code>. I created a separate file <code>some_class.rb</code> where I put the following code:</p> <pre> class MyModule::SomeClass def some_new_method ... end end </pre> <p>My question is: what is the best place to put this code? And what should be the name of the file?</p> <p>The things I tried:</p> <ol> <li>Placing the code into <code>initializers</code> folder. it works fine in production environment. However, it breaks in <code>development</code> because the code in <code>initiliazers</code> folder is loaded once when the server starts up and <code>lib</code> is reloaded on each request. So, the first request in development works fine, however, the subsequent requests fail. Also I don't like placing the code into <code>initializers</code> because it's not really initialization piece but rather a part of business logic.</li> <li>Placing the code into <code>lib</code> folder also doesn't work. It's not loaded. Only <code>my_module.rb</code> is.</li> <li>Placing the code into <code>lib</code> or some other folder under <code>app</code> and then explicitly <code>requir</code>ing it in controllers. Again it works in production but fails in development for the same reason aforementioned in case #1.</li> <li>I switched off auto-loading, placed both <code>my_module.rb</code> and <code>some_class.rb</code> into <code>lib</code> folder and <code>require</code>ed them in an initializer. I think this is the best approach. The downside is that I still have to create a custom initializer and the code is not loaded lazily.</li> </ol> <p>UPDATE: 5. Thanks to raggi's suggestion, with auto-loading switched on, I put <code>require_dependency 'some_class'</code> in my controller. In this case <code>some_class.rb</code> is loaded on each request in development mode. This is my preferred approach . However, it still requires some sort of <code>require</code> :)</p> <p>Remember that files in <code>lib</code> folder are auto-loaded lazily. Rails checks the folder only when you use modules or classes that rails can't find. The file name must follow the naming convention, hence the name <code>my_module.rb</code>.</p> <p>I wonder if there is a solution when both files are auto-loaded lazily <strong>without</strong> any <code>require</code> statements.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload