From 70d71ec4911de932243f2b0e181abd9a31c80612 Mon Sep 17 00:00:00 2001 From: zhitomirskiyi Date: Fri, 18 Mar 2011 15:33:55 -0700 Subject: [PATCH] service users is green --- app/models/service_user.rb | 28 +++++++++ app/models/services/facebook.rb | 4 +- .../20110318000734_create_service_users.rb | 5 +- db/schema.rb | 10 ++-- lib/tasks/db.rake | 1 - spec/models/service_user_spec.rb | 60 +++++++------------ spec/models/services/facebook_spec.rb | 14 +++++ 7 files changed, 77 insertions(+), 45 deletions(-) diff --git a/app/models/service_user.rb b/app/models/service_user.rb index 226a3fdbb..2f1ce222a 100644 --- a/app/models/service_user.rb +++ b/app/models/service_user.rb @@ -1,2 +1,30 @@ class ServiceUser < ActiveRecord::Base + + belongs_to :person + belongs_to :contact + belongs_to :service + belongs_to :request + belongs_to :invitation + + 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 + else + self.person = nil + end + + if self.person + self.contact = self.service.user.contact_for(self.person) + self.request = Request.where(:recipient_id => self.service.user.person.id, + :sender_id => self.person_id).first + end + + self.invitation = Invitation.joins(:recipient).where(:sender_id => self.service.user_id, + :users => {:invitation_service => self.service.provider, + :invitation_identifier => self.uid}).first + end end diff --git a/app/models/services/facebook.rb b/app/models/services/facebook.rb index 519ee6cae..a24601a5a 100644 --- a/app/models/services/facebook.rb +++ b/app/models/services/facebook.rb @@ -68,8 +68,8 @@ class Services::Facebook < Service {:fields => ['name', 'id', 'picture'], :access_token => self.access_token}}) data = JSON.parse(response.body)['data'] data.each{ |p| - ServiceUser.create(:service_id => self.id, :name => p["name"], - :uid => p["id"], :picture_url => p["picture"]) + ServiceUser.find_or_create(:service_id => self.id, :name => p["name"], + :uid => p["id"], :photo_url => p["picture"]) } end end diff --git a/db/migrate/20110318000734_create_service_users.rb b/db/migrate/20110318000734_create_service_users.rb index 30d5d907f..7ac30e697 100644 --- a/db/migrate/20110318000734_create_service_users.rb +++ b/db/migrate/20110318000734_create_service_users.rb @@ -3,15 +3,18 @@ class CreateServiceUsers < ActiveRecord::Migration create_table :service_users do |t| t.string :uid, :null => false t.string :name, :null => false - t.string :picture_url, :null => false + t.string :photo_url, :null => false t.integer :service_id, :null => false t.integer :person_id t.integer :contact_id t.integer :request_id + t.integer :invitation_id + t.timestamps end add_index :service_users, :service_id + add_index :service_users, [:uid, :service_id], :unique => true end def self.down diff --git a/db/schema.rb b/db/schema.rb index e036785f0..f3032b399 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -274,18 +274,20 @@ ActiveRecord::Schema.define(:version => 20110319172136) do add_index "requests", ["sender_id"], :name => "index_requests_on_sender_id" create_table "service_users", :force => true do |t| - t.string "uid", :null => false - t.string "name", :null => false - t.string "picture_url", :null => false - t.integer "service_id", :null => false + t.string "uid", :null => false + t.string "name", :null => false + t.string "photo_url", :null => false + t.integer "service_id", :null => false t.integer "person_id" t.integer "contact_id" t.integer "request_id" + t.integer "invitation_id" t.datetime "created_at" t.datetime "updated_at" end add_index "service_users", ["service_id"], :name => "index_service_users_on_service_id" + add_index "service_users", ["uid", "service_id"], :name => "index_service_users_on_uid_and_service_id", :unique => true create_table "services", :force => true do |t| t.string "type", :null => false diff --git a/lib/tasks/db.rake b/lib/tasks/db.rake index 77e4b4022..b259fc28d 100644 --- a/lib/tasks/db.rake +++ b/lib/tasks/db.rake @@ -46,7 +46,6 @@ namespace :db do desc 'Purge and seed the current RAILS_ENV database using information from db/seeds.rb' task :reset do - puts "Resetting the database for #{Rails.env}".upcase Rake::Task['db:purge'].invoke Rake::Task['db:seed'].invoke diff --git a/spec/models/service_user_spec.rb b/spec/models/service_user_spec.rb index 1138621f0..d58da22e7 100644 --- a/spec/models/service_user_spec.rb +++ b/spec/models/service_user_spec.rb @@ -12,6 +12,7 @@ describe ServiceUser do @user2 = Factory.create(:user_with_aspect) @user2_fb_id = '820651' @user2_fb_name = 'Maxwell Salzberg' + @user2_fb_photo_url = 'http://cdn.fn.com/pic1.jpg' @user2_service = Services::Facebook.new(:uid => @user2_fb_id, :access_token => "yo") @user2.services << @user2_service @fb_list_hash = < @service.id, :uid => @user2_fb_id, :name => @user2_fb_name, + :photo_url => @user2_fb_photo_url) + end it 'contains a name' do - su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id) - su.save - su.name.should == @user2_fb_name + @su.name.should == @user2_fb_name end it 'contains a photo url' do - pending + @su.photo_url.should == @user2_fb_photo_url end it 'contains a FB id' do - @service.finder.include?(@user2_fb_id).should be_true + @su.uid.should == @user2_fb_id end it 'contains a diaspora person object' do - @service.finder["#{@user2_fb_id}"][:person].should == @user2.person + @su.person.should == @user2.person end - it 'caches the profile' do - @service.finder["#{@user2_fb_id}"][:person].profile.loaded?.should be_true + it 'queries for the correct service type' do + Services::Facebook.should_receive(:where).with(hash_including({:type => "Services::Facebook"})).and_return([]) + @su.send(:attach_local_models) end it 'does not include the person if the search is disabled' do p = @user2.person.profile p.searchable = false p.save - @service.finder["#{@user2_fb_id}"][:person].should be_nil + @su.save + @su.person.should be_nil end context "request" do @@ -67,36 +72,15 @@ JSON Request.count.should == 1 end it 'contains a request object if one has been sent' do - @service.finder["#{@user2_fb_id}"][:request].should == @request + @su.save + @su.request.should == @request end - - it 'caches the profile' do - @service.finder["#{@user2_fb_id}"][:request].sender.profile.loaded?.should be_true - end - - it 'caches the sender' do - @service.finder["#{@user2_fb_id}"][:request].sender.loaded?.should be_true - end - end it 'contains a contact object if connected' do connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first) - @service.finder["#{@user2_fb_id}"][:contact].should == @user.reload.contact_for(@user2.person) - end - - context 'only local' do - it 'does not return people who are remote' do - @service.finder(:local => true)['abc123'].should be nil - @service.finder(:local => true)["#{@user2_fb_id}"].should_not be_nil - end - end - - context 'only remote' do - it 'does not return people who are remote' do - @service.finder(:remote => true)['abc123'].should_not be nil - @service.finder(:remote => true)["#{@user2_fb_id}"].should be_nil - end + @su.save + @su.contact.should == @user.reload.contact_for(@user2.person) end context 'already invited' do @@ -107,14 +91,16 @@ JSON end it 'contains an invitation if invited' do @inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) - @service.finder["#{@user2_fb_id}"][:invitation_id].should == @inv.id + @su.save + @su.invitation_id.should == @inv.id end it 'does not find the user with a wrong identifier' do @user2.invitation_identifier = 'dsaofhnadsoifnsdanf' @user2.save @inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) - @service.finder["#{@user2_fb_id}"][:invitation_id].should be_nil + @su.save + @su.invitation_id.should be_nil end end end diff --git a/spec/models/services/facebook_spec.rb b/spec/models/services/facebook_spec.rb index 414e5161a..4ce8beb4b 100644 --- a/spec/models/services/facebook_spec.rb +++ b/spec/models/services/facebook_spec.rb @@ -66,5 +66,19 @@ JSON @service.save_friends }.should change(ServiceUser, :count).by(2) end + + context 'only local' do + it 'does not return people who are remote' do + @service.finder(:local => true).should be_empty + @service.finder(:local => true).should_not be_empty + end + end + + context 'only remote' do + it 'does not return people who are remote' do + @service.finder(:remote => true).should_not be_empty + @service.finder(:remote => true).should be_empty + end + end end end