Skip to content
Extraits de code Groupes Projets
Valider 7e8f17cc rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

refactoring this stuff

parent 894e4ef1
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -51,12 +51,6 @@ class ServicesController < ApplicationController
end
def inviter
if !AppConfig[:open_invitations] && current_user.invites == 0
flash[:error] = I18n.t 'invitations.create.no_more'
redirect_to :back
return
end
@uid = params[:uid]
if i_id = params[:invitation_id]
......
......@@ -43,6 +43,8 @@ class Person < ActiveRecord::Base
validates_uniqueness_of :diaspora_handle
scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
scope :remote, where('people.owner_id IS NULL')
scope :local, where('people.owner_id IS NOT NULL')
def self.search_query_string(query)
query = query.downcase
......
......@@ -6,6 +6,17 @@ class ServiceUser < ActiveRecord::Base
belongs_to :invitation
before_save :attach_local_models
scope :with_local_people, joins(:person).merge(Person.local)
scope :with_remote_people, joins(:person).merge(Person.remote)
def already_invited?
self.invitation_id.present?
end
def on_diaspora?
self.person_id.present?
end
def attach_local_models
service_for_uid = Services::Facebook.where(:type => service.type.to_s, :uid => self.uid).first
......
......@@ -21,18 +21,11 @@ class Services::Facebook < Service
def finder(opts = {})
Rails.logger.debug("event=friend_finder type=facebook sender_id=#{self.user_id}")
if self.service_users.blank?
self.save_friends
self.service_users.reload
else
Resque.enqueue(Job::UpdateServiceUsers, self.id)
end
person = Person.arel_table
service_user = ServiceUser.arel_table
prevent_service_users_from_being_empty
if opts[:local]
ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].not_eq(nil)).order(:name).all
self.service_users.with_local_people
elsif opts[:remote]
ServiceUser.joins(:person).where(:service_id => self.id).where(person[:owner_id].eq(nil)).order(:name).all
self.service_users.with_remote_people
else
self.service_users
end
......@@ -49,4 +42,15 @@ class Services::Facebook < Service
}
ServiceUser.import(data, :on_duplicate_key_update => [:updated_at, :contact_id, :person_id, :request_id, :invitation_id, :photo_url, :name])
end
private
def prevent_service_users_from_being_empty
if self.service_users.blank?
self.save_friends
self.service_users.reload
else
Resque.enqueue(Job::UpdateServiceUsers, self.id)
end
end
end
%li.remote_friend{:id => "uid_" + friend.uid, :uid => friend.uid}
.right
-if friend.contact
/ TODO(*) add following method in Contact
sharing
- elsif friend.invitation_id
- if friend.already_invited?
= t('invitations.new.already_invited')
%br
= link_to t('.resend'), service_inviter_path(:uid => friend.uid, :provider => 'facebook', :invitation_id => friend.invitation_id)
- elsif friend.person
= link_to t('people.show.start_sharing'),
'#',
:class => 'button'
- elsif friend.on_diaspora?
= link_to(t('people.show.start_sharing'),'#', :class => 'button')
- elsif current_user.invites > 0
= form_tag service_inviter_path(:provider => 'facebook') do
= select_tag(:aspect_id, options_from_collection_for_select(all_aspects, 'id', 'name'))
= hidden_field_tag :uid, friend.uid
= submit_tag t('.invite')
= form_tag service_inviter_path(:provider => 'facebook') do
= select_tag(:aspect_id, options_from_collection_for_select(all_aspects, 'id', 'name'))
= hidden_field_tag :uid, friend.uid
= submit_tag t('.invite')
- if friend.person
- if friend.on_diaspora?
= person_image_link(friend.person)
- else
= image_tag(friend.photo_url, :class => 'avatar')
%h4.name
- if friend.person
- if friend.on_diaspora?
= link_to friend.name, person_path(friend.person)
- else
= friend.name
- unless friend.person.nil?
- if friend.on_diaspora?
%li{:id => "options_" + friend.uid, :class => "share_with hidden", :style => "height:auto"}
- contact = friend.contact
- contact ||= Contact.new(:person => friend.person)
= render 'aspect_memberships/aspect_dropdown', :contact => contact, :person => friend.person, :hang => 'left'
= render 'aspect_memberships/aspect_dropdown', :contact => contact_proxy(friend), :person => friend.person, :hang => 'left'
......@@ -141,14 +141,7 @@ describe ServicesController do
}.should_not change(Invitation, :count)
end
it' does not crete an invitation if the user has no invitations' do
@user.invites = 0
lambda {
put :inviter, @invite_params
}.should_not change(Invitation, :count)
end
it 'disregares the amount of invites if open_invitations are anabled' do
it 'disregares the amount of invites if open_invitations are enabled' do
open_bit = AppConfig[:open_invitations]
AppConfig[:open_invitations] = true
@user.invites = 0
......
......@@ -11,6 +11,19 @@ describe Person do
@person = Factory.create(:person)
end
context 'scopes' do
describe '.local' do
it 'returns only local people' do
Person.local =~ [@person]
end
end
describe '.remote' do
it 'returns only local people' do
Person.remote =~ [@user.person]
end
end
end
describe "delegating" do
it "delegates last_name to the profile" do
@person.last_name.should == @person.profile.last_name
......
......@@ -2,6 +2,40 @@ require 'spec_helper'
describe ServiceUser do
context 'scope' do
before do
@user = alice
@service = Services::Facebook.new(:access_token => "yeah")
@user.services << @service
@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.create(:uid => @user2_fb_id, :access_token => "yo", :user_id => @user2.id)
@su = ServiceUser.new(:service_id => @service.id, :uid => @user2_fb_id, :name => @user2_fb_name,:photo_url => @user2_fb_photo_url)
@su.person = @user2.person
@su.save
end
describe 'with_local_people' do
it 'returns services with local people' do
ServiceUser.with_local_people.count.should == 1
ServiceUser.with_remote_people.count.should == 0
end
end
describe 'with_remote_people' do
it 'returns services with remote people' do
@user2_service.delete
p = @user2.person
p.owner_id = nil
p.save
ServiceUser.with_local_people.count.should == 0
ServiceUser.with_remote_people.count.should == 1
end
end
end
describe '#finder' do
before do
@user = alice
......
......@@ -99,20 +99,20 @@ JSON
context 'opts' do
it 'only local does not return people who are remote' do
@service.save_friends
@service.finder(:local => true).each{|su| su.person.should == @user2.person}
@service.finder(:local => true).all.each{|su| su.person.should == @user2.person}
end
it 'does not return people who are remote' do
@service.save_friends
@service.finder(:remote => true).each{|su| su.person.should be_nil}
@service.finder(:remote => true).all.each{|su| su.person.should be_nil}
end
it 'does not return wrong service objects' do
su2 = ServiceUser.create(:service => @user2_service, :uid => @user2_fb_id, :name => @user2_fb_name, :photo_url => @user2_fb_photo_url)
su2.person.should == @user2.person
@service.finder(:local => true).each{|su| su.service.should == @service}
@service.finder(:remote => true).each{|su| su.service.should == @service}
@service.finder(:local => true).all.each{|su| su.service.should == @service}
@service.finder(:remote => true).all.each{|su| su.service.should == @service}
@service.finder.each{|su| su.service.should == @service}
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