Skip to content

Instantly share code, notes, and snippets.

# The following lookups will be cached:
#
# User.find(123)
# User.find_by_id(123)
# User.find_by_username("Joe")
# User.find_by_email("joe@example.com")
class User < ActiveRecord::Base
cachable_model :find_by => [:username, :email]
end
module URI
# Safe alternative to URI.parse(uri_string).host to avoid exception being thrown
# on URLs with trailing spaces or invalid URLs. See also:
# http://www.practicalguile.com/2007/09/15/raising-uriinvalidurierror-from-a-perfectly-valid-uri/
def self.host(uri_string)
begin
URI.parse(uri_string.try(:strip)).host
rescue URI::InvalidURIError
nil
end
config.before(:each, :behaviour_type => :controller) do
rescue_action_in_public!
end