Make sure that related data for fb users is attached to them

This commit is contained in:
Raphael Sofaer 2011-08-03 15:28:56 -07:00
parent eb16d15c42
commit 7d0c796762
3 changed files with 11 additions and 5 deletions

View file

@ -7,11 +7,10 @@ class ServiceUser < ActiveRecord::Base
before_save :attach_local_models
private
def attach_local_models
service_for_uid = Services::Facebook.where(:type => service.type.to_s, :uid => self.uid).first
if !service_for_uid.blank? && (service_for_uid.user.person.profile.searchable)
self.person = service_for_uid.user.person
self.person = service_for_uid.user.person
else
self.person = nil
end

View file

@ -42,9 +42,11 @@ class Services::Facebook < Service
url = "https://graph.facebook.com/me/friends?fields[]=name&fields[]=picture&access_token=#{URI.escape(self.access_token)}"
response = Faraday.get(url)
data = JSON.parse(response.body)['data']
s_users = data.map{ |p|
ServiceUser.new(:service_id => self.id, :uid => p["id"], :photo_url => p["picture"], :name => p["name"])
data.map!{ |p|
su = ServiceUser.new(:service_id => self.id, :uid => p["id"], :photo_url => p["picture"], :name => p["name"])
su.attach_local_models
su
}
ServiceUser.import(s_users)
ServiceUser.import(data)
end
end

View file

@ -66,6 +66,11 @@ JSON
@service.save_friends
}.should change(ServiceUser, :count).by(2)
end
it 'attaches local models' do
@service.save_friends
@service.service_users.first.person.should == @user2.person
end
end
describe '#finder' do