0.0
No commit activity in last 3 years
No release in over 3 years
Use this gem to add dynamic routes to your project.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
~> 0.10
~> 10.3
~> 3.1
~> 1.3

Runtime

~> 4.0
 Project Readme

DynamicRouter

Build Status Gem Version Test Coverage Code Climate

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

  1. Fork it ( https://github.com/coyosoftware/dynamic_router/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request