Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created September 23, 2011 01:27
Show Gist options
  • Select an option

  • Save jsmestad/1236543 to your computer and use it in GitHub Desktop.

Select an option

Save jsmestad/1236543 to your computer and use it in GitHub Desktop.
module Socialite
class User < ActiveRecord::Base
has_many :facebook_identities, :dependent => :destroy
has_many :twitter_identities, :dependent => :destroy
def self.identity_associations
@identity_associations ||= reflect_on_all_associations(:has_many).map(&:name).select {|n| n =~ /_identities$/}
end
# Create or update a user by identity
#
# @param [String] name of the provider i.e. 'facebook'
# @param [Hash] the resultant OmniAuth auth_hash returned after authenticating with a provider
# @return [User] the created or updated user
def self.create_or_update_by_identity(provider, auth_hash)
provider = "#{provider}_identity".classify
return nil unless Socialite.constants.include?(provider.to_sym)
identity_class = Socialite.const_get(provider.to_sym)
identity = identity_class.send(:find_or_initialize_by_unique_id, auth_hash['uid'])
identity.user = new if identity.user.blank?
identity.auth_hash = auth_hash
identity.save
identity.user
end
def identities
identities = self.class.identity_associations.collect { |identity| self.send(identity) }.flatten
end
# Returns the first linked facebook identity
#
# @return [FacebookIdentity] the first facebook identity
def facebook
facebook_identities.first
end
# Returns the first linked twitter account
#
# @return [TwitterIdentity] the first twitter identity
def twitter
twitter_identities.first
end
# Set the user's remember token
#
# @return [User] the current user
def remember
update_attributes(:remember_token => BCrypt::Password.create("#{Time.now}-#{self.login_account.type}-#{self.login}")) unless new_record?
end
# Clear the user's remember token
#
# @return [User] the current user
def forget
update_attributes(:remember_token => nil) unless new_record?
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment