public
Description: Pretty url support for Ruby on Rails applications
Homepage: http://www.caring.com
Clone URL: git://github.com/caring/acts_as_url_param.git
Chris Eppstein (author)
Tue Mar 25 08:02:53 -0700 2008
commit  f57fc17722bbc9a358a710ebde9fcfa1e69119d8
tree    5a959de3329ca712e34e95a0fa399458faf21b8a
parent  d6c53692da2d7aaa70cad14ea23f8e5d5f96ded3
100644 65 lines (52 sloc) 2.459 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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 (c) 2007 Chris Eppstein, released under the MIT license