No commit activity in last 3 years
No release in over 3 years
Make it easy to use handles (callsigns/monikers/usernames) in URLs, even if they might be blank. Provides sensible validations and a find_by_id_or_handle method.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

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 © 2010 Seamus Abshere. See LICENSE for details.