service users is green

This commit is contained in:
zhitomirskiyi 2011-03-18 15:33:55 -07:00
parent ede7d4e8e2
commit 70d71ec491
7 changed files with 77 additions and 45 deletions

View file

@ -1,2 +1,30 @@
class ServiceUser < ActiveRecord::Base 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 end

View file

@ -68,8 +68,8 @@ class Services::Facebook < Service
{:fields => ['name', 'id', 'picture'], :access_token => self.access_token}}) {:fields => ['name', 'id', 'picture'], :access_token => self.access_token}})
data = JSON.parse(response.body)['data'] data = JSON.parse(response.body)['data']
data.each{ |p| data.each{ |p|
ServiceUser.create(:service_id => self.id, :name => p["name"], ServiceUser.find_or_create(:service_id => self.id, :name => p["name"],
:uid => p["id"], :picture_url => p["picture"]) :uid => p["id"], :photo_url => p["picture"])
} }
end end
end end

View file

@ -3,15 +3,18 @@ class CreateServiceUsers < ActiveRecord::Migration
create_table :service_users do |t| create_table :service_users do |t|
t.string :uid, :null => false t.string :uid, :null => false
t.string :name, :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 :service_id, :null => false
t.integer :person_id t.integer :person_id
t.integer :contact_id t.integer :contact_id
t.integer :request_id t.integer :request_id
t.integer :invitation_id
t.timestamps t.timestamps
end end
add_index :service_users, :service_id add_index :service_users, :service_id
add_index :service_users, [:uid, :service_id], :unique => true
end end
def self.down def self.down

View file

@ -274,18 +274,20 @@ ActiveRecord::Schema.define(:version => 20110319172136) do
add_index "requests", ["sender_id"], :name => "index_requests_on_sender_id" add_index "requests", ["sender_id"], :name => "index_requests_on_sender_id"
create_table "service_users", :force => true do |t| create_table "service_users", :force => true do |t|
t.string "uid", :null => false t.string "uid", :null => false
t.string "name", :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 "service_id", :null => false
t.integer "person_id" t.integer "person_id"
t.integer "contact_id" t.integer "contact_id"
t.integer "request_id" t.integer "request_id"
t.integer "invitation_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
end end
add_index "service_users", ["service_id"], :name => "index_service_users_on_service_id" 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| create_table "services", :force => true do |t|
t.string "type", :null => false t.string "type", :null => false

View file

@ -46,7 +46,6 @@ namespace :db do
desc 'Purge and seed the current RAILS_ENV database using information from db/seeds.rb' desc 'Purge and seed the current RAILS_ENV database using information from db/seeds.rb'
task :reset do task :reset do
puts "Resetting the database for #{Rails.env}".upcase puts "Resetting the database for #{Rails.env}".upcase
Rake::Task['db:purge'].invoke Rake::Task['db:purge'].invoke
Rake::Task['db:seed'].invoke Rake::Task['db:seed'].invoke

View file

@ -12,6 +12,7 @@ describe ServiceUser do
@user2 = Factory.create(:user_with_aspect) @user2 = Factory.create(:user_with_aspect)
@user2_fb_id = '820651' @user2_fb_id = '820651'
@user2_fb_name = 'Maxwell Salzberg' @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_service = Services::Facebook.new(:uid => @user2_fb_id, :access_token => "yo")
@user2.services << @user2_service @user2.services << @user2_service
@fb_list_hash = <<JSON @fb_list_hash = <<JSON
@ -20,7 +21,7 @@ describe ServiceUser do
{ {
"name": "#{@user2_fb_name}", "name": "#{@user2_fb_name}",
"id": "#{@user2_fb_id}", "id": "#{@user2_fb_id}",
"picture": "http://cdn.fn.com/pic1.jpg" "picture": ""
}, },
{ {
"name": "Person to Invite", "name": "Person to Invite",
@ -36,28 +37,32 @@ JSON
end end
context 'lifecycle callbacks' do context 'lifecycle callbacks' do
before do
@su = ServiceUser.create(:service_id => @service.id, :uid => @user2_fb_id, :name => @user2_fb_name,
:photo_url => @user2_fb_photo_url)
end
it 'contains a name' do it 'contains a name' do
su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id) @su.name.should == @user2_fb_name
su.save
su.name.should == @user2_fb_name
end end
it 'contains a photo url' do it 'contains a photo url' do
pending @su.photo_url.should == @user2_fb_photo_url
end end
it 'contains a FB id' do it 'contains a FB id' do
@service.finder.include?(@user2_fb_id).should be_true @su.uid.should == @user2_fb_id
end end
it 'contains a diaspora person object' do it 'contains a diaspora person object' do
@service.finder["#{@user2_fb_id}"][:person].should == @user2.person @su.person.should == @user2.person
end end
it 'caches the profile' do it 'queries for the correct service type' do
@service.finder["#{@user2_fb_id}"][:person].profile.loaded?.should be_true Services::Facebook.should_receive(:where).with(hash_including({:type => "Services::Facebook"})).and_return([])
@su.send(:attach_local_models)
end end
it 'does not include the person if the search is disabled' do it 'does not include the person if the search is disabled' do
p = @user2.person.profile p = @user2.person.profile
p.searchable = false p.searchable = false
p.save p.save
@service.finder["#{@user2_fb_id}"][:person].should be_nil @su.save
@su.person.should be_nil
end end
context "request" do context "request" do
@ -67,36 +72,15 @@ JSON
Request.count.should == 1 Request.count.should == 1
end end
it 'contains a request object if one has been sent' do 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 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 end
it 'contains a contact object if connected' do it 'contains a contact object if connected' do
connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first) connect_users(@user, @user.aspects.first, @user2, @user2.aspects.first)
@service.finder["#{@user2_fb_id}"][:contact].should == @user.reload.contact_for(@user2.person) @su.save
end @su.contact.should == @user.reload.contact_for(@user2.person)
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
end end
context 'already invited' do context 'already invited' do
@ -107,14 +91,16 @@ JSON
end end
it 'contains an invitation if invited' do it 'contains an invitation if invited' do
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) @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 end
it 'does not find the user with a wrong identifier' do it 'does not find the user with a wrong identifier' do
@user2.invitation_identifier = 'dsaofhnadsoifnsdanf' @user2.invitation_identifier = 'dsaofhnadsoifnsdanf'
@user2.save @user2.save
@inv = Invitation.create(:sender => @user, :recipient => @user2, :aspect => @user.aspects.first) @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 end
end end

View file

@ -66,5 +66,19 @@ JSON
@service.save_friends @service.save_friends
}.should change(ServiceUser, :count).by(2) }.should change(ServiceUser, :count).by(2)
end 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
end end