Rails Gem to easily configure any url as a friendlier one.
You can see the gem running live in the following url: https://rails-friendly-urls-test.herokuapp.com/
The source code for this example project is available here: https://awesomeopensource.com/project/calonso/rails_friendly_urls_test
Installing this gem only requires you to add the following line to your
$ bundle install $ rails generate rails_friendly_urls:install
Here I detail you the steps that I followed to set up the friendly urls engine in the example application, so most of them should be the same for you, some others slightly different, but don't worry, you'll see appropriated explanations while reading this steps.
###1. Friendly URLs Storage
First of all we need to decide our urls storage technology, in my case I decided to use a standard activerecord rails model, but I guess that a YAML file could do the job as well or any other persistence technology.
5 fields are required to be stored to be able to build a friendly url. They are:
Just a reminder that you must make your storage engine to manage the defaults field as a hash.
Once you've done it, remember to include the
RailsFriendlyUrls::FriendlyUrl module in that class so that you can use
set_destination_data! method. That method will complete all your
defaults fields once you've provided
###2. The Manager
After running the bundled installer (
$ rails generate rails_friendly_urls:install) a new but incomplete file appears at
config/initializers/friendly_urls_manager.rb. We need to complete the
urls method in that file to make it return the list of friendly url objects (objects that simply respond to the five methods described above, i.e: path, slug, controller, action and defaults)
In my example project this is the final implementation:
# FriendlyUrls Manager contents class RailsFriendlyUrls::Manager def self.urls ::FriendlyUrl.all end end
###3. URL injection
As part of the installation process, a new line is inserted on top of the
routes.rb file that simply invokes the friendly urls engine to do its magic.
git checkout -b my-new-feature)
git commit -am 'Add some feature')
git push origin my-new-feature)
Released under the MIT-LICENSE.