= has_handle_fallback
Make it easy to use handles (callsigns/monikers/usernames) in URLs, even if they might be blank.
== Quickstart
class Person < ActiveRecord::Base
has_handle_fallback :email
end
This assumes that Person has the fields handle and email.
class PeopleController < ActionController::Base
def object
@object ||= Person.find_by_id_or_handle(params[:id])
end
end
Then you can safely use Person#to_param in URLs, etc. because, when in doubt, the finder will use the numeric ID.
Long forms: Person.find_by_id_or_handle('pierrebourdieu'), Person.find_by_id_or_handle(1)
Short forms: Person['pierrebourdieu'], Person[1]
== What's going on?
def test_only_uses_handle_as_param_when_not_changed_from_value_in_database
pierre = Person.new :email => 'pierre.bourdieu@example.com'
# not saved, so to_param is just blank
assert_equal '', pierre.to_param
assert_equal nil, Person[pierre.to_param]
# no handle set, so to_param is integer primary key
pierre.save!
assert_equal pierre.id.to_s, pierre.to_param
assert_equal pierre, Person[pierre.to_param]
# handle is set, but not saved, so STILL use integer primary key
pierre.handle = 'pierrebourdieu'
assert_equal pierre.id.to_s, pierre.to_param
assert_equal pierre, Person[pierre.to_param]
# now handle is saved, so we can use it as the param
pierre.save!
assert_equal 'pierrebourdieu', pierre.to_param
assert_equal pierre, Person[pierre.to_param]
# handle was changed, so let's use the integer primary key until it's saved again
pierre.handle = ''
assert_equal pierre.id.to_s, pierre.to_param
assert_equal pierre, Person[pierre.to_param]
end
== Copyright
Copyright (c) 2010 Seamus Abshere. See LICENSE for details.