/speaking-url

This gem is deprecated now in favor of the new Rewritten gem

Primary LanguageRuby

NOTE

This gem is deprecated in favor of the new Rewritten gem. It’s better to make use of the new gem because being Rack based it decouples URLs from your App: no need alter routing, models, controllers and views as with the SpeakingURL gem here.

Speaking URL

The Speaking URL Gem provides an extension for the routes.rb file of your Rails-App to allow for arbitrary URL being used for resources. The Gem is mainly SEO-motivated; it facilitates:

  • Usage of so called “static” or “nice-looking” URLs,

  • Implementation of a SEO friendly URL strategy,

  • Usage of different style URLs for resources of the same type

  • And (from a SEO-point maybe most importantly) Modification of URL strategies with proper HTTP (301) redirects.

This Gem does not:

  • generate URLs

  • manage all of your App’s URLs

These points have to by managed by yourself.

Installation

Add the gem to your Rails-App by including it in your Gemfile:

gem "speaking_url"

Usage

A quick list of steps that have to be done in order to get started with using this Gem follows.

Usage in your models

The Speaking Url must be setup within the models (resources) that you want to assign a Speaking URL to.

class Person
  include Mongoid::Document
  include SpeakingUrl::MongoResource
end

This adds an array field urls to the Person resource, where the current url and the url history are stored.

To add a new route to a some resource, use the add_mapping method instead of modifying the field directly.

@person.add_mapping('/some/new/mapping')

Core Extensions

The gem comes with some helpful inflection methods for the String class.

  • urlify : replaces all white space and special characters with dashes

  • unumlaut : converts German Umlauts to their ASCII counterparts

Additionally, the methods upcase and downcase are made Umlaut aware.

Configuring routes

To actually deploy the new routes, the Gem ships with the speaking_url_resource method. You simply provide the resources as symbols:

speaking_url_resource :person, :product

As a a result both - the person and product resource - are made accessible by their speaking url(s). The controller is inferred from the model name, but can be customized:

speaking_url_resource :person, :controller => 'vip/persons'
speaking_url_resource :product, :controller => 'my_products'

NOTE: You probably want to include the lines as one of your last statements in your route.rb file to:

  • give standard routes higher priority

  • and achieve better performance (due to DB calls in the routing process)

Usage in Controllers

In the appropriate controllers actions a specific resource is no longer fetches by it’s databse ID, but by the request.path:

@search = Person.find_by_url(request.path)

You can be sure there that a resource is found because otherwise the the controller wouldn’t have been called by the routing mapper.

Usage in views

Instead of using the default rails route helpers in your views, you want to use the speaking url helpers:

<%= link_to "A nice person", @person.current_url %>

Outlook and Todo

So far the Gem is pretty limited. What could be developed for following version is:

  • more configuration options

  • support for other persistent storage mappers

  • go to a single table/collection implementation for performance and more flexibility (i.e. index and create actions/routes)

  • make this a rack application

License

GPLv3. Copyright 2011 Kai Rubarth.