# How to dynamically load plugin files on every request?

**URL:** <https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510>\
**Category:** Rails\
**Created:** [July 3, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510 "2009-07-03T20:04:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 3, 2009, 8:04pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/1 "2009-07-03T20:04:27Z")

</div>

Hi,  
I have been developing a Rails plugin recently, and I have realised  
that for every change I make into files of my plugin, I have to  
restart the server to make changes take effect. While changes at  
application’s own file appear instantly.  
I’m writing only after I searched for an answer over web but didn’t  
find any. It’s getting irritating now.

-thanks

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 3, 2009, 9:33pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/2 "2009-07-03T21:33:59Z")

</div>

Vikrant wrote:

> Hi,  
> I have been developing a Rails plugin recently, and I have realised  
> that for every change I make into files of my plugin, I have to  
> restart the server to make changes take effect. While changes at  
> application’s own file appear instantly.  
> I’m writing only after I searched for an answer over web but didn’t  
> find any. It’s getting irritating now.
> 
> -thanks

I think it comes down to what type of plugin you are using first of all.  
If the plugin communicates with a database or something that can change  
dynamically then it can be done.

You can specify a configs environment with your plugin for how it  
behaves via option types. Load those in a database and have an admin  
page that changes the way it works there. I do this with a CMS plugin I  
created for my site.

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 8:25am UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/3 "2009-07-04T08:25:34Z")

</div>

No, you didn’t understand.  
What I’m telling is, while I’m coding in “development” mode, if I  
change something in “app/controllers/application\_controller.rb”,  
changes appear instantly. But if I change something in “vendor/plugins/  
my\_plugin/lib/example.rb”, I need to restart the server for changes to  
take effect. (even in “development” mode).  
In other words, applications files get reloaded on every request,  
while plugin files load only once and stay in memory.  
How can I make sure that plugin files also reload with every request?

- thanks

On Jul 4, 12:33 am, “Älphä Blüë” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 8:54am UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/4 "2009-07-04T08:54:48Z")

</div>

Thanks Simon M.,  
It worked!!  
I hope you mean ActiveSupport::Dependencies by Dependencies otherwise  
I get an error `const\_missing’:NameError: uninitialized constant  
Rails::Initializer::Dependencies.  
I first tried only to write

ActiveSupport::Dependencies.load\_once\_paths.delete RAILS\_ROOT + ‘/  
vendor/plugins/my\_plugin/lib’

but that didn’t work out, so I changed it too -

ActiveSupport::Dependencies.explicitly\_unloadable\_constants \<\<  
‘MyPlugin’  
ActiveSupport::Dependencies.load\_once\_paths.delete RAILS\_ROOT + ‘/  
vendor/plugins/my\_plugin/lib’

- thanks for your answer.

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 8:33am UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/5 "2009-07-04T08:33:10Z")

</div>

I have this in my development.rb file, which I set up when I was making  
some changes to a thirdparty plugin and got sick of the constant  
restarting.

# this forces the Ezgraphix plugin to be reloaded each time - to

facilitate ‘fixing’ and updating it  
Dependencies.explicitly\_unloadable\_constants \<\< ‘Ezgraphix’  
[“ezgraphix”].each do |plugin\_name|  
reloadable\_path = RAILS\_ROOT + “/vendor/plugins/#{plugin\_name}/lib”  
Dependencies.load\_once\_paths.delete(reloadable\_path)  
end

Cheers  
Simon

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 11:38am UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/6 "2009-07-04T11:38:22Z")

</div>

Hi - Why not develop the plugin in your app’s /lib directory first?

Once you’re happy with how it works, you can transfer it over to the  
plugin’s /lib directory

That way you don’t have to restart the server or change your apps  
configuration options

Gavin

> **[Ruby on Rails tips & Tutorials](https://handyrailstips.com/)**
>
> Ruby on Rails tips and tutorials

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 8:57am UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/7 "2009-07-04T08:57:08Z")

</div>

Your welcome.

The differences probably mean we are using different versions of Rails.

Cheers  
Simon

---

<div class="post-metadata">

**Author:** ![vikrant](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/cc9497/32.png) [@vikrant](https://www.ruby-forum.com/u/vikrant)\
**Post date:** [July 4, 2009, 3:31pm UTC](https://www.ruby-forum.com/t/how-to-dynamically-load-plugin-files-on-every-request/171510/8 "2009-07-04T15:31:50Z")

</div>

Gavin M. wrote:

> Hi - Why not develop the plugin in your app’s /lib directory first?
> 
> Once you’re happy with how it works, you can transfer it over to the  
> plugin’s /lib directory
> 
> That way you don’t have to restart the server or change your apps  
> configuration options
> 
> Gavin
> 
> [http://handyrailstips.com/](http://handyrailstips.com/)

That’s exactly what I was going to suggest. I have several lib files I  
develop parsers with and other app factorization methods with that I  
will eventually place into a plugin for later use. As I’m in  
development, I keep them there so I can change and keep going along.

Plugins are for finished pieces that you want to use in multiple  
projects. If you are developing something that will be used in multiple  
projects, get it working first and then create a plugin for it.  
Otherwise, if it’s just a one-off type of deal, keep it internal…
