when connected, external services fill in profile info that you have not yet filled out

This commit is contained in:
Maxwell Salzberg 2011-10-17 17:28:30 -07:00
parent aa1623a95f
commit cb89772a6a
4 changed files with 38 additions and 1 deletions

View file

@ -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

View file

@ -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?:\/\//)

View file

@ -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

View file

@ -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