Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jacob Rothstein
committed
Oct 21, 2009
1 parent
005565a
commit 43cd953
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
h1. UrlParam | ||
|
||
Yet another rails url parameter column plugin. It's like all of the others, only it's mine. | ||
|
||
h2. Usage | ||
|
||
<pre><code> | ||
#--- | ||
#schema.rb | ||
create_table "entries", :force => true do |t| | ||
t.string "title" | ||
t.string "url_param" | ||
#... | ||
end | ||
add_index "entries", ["url_param"], :name => "index_entries_on_url_param" | ||
|
||
|
||
#--- | ||
#entry.rb | ||
class Entry < ActiveRecord::Base | ||
generate_url_param_from :title | ||
end | ||
|
||
e = Entry.new :title => 'hello, world' | ||
e.url_param #=> 'hello-world' | ||
e.to_param #=> 'hello-world' | ||
e.save! | ||
|
||
e2 = Entry.create(:title => 'hello, world').url_param #=> 'hello-world-1' | ||
|
||
Entry.create(:title => '! hello world').url_param #=> 'hello-world-2' | ||
|
||
Entry['hello-world-2'].title #=> '! hello world' | ||
Entry.find_by_param('hello-world').title #=> 'hello, world' | ||
|
||
</code></pre> | ||
|
||
The only allowable characters in a url param are lowercase letters, numbers, and the hyphen. | ||
|
||
If you want to make your own url_param, you can use | ||
|
||
<pre><code> | ||
class Entry < ActiveRecord::Base | ||
self.url_param_column = :some_other_column | ||
end | ||
|
||
e = Entry.create :some_other_column => 3 | ||
|
||
e.to_param #=> '3' | ||
|
||
Entry.find_by_param(3).id #=> e.id | ||
Entry[3].id #=> e.id | ||
</code></pre> | ||
|