Friendly Routes
Friendly Routes creates DSL for creating rails routes with human friendly URLs
Usage
How to use my plugin.
Installation
Add this line to your application's Gemfile:
gem 'friendly_routes'
And then execute:
$ bundle
Or install it yourself as:
$ gem install friendly_routes
Usage example
# config/routes.rb
free = FriendlyRoutes::Params::BooleanParams.new(:free, true: :free, false: :paid)
category = FriendlyRoutes::Params::CollectionParams.new(:category_id, Category, :title)
dummies_route = FriendlyRoutes::Route.new('/', controller: :dummies, action: :index)
dummies_route.params = [free, category]
friendly_url_for dummies_route, :get # '/:free/:category'
second_dummies_route = FriendlyRoutes::Route.new(controller: :dummies, action: :index)
dummies_route.params = [free, 'categories', category]
friendly_url_for dummies_route, :get, '/' # '/:free/categories/:category'
friendly_url_for dummies_route, :get, '/hello/' # '/hello/:free/categories/:category'
# app/controllers/dummies_controller.rb
class DummiesController < ApplicationController
before_action :parse_friendly_routes, only: [:index]
def index
end
end
# Categories:
# <Category id: 1, title: "lorem">
# <Category id: 2, title: "ipsum">
# GET "/free/lorem" - { free: true, category_id: 1}
# GET "/free/ipsum" - { free: true, category_id: 2}
# GET "/paid/lorem" - { free: false, category_id: 1}
# GET "/paid/ipsum" - { free: false, category_id: 2}
Configuration
Routes
- Create new route, pass method, path, controller name, and action to it.
ruby dummies_route = FriendlyRoutes::Route.new('/', controller: :dummies, action: :index) - Create route params. Note: params in route will be accepted in creation order
Boolean param, pass name, and hash with true and false keys.
FriendlyRoutes::Params::BooleanParams.new(:discount, true: :true_condition, false: :false_condition)
collection param, pass name, collection, and attribute keys.
FriendlyRoutes::Params::CollectionParams.new(:categories, Category.where(active: true), :title)
hash param, pass name, collection, and attribute keys.
FriendlyRoutes::Params::HashParams.new(:rooms, 'one-roomed' => 1, 'two-roomed' => 2)
- Initialize route wih
friendly_url_forruby friendly_url_for dummies_route, :get
Controllers
Simply call parse_friendly_routes before controller action to parse params.
before_action :parse_friendly_routes, only: [:index]
Contributing
Feel free to contribute into this repository by creating pull requests.
License
The gem is available as open source under the terms of the MIT License.