Created
August 18, 2012 14:01
-
-
Save fredguth/3386972 to your computer and use it in GitHub Desktop.
Stack Overflow Question 12016316
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Profile < ActiveRecord::Base | |
belongs_to :user, :class_name => 'User' | |
attr_accessible :name, :owner | |
attr_accessor :name, :owner | |
attr_reader :owner | |
def owner | |
self.user | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
devise :database_authenticatable, :registerable, :omniauthable, | |
:recoverable, :rememberable, :trackable, :validatable | |
attr_accessible :email, :password, :remember_me, :username | |
validates_presence_of :username | |
has_one :profile | |
accepts_nested_attributes_for :profile | |
before_create :build_user_profile | |
def build_user_profile | |
self.profile ||= build_profile | |
self.profile.save | |
true | |
end | |
def self.from_omniauth(auth) | |
where(auth.slice(:provider, :uid)).first_or_create do |user| | |
user.provider = auth.provider | |
user.uid = auth.uid | |
user.username = auth.info.nickname | |
user.username ||= auth.info.email.split("@")[0] | |
user.email = auth.info.email | |
user.profile ||= user.build_profile | |
user.profile.save | |
user.profile["name"] = auth.info.name | |
user.profile["phone"] = auth.info.phone | |
user.profile["image_remote_url"] = auth.info.image if auth.info.image | |
user.profile.save | |
end | |
end | |
def self.new_with_session(params, session) | |
if session["devise.user_attributes"] | |
new(session["devise.user_attributes"], without_protection: true) do |user| | |
user.attributes = params | |
user.valid? | |
end | |
else | |
super | |
end | |
end | |
def password_required? | |
super && provider.blank? | |
end | |
def update_with_password(params, *options) | |
if encrypted_password.blank? | |
update_attributes(params, *options) | |
else | |
super | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment