From d1a16d09735d6f97bb95639aea1c83418d03e798 Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Sat, 9 Aug 2014 17:59:38 -0700 Subject: [PATCH] refactor --- app/models/service.rb | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/app/models/service.rb b/app/models/service.rb index 49f4990a..f6e20b7e 100644 --- a/app/models/service.rb +++ b/app/models/service.rb @@ -64,20 +64,24 @@ class Service < ActiveRecord::Base def self.provider_specific_options(omniauth) case omniauth['provider'] + when 'twitter', 'github' + { name: omniauth['info']['nickname'] } when '37signals' - { user_id: omniauth['extra']['accounts'][0]['id'] } + { user_id: omniauth['extra']['accounts'][0]['id'], name: omniauth['info']['name'] } else - {} + { name: omniauth['info']['nickname'] } end end def self.initialize_or_update_via_omniauth(omniauth) - find_or_initialize_by(provider: omniauth['provider'], name: omniauth['info']['nickname'] || omniauth['info']['name']).tap do |service| + options = provider_specific_options(omniauth) + + find_or_initialize_by(provider: omniauth['provider'], name: options[:name]).tap do |service| service.assign_attributes token: omniauth['credentials']['token'], secret: omniauth['credentials']['secret'], refresh_token: omniauth['credentials']['refresh_token'], expires_at: omniauth['credentials']['expires_at'] && Time.at(omniauth['credentials']['expires_at']), - options: provider_specific_options(omniauth) + options: options end end end \ No newline at end of file