Skip to content
Extraits de code Groupes Projets
Valider 70d71ec4 rédigé par zhitomirskiyi's avatar zhitomirskiyi
Parcourir les fichiers

service users is green

parent ede7d4e8
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
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
...@@ -68,8 +68,8 @@ class Services::Facebook < Service ...@@ -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
...@@ -3,15 +3,18 @@ class CreateServiceUsers < ActiveRecord::Migration ...@@ -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
......
...@@ -274,18 +274,20 @@ ActiveRecord::Schema.define(:version => 20110319172136) do ...@@ -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
......
...@@ -46,7 +46,6 @@ namespace :db do ...@@ -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
......
...@@ -12,6 +12,7 @@ describe ServiceUser do ...@@ -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 ...@@ -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 ...@@ -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 ...@@ -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
end @su.request.should == @request
it 'caches the profile' do
@service.finder["#{@user2_fb_id}"][:request].sender.profile.loaded?.should be_true
end 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 ...@@ -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
......
...@@ -66,5 +66,19 @@ JSON ...@@ -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
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter