Agile Web Development

Build it. Launch it. Love it.

acts_as_url_param

ActsAsUrlParam

========

This plugin allows restful resources to be automatically exposed with human readable urls based on another method on the resource.

The URL parameter value that is computed will be unique within the model. If the model is using inheritance, it’s recommended to define acts_as_url_param on the base class.

Defines one public class methods on your model: MyResource.url_param_available?(candidate,id=nil) # => true # if the candidate value does not exist for the model

Defines one public instance method on your model: my_resource.compute_url_param # => "unique-url-parameter-2"

Contributors

======

"Chris Eppstein"<chris@eppsteins.net> "Joshua Bates"<joshuabates@gmail.com>

Example

=

# use the url_param column as the model’s url. It will be set and/or modified whenever the default method changes. # The default methods, in order of precedence, are: :name, :label, :title class MyResource < ActiveRecord::Base

  acts_as_url_param

end

# use the url_name column as the model’s url. It will be set and/or modified whenever title changes class MyResource < ActiveRecord::Base

  acts_as_url_param :url_name, :from => :title

end

# use the url_param column as the model’s url. It will be set and/or modified whenever title changes class MyResource < ActiveRecord::Base

  acts_as_url_param :from => :title

end

# use the url_param column as the model’s url. It will be set and/or modified whenever the :on event occurs. # Allowed events are: :create, :save, :update. Default is :create so that your permalinks stay permanent. # May also be a Proc that accepts the model instance and returns true if url_param should change. class MyResource < ActiveRecord::Base

  acts_as_url_param :from => :title, :on => :create

end

# use the url_name column as the model’s url. It will be set and/or modified whenever the :on event(s) occur. class MyResource < ActiveRecord::Base

  acts_as_url_param :url_name, :from => :title, :on => :create

end

# Passing a block allows you to approve candidate url parameters. # This enables definition of a url space across that spans several models. This is easiest if all # models are acts_as_url_param. class MyResource < ActiveRecord::Base

  acts_as_url_param :url_name, :from => :title do |candidate|
    MyResource.url_param_available?(candidate,self.id) &&
    MyOtherResource.url_param_available?(candidate)
  end

end

Copyright © 2007 Caring, Inc., released under the MIT license

Vitals

Home http://github.com/caring/acts_as_url_param/tree/master
Repository git://github.com/caring/acts_as_url_param.git
License Rails' (MIT)
Tags Tag_red to_param url
Rating (4 votes)
Owner Chris Eppstein
Created 19 March 2008

Comments

  • Avatar
    19 March 2008

    Note that this is already in production at caring.com and it is thoroughly tested. Props to Joshua Bates who did a bulk of the coding on this plugin despite what it says in source control. Enjoy!

  • aoedl
    8 April 2008

    How can I replace other characters, especially german "umlauts" like "ä", "ö", "ü" and "ß"?

    Thanks.

  • Avatar
    juju
    16 May 2008

    looks just like what I need! but I can't get it to work...

    I created a table "urlname" tried every combinations in the provided examples, they all successfully fill up the column with the escaped title, the "linkto" provide the good links (i.e. /articles/my-article) but I always get error such as:

    Couldn't find Article with ID=my-article

    the tests passes, I first thought I had a conflict with another plugin, then thought it might not run on rails 2, so I took an older 1.2.3 project, reduced to the minimum, just an article model with a title and a body.....(indeed, it's a restfull ressource)

    what am I missing ? how the routing is supposed to be handled ?

  • Tim
    15 October 2008

    This seems like a cool plugin. I can't figure out how to use it though. Do you have some more detailed instructions? Some sort of simple walk-through would be awesome.

  • Avatar
    Chris Eppstein
    6 February 2009

    @tim I'll see what I can do.

    @juju Use Article.find_by_url(params[:id]) to look up your instance.

    @aoedl the urlsafe method has a :replacements hash that will let you specify such things, but there's not a straightforward way to pass that info from the actsasurlparam declaration. Patches are gladly accepted.

Add a comment