Skip to content

Instantly share code, notes, and snippets.

@cheald
Created October 29, 2012 02:03
Show Gist options
  • Save cheald/3970981 to your computer and use it in GitHub Desktop.
Save cheald/3970981 to your computer and use it in GitHub Desktop.
class User
# Return a string suitable for uniquely identifying this user in URLs
# @return [String]
def to_param
begin
n = shadow_username
n = _id.to_s if n.blank? or (username_changed? and !valid?)
n
end
end
# Find a single record by ID or slug
# @param [username, document ID] Username or document ID to find
# @raise MongoMapper::DocumentNotFound
# @return [Integer]
def get!(id)
id = id.to_s.downcase
if id.match(/[a-f0-9]{24,}/i)
find(id)
else
where(:username => id).find
end
end
end
# Usage:
user_path(@user) # => /users/508bec6597b2f809b6000001
user_path(@user) # => /users/cheald
User.get!("508bec6597b2f809b6000001")
User.get!("cheald")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment