DynamicRouter
Add dynamic routes based on model attributes
Installation
Add this line to your application's Gemfile:
gem 'dynamic_router'
And then execute:
$ bundle
Or install it yourself as:
$ gem install dynamic_router
Usage
Suppose you want to create a friendly URL for a resource based on fields of an example model:
class Example < ActiveRecord::Base
# This model has a field called 'url'
end
To create a route to a resource using the field 'url' as URL, add the following line to your routes.rb:
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method"
After this when you create models like:
Example.create!(:url => "abc")
Example.create!(:url => "123")
The dynamic router will create the routes "/abc" and "/123" mapping to DummyController#dummy_method
You can pass the desired HTTP method also:
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :method => :post
And can specify default values to be passed, like:
DynamicRouter::Router.has_dynamic_route_for Example, Proc.new {|example| "/#{example.url}"}, "dummy#dummy_method", :defaults => {:some_value => Proc.new {|example| example.default_field}}
The dynamic router will map ALL records of the model on the startup and will create an after_save hook to create new routes as the models are created.
Notes for Unicorn If you use Unicorn as server (or some other server that spawn multiple workers), you might experience an 'intermittent' 404 page for the dynamic routes when they are updated after saving a model. This occurs because only the worker that served the update request will update its routes. To avoid this you can reload the routes before every request or implement some kind of messaging system (using redis or similar) to tell your workers to reload the routes.
Credits
Thanks to Michael Lang (http://codeconnoisseur.org/) for the code that inspired this gem.
Contributing
- Fork it ( https://github.com/coyosoftware/dynamic_router/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request