1 Replies - 618 Views - Last Post: 22 April 2014 - 07:15 PM Rate Topic: -----

#1 code4life2live  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-April 14

link facebook twitter youtube linkedin to my account , hyperlinks ruby

Posted 22 April 2014 - 11:17 AM

As an entity I should be able to link my account to Facebook, Twitter, YouTube, and LinkedIn (Just provide links)
i tried to do it using


def self.find_for_twitter_oauth(auth, signed_in_resource=nil)
    user = User.where(:provider => auth.provider, :uid => auth.uid).first
    if user
      return user
    else
      registered_user = User.where(:email => auth.uid + "@twitter.com").first
      if registered_user
        return registered_user
      else

        user = User.create(name:auth.extra.raw_info.name,
                            provider:auth.provider,
                            uid:auth.uid,
                            email:auth.uid+"@twitter.com",
                            password:Devise.friendly_token[0,20],
                          )
      end





but it did not work out pretty well please help me i tried many things

This post has been edited by macosxnerd101: 22 April 2014 - 12:51 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: link facebook twitter youtube linkedin to my account , hyperlinks ruby

#2 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1357
  • View blog
  • Posts: 3,424
  • Joined: 28-November 09

Re: link facebook twitter youtube linkedin to my account , hyperlinks ruby

Posted 22 April 2014 - 07:15 PM

View Postcode4life2live, on 22 April 2014 - 01:17 PM, said:

def self.find_for_twitter_oauth(auth, signed_in_resource=nil) # signed_in_resource isn't used
  # What if it returns nothing?
  #
  # You're pulling multiple records for no reason, just to get the first? That's wasteful
  user = User.where(:provider => auth.provider, :uid => auth.uid).first

  if user
     return user # You don't need explicit returns you know
  else
    # Same as above, wasting resources and it'll crash
    registered_user = User.where(:email => auth.uid + "@twitter.com").first
    
    # These if branches are unnecessary as well
    if registered_user
      return registered_user # Unneeded return
    else
      # Indentation is messy
      user = User.create(
        name: auth.extra.raw_info.name,
        provider: auth.provider,
        uid: auth.uid,
        email: auth.uid+"@twitter.com",
        password: Devise.friendly_token[0,20], # extra comma?
      )
    # You're missing a few ends here, that hurts
end



Well first let's clean up the code, you're doing some really odd and unnecessary things. I've added comments to original code.

Now let's refactor it a bit:
def self.find_for_twitter_oauth(auth)
  User.find_by(provider: auth.provider, uid: auth.uid) ||
  User.find_by(email:    "#{auth.uid}@twitter.com")    ||
  User.create(
    name:     auth.extra.raw_info.name,
    provider: auth.provider,
    uid:      auth.uid,
    email:    "#{auth.uid}@twitter.com",
    password: Devise.friendly_token[0,20]
  )
end



Use short circuiting for nils to pass through instead of a bunch of conditional branches. Remember that things return implicitly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1