Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With Rails, where should I put html snippets? I don't want partials but I want them reloaded during development

Being lazy (and liking DRY code), I'm the kind of guy who's going to write a few little wrappers for recurring HTML markup. Those provided by Rails are good already, but sometimes I have something a little more specific that I know I'm going to repeat over and over.

In some situations a partial can be the solution, but sometimes I'm just going to call the snippet way too often to justify the overhead of using partials.

Right now I create a helpers/html_helper.rb file and stick them in there. The problem is that helpers are not reloaded dynamically per request during development. So each time I tweak my snippet or the code around it, I have to kill the server and restart it.

Granted, it's just a 5 seconds process, but I love Rails' convenience of just developing and then refreshing the browser. So I'd love to have that for my markup snippets as well.

Note: Just sticking 'unloadable' inside the helper module doesn't work.

like image 781
webmat Avatar asked Oct 02 '08 14:10

webmat


1 Answers

Good question! This is a technique I should abuse more frequently.

    #I go in environment.db (presumably it will work in one of the per-environment files, too.)
    Dependencies.explicitly_unloadable_constants << 'NameOfHelperToReloadHere'

That array starts out empty, incidentally, at least in my install. (Checked via console.)

I tested this locally and it works for me, at least on Rails 2.0.2. Major credit for the solution belongs to this gentleman.

like image 108
Patrick McKenzie Avatar answered Oct 06 '22 21:10

Patrick McKenzie