Refactor peoplecontroller index
This commit is contained in:
parent
1f71474f0f
commit
32a2264af5
4 changed files with 74 additions and 20 deletions
|
|
@ -12,19 +12,33 @@ class PeopleController < ApplicationController
|
|||
@aspect = :search
|
||||
|
||||
@people = Person.search(params[:q]).paginate :page => params[:page], :per_page => 25, :order => 'created_at DESC'
|
||||
@requests = Request.all(:to_id.in => @people.map{|p| p.id}, :from_id => current_user.person.id)
|
||||
|
||||
if @people.count == 1
|
||||
redirect_to @people.first
|
||||
else
|
||||
@hashes = hashes_for_people(@people, @aspects)
|
||||
#only do it if it is an email address
|
||||
if params[:q].try(:match, Devise.email_regexp)
|
||||
webfinger(params[:q])
|
||||
end
|
||||
|
||||
if @people.count == 1
|
||||
redirect_to @people.first
|
||||
else
|
||||
respond_with @people
|
||||
end
|
||||
end
|
||||
def hashes_for_people people, aspects
|
||||
ids = people.map{|p| p.id}
|
||||
requests = {}
|
||||
Request.all(:to_id.in => ids, :from_id => current_user.person.id).each do |r|
|
||||
requests[r.to_id] = r
|
||||
end
|
||||
contacts = {}
|
||||
Contact.all(:user_id => current_user.id, :person_id.in => ids).each do |contact|
|
||||
contacts[contact.person_id] = contact
|
||||
end
|
||||
people.map{|p|
|
||||
{:person => p,
|
||||
:contact => contacts[p.id],
|
||||
:request => requests[p.id],
|
||||
:aspects => aspects}
|
||||
}
|
||||
end
|
||||
|
||||
def show
|
||||
@person = Person.find(params[:id].to_id)
|
||||
|
|
|
|||
|
|
@ -8,11 +8,7 @@
|
|||
%u= params[:q]
|
||||
|
||||
%ul{:class => 'stream people', :id => 'people_stream'}
|
||||
- for person in @people
|
||||
= render 'people/person',
|
||||
:person => person,
|
||||
:aspects => @aspects,
|
||||
:contact => @contacts.detect{|contact| contact.person_id == person.id},
|
||||
:request => @requests.detect{|request| request.to_id == person.id}
|
||||
- for hash in @hashes
|
||||
= render :partial => 'people/person', :locals => hash
|
||||
|
||||
= will_paginate @people
|
||||
|
|
|
|||
|
|
@ -14,22 +14,66 @@ describe PeopleController do
|
|||
sign_in :user, user
|
||||
end
|
||||
|
||||
describe '#hashes_from_people' do
|
||||
before do
|
||||
@everyone = []
|
||||
10.times do
|
||||
@everyone << Factory.create(:person)
|
||||
end
|
||||
|
||||
user.send_contact_request_to(@everyone[3], aspect)
|
||||
user.send_contact_request_to(@everyone[2], aspect)
|
||||
user.activate_contact(@everyone[4], aspect)
|
||||
user.activate_contact(@everyone[5], aspect)
|
||||
|
||||
@people = Person.search('eugene')
|
||||
@people.length.should == 10
|
||||
@hashes = @controller.hashes_for_people(@people, user.aspects)
|
||||
end
|
||||
it 'has the correct result for no relationship' do
|
||||
hash = @hashes.first
|
||||
hash[:person].should == @people.first
|
||||
hash[:contact].should be_false
|
||||
hash[:request].should be_false
|
||||
hash[:aspects].should == user.aspects
|
||||
end
|
||||
it 'has the correct result for a connected person' do
|
||||
hash = @hashes[4]
|
||||
hash[:person].should == @people[4]
|
||||
hash[:contact].should be_true
|
||||
hash[:request].should be_false
|
||||
hash[:aspects].should == user.aspects
|
||||
end
|
||||
it 'has the correct result for a requested person' do
|
||||
hash = @hashes[2]
|
||||
hash[:person].should == @people[2]
|
||||
hash[:contact].should be_false
|
||||
hash[:request].should be_true
|
||||
hash[:aspects].should == user.aspects
|
||||
end
|
||||
end
|
||||
describe '#index' do
|
||||
before do
|
||||
@eugene = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
|
||||
@korth = Factory.create(:person, :profile => {:first_name => "Evan", :last_name => "Korth"})
|
||||
end
|
||||
|
||||
it "yields search results for substring of person name" do
|
||||
it "assigns hashes" do
|
||||
eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
|
||||
get :index, :q => "Eu"
|
||||
assigns[:people].should include @eugene
|
||||
assigns[:hashes][0][:person].should == @eugene
|
||||
assigns[:hashes][0][:person].should == eugene2
|
||||
end
|
||||
it "assigns people" do
|
||||
eugene2 = Factory.create(:person, :profile => {:first_name => "Eugene", :last_name => "w"})
|
||||
get :index, :q => "Eu"
|
||||
assigns[:people].should == [@eugene, eugene2]
|
||||
end
|
||||
|
||||
it 'shows a contact' do
|
||||
user2 = make_user
|
||||
connect_users(user, aspect, user2, user2.aspects.create(:name => 'Neuroscience'))
|
||||
get :index, :q => user2.person.profile.first_name.to_s
|
||||
assigns[:people].should include user2.person
|
||||
response.should redirect_to user2.person
|
||||
end
|
||||
|
||||
it 'shows a non-contact' do
|
||||
|
|
@ -37,7 +81,7 @@ describe PeopleController do
|
|||
user2.person.profile.searchable = true
|
||||
user2.save
|
||||
get :index, :q => user2.person.profile.first_name.to_s
|
||||
assigns[:people].should include user2.person
|
||||
response.should redirect_to user2.person
|
||||
end
|
||||
|
||||
it "redirects to person page if there is exactly one match" do
|
||||
|
|
|
|||
|
|
@ -140,7 +140,7 @@ describe Person do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#search' do
|
||||
describe '.search' do
|
||||
before do
|
||||
@connected_person_one = Factory.create(:person)
|
||||
@connected_person_two = Factory.create(:person)
|
||||
|
|
|
|||
Loading…
Reference in a new issue