skwpspace yan pritzker’s home on the web

skwpspace is Yan Pritzker's home on the web

Blog :: Photography :: About Me

hello, i'm yan

This blog is about startups, blogging, Ruby On Rails, virtualization and cloud computing, photography, customer service, marketing, ux and design, git, and lots more.

Get updates by email
Follow me on twitter

Top Posts

planypus

I'm the founder of Planypus, the place to share your plans!

cohesiveft

Accessible, manageable, virtualized application stacks ready to download or deploy to the cloud!

flickr

at the paradeat the parade@slava626@leorazellman and @smazo at bat17my poor ricohyou're wearing my sweaterthe graduatethe answer, my friend

Archives

Contact

Reach me at yan at pritzker.ws

Posted
27 March 2007 @ 7pm

Tagged
RubyOnRails, software

Dynamically adding routes from your engine plugins

At Cohesive FT I am developing a sort of webmin console that has drop-in pluggable components. I am using the RoR plugin system with the engines plugin to drop entire chunks of MVC infrastructure into my application as required. This is all well and good except that rails engines require you to add this line into your routes.rb:

map.from_plugin :your_plugin_name

Now since I want a truly plug-n-play system, I can’t be modifying the route file of my main application. So here is a quick solution to import all the routes (note that in engine-plugin style you do not use the “map.” prefix for your routes, so I am adding that in)


#--routes.rb of the main applicatoin--
Dir["#{RAILS_ROOT}/vendor/plugins/*"].each do |plugin|
File.open(”#{plugin}/routes.rb”).each do |line|
eval “map.#{line}”
end
end

That’s all folks!


3 Comments

Posted by
James Adam
28 March 2007 @ 10am

Why not try this?

Dir["#{RAILS_ROOT}/vendor/plugins/*"].each do |plugin_dir|
map.from_plugin(File.basename(plugin_dir))
end

.. it should work, and using the API should protect you against any changes to the way that plugin routes are implemented. Ideally you’d be able to run

Rails.plugins.each do |plugin|
map.from_plugin(plugin.name)
end

but this will only work if you *aren’t* using the

config.plugins = %w(engines *)

loading style, since all other plugins will be loaded during the “after_initialize” hook and so won’t be present in Rails.plugins.

HTH,

James


Posted by
Yan
28 March 2007 @ 3pm

Good point James, I think I overhacked the solution on this one. Will try your method.

I didn’t know about Rails.plugins..that’s pretty nice. But in actuality what I didn’t show in my code is that PLUGINS is a special list of plugins that I have prefiltered since they are the application specific dropins rather than any old plugin that might exist.


Posted by
james dot schumann » links for 2007-03-28
28 March 2007 @ 3pm

[...] Dynamically adding routes from your engine plugins at skwpspace.com (tags: rails development) [...]


Leave a Comment

Open letter to yelp.com Five tips for twitter usage in the workplace