Skip to content

Instantly share code, notes, and snippets.

@guiltry
Last active August 29, 2015 14:18
Show Gist options
  • Save guiltry/5c7794355ce6fe95cf3b to your computer and use it in GitHub Desktop.
Save guiltry/5c7794355ce6fe95cf3b to your computer and use it in GitHub Desktop.
# factory_method/user.rb
module FactoryMethod
# [Role] Factory
class User
def self.create(type, data=nil, current_user=nil)
if type == 'normal'
# login normally
else
# OAUTH
# Get the identity and user if they exist
identity = Identity.find_or_create_by(uid: data.uid, provider: data.provider)
# If a current_user is provided it always overrides the existing user
# to prevent the identity being locked with accidentally created accounts.
# Note that this may leave zombie accounts (with no associated identity) which
# can be cleaned up at a later date.
@user = current_user || identity.user
# Create the user if needed
if @user.blank?
case type
when 'google_oauth2'
@user = GoogleUser.new(identity, data).create
when 'facebook'
@user = FacebookUser.new(identity, data).create
end
end
end
@user
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment