Skip to content
Extraits de code Groupes Projets
Valider 3c34749d rédigé par Raphael Sofaer's avatar Raphael Sofaer
Parcourir les fichiers

Put a select limit on the person query on contactsController index, since we...

Put a select limit on the person query on contactsController index, since we know exactly what we're doing with the data
parent 7df883ea
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -24,12 +24,11 @@ class ContactsController < ApplicationController ...@@ -24,12 +24,11 @@ class ContactsController < ApplicationController
format.html { @contacts = sort_and_paginate_profiles(@contacts) } format.html { @contacts = sort_and_paginate_profiles(@contacts) }
format.mobile { @contacts = sort_and_paginate_profiles(@contacts) } format.mobile { @contacts = sort_and_paginate_profiles(@contacts) }
format.json { format.json {
@people = Person.joins(:contacts => :aspect_memberships). @people = Person.for_json.joins(:contacts => :aspect_memberships).
select('DISTINCT people.*').
where(:contacts => { :user_id => current_user.id }, where(:contacts => { :user_id => current_user.id },
:aspect_memberships => { :aspect_id => params[:aspect_ids] }) :aspect_memberships => { :aspect_id => params[:aspect_ids] })
render :json => @people.includes(:profile).to_json render :json => @people.to_json
} }
end end
end end
......
...@@ -45,6 +45,7 @@ class Person < ActiveRecord::Base ...@@ -45,6 +45,7 @@ class Person < ActiveRecord::Base
scope :searchable, joins(:profile).where(:profiles => {:searchable => true}) scope :searchable, joins(:profile).where(:profiles => {:searchable => true})
scope :remote, where('people.owner_id IS NULL') scope :remote, where('people.owner_id IS NULL')
scope :local, where('people.owner_id IS NOT NULL') scope :local, where('people.owner_id IS NOT NULL')
scope :for_json, select('DISTINCT people.id, people.diaspora_handle').includes(:profile)
def self.featured_users def self.featured_users
AppConfig[:featured_users].present? ? Person.where(:diaspora_handle => AppConfig[:featured_users]) : [] AppConfig[:featured_users].present? ? Person.where(:diaspora_handle => AppConfig[:featured_users]) : []
...@@ -63,7 +64,7 @@ class Person < ActiveRecord::Base ...@@ -63,7 +64,7 @@ class Person < ActiveRecord::Base
p p
end end
def self.search_query_string(query) def self.search_query_string(query)
query = query.downcase query = query.downcase
......
...@@ -12,6 +12,25 @@ describe Person do ...@@ -12,6 +12,25 @@ describe Person do
end end
context 'scopes' do context 'scopes' do
describe '.for_json' do
it 'does not select public keys' do
proc {
Person.for_json.first.serialized_public_key
}.should raise_error ActiveModel::MissingAttributeError
end
it 'eager loads profiles' do
Person.for_json.first.loaded_profile?.should be_true
end
it 'selects distinct people' do
aspect = bob.aspects.create(:name => 'hilarious people')
aspect.contacts << bob.contact_for(eve.person)
person_ids = Person.for_json.joins(:contacts => :aspect_memberships).
where(:contacts => {:user_id => bob.id},
:aspect_memberships => {:aspect_id => bob.aspect_ids}).map{|p| p.id}
person_ids.uniq.should == person_ids
end
end
describe '.local' do describe '.local' do
it 'returns only local people' do it 'returns only local people' do
Person.local =~ [@person] Person.local =~ [@person]
...@@ -28,7 +47,7 @@ describe Person do ...@@ -28,7 +47,7 @@ describe Person do
it 'searchs for a person if id is passed' do it 'searchs for a person if id is passed' do
Person.find_from_id_or_username(:id => @person.id).id.should == @person.id Person.find_from_id_or_username(:id => @person.id).id.should == @person.id
end end
it 'searchs a person from a user if username is passed' do it 'searchs a person from a user if username is passed' do
Person.find_from_id_or_username(:username => @user.username).id.should == @user.person.id Person.find_from_id_or_username(:username => @user.username).id.should == @user.person.id
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