Skip to content

Instantly share code, notes, and snippets.

@divoxx
Forked from mmurray/gist:594579
Created September 23, 2010 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save divoxx/594580 to your computer and use it in GitHub Desktop.
Save divoxx/594580 to your computer and use it in GitHub Desktop.
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable, :token_authenticatable,
:oauthable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :has_username, :is_admin, :profile
validates :username, :presence => true
validates_uniqueness_of :username
has_one :profile
# We make an alias of profile_assoc to the generated profile_method
alias_method :profile_assoc, :profile
def profile
profile_assoc || build_profile
end
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = ActiveSupport::JSON.decode(access_token.get('https://graph.facebook.com/me?'))
if user = User.find_by_email(data["email"])
user
else
# Create an user with a stub password.
u = User.create!(:email => data["email"], :password => Devise.friendly_token, :username => Devise.friendly_token, :has_username => false)
u.skip_confirmation!
u
end
end
def to_s
self.profile.avatar.url
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment