Skip to content

Instantly share code, notes, and snippets.

@guiltry
Created April 5, 2015 15:34
Show Gist options
  • Save guiltry/cc61ec56ad7bb0a60a50 to your computer and use it in GitHub Desktop.
Save guiltry/cc61ec56ad7bb0a60a50 to your computer and use it in GitHub Desktop.
module FactoryMethod
# [Role] Product
class FacebookUser
def initialize(identity, data)
@identity = identity
@data = data
end
def create
@user = User.where(email: @data.info.email)
# Create the user if it's a new registration
if @user.blank?
@user = User.new(
first_name: @data.info.first_name,
last_name: @data.info.last_name,
email: @data.info.email,
image: @data.info.image,
gender: @data.extra.raw_info.gender,
locale: @data.extra.raw_info.locale,
password: Devise.friendly_token[0,20]
)
@user.skip_confirmation! if @data.info.verified
@user.save!
end
# Associate the identity with the user if needed
if identity.user != @user
identity.user = @user
identity.profile = @data.info.urls.Facebook
identity.token = @data.credentials.token
identity.save!
end
return @user
end
end
# [Role] Product
class GoogleUser
def initialize(identity, data)
@identity = identity
@data = data
end
def create
@user = ::User.find_by(email: @data.info.email)
# Create the user if it's a new registration
if @user.blank?
@user = ::User.new(
first_name: @data.info.first_name,
last_name: @data.info.last_name,
email: @data.info.email,
image: @data.info.image,
gender: @data.extra.raw_info.gender,
locale: @data.extra.raw_info.locale,
password: Devise.friendly_token[0,20]
)
@user.skip_confirmation! if @data.extra.raw_info.email_verified
@user.save!
end
# Associate the identity with the user if needed
if @identity.user != @user
@identity.user = @user
@identity.profile = @data.extra.raw_info.profile
@identity.token = @data.credentials.token
@identity.save!
end
return @user
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment