diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb index 09d6cbe7d..2db4646a0 100644 --- a/app/controllers/services_controller.rb +++ b/app/controllers/services_controller.rb @@ -26,9 +26,11 @@ class ServicesController < ApplicationController :uid => auth['uid']) current_user.services << service + current_user.update_profile(current_user.person.profile.from_omniauth_hash(user)) + flash[:notice] = I18n.t 'services.create.success' if current_user.getting_started - redirect_to getting_started_path(:step => 3) + redirect_to getting_started_path else redirect_to services_url end diff --git a/app/models/profile.rb b/app/models/profile.rb index 579dd2208..8e7dd4976 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -45,6 +45,7 @@ class Profile < ActiveRecord::Base before_validation do self.tag_string = self.tag_string.split[0..4].join(' ') end + before_save do self.build_tags self.construct_full_name @@ -77,6 +78,23 @@ class Profile < ActiveRecord::Base result || '/images/user/default.png' end + def from_omniauth_hash(omniauth_user_hash) + mappings = {"description" => "bio", + 'image' => 'image_url', + 'first_name' => 'first_name', + 'last_name' => 'last_name', + 'location' => 'location', + 'name' => 'full_name' + } + if omniauth_user_hash['first_name'].blank? || omniauth_user_hash['last_name'].blank? + omniauth_user_hash['first_name'], omniauth_user_hash['last_name'] = omniauth_user_hash['name'].split + end + + update_hash = Hash[omniauth_user_hash.map {|k, v| [mappings[k], v] }] + + self.attributes.merge(update_hash){|key, old, new| old.blank? ? new : old} + end + def image_url= url return image_url if url == '' if url.nil? || url.match(/^https?:\/\//) diff --git a/app/models/user.rb b/app/models/user.rb index 04c8ce1cb..ac46491db 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -309,6 +309,7 @@ class User < ActiveRecord::Base params[:image_url_medium] = photo.url(:thumb_medium) params[:image_url_small] = photo.url(:thumb_small) end + if self.person.profile.update_attributes(params) Postzord::Dispatcher.build(self, profile).post true diff --git a/spec/models/profile_spec.rb b/spec/models/profile_spec.rb index 81a816674..9200fbef2 100644 --- a/spec/models/profile_spec.rb +++ b/spec/models/profile_spec.rb @@ -29,6 +29,22 @@ describe Profile do end end + describe 'from_omniauth_hash' do + before do + @from_omniauth = {'first_name' => 'bob', 'last_name' => 'jones', 'description' => 'this is my bio', 'location' => 'sf', 'image' => 'http://cats.com/gif.gif'} + end + + it 'outputs a hash that can update a diaspora profile' do + profile = Profile.new + profile.from_omniauth_hash(@from_omniauth)['first_name'].should == 'bob' + end + + it 'does not overwrite any exsisting profile fields' do + profile = Profile.new(:first_name => 'maxwell') + profile.from_omniauth_hash(@from_omniauth)['first_name'].should == 'maxwell' + end + end + describe '#contruct_full_name' do it 'generates a full name given only first name' do profile = Factory(:person).profile