run second search ajaxily when no results are found from remote pod.

This commit is contained in:
Steven Fuchs 2012-02-04 22:49:25 -05:00
parent 9b08f3f69a
commit 23aff2af5e
6 changed files with 73 additions and 3 deletions

View file

@ -35,6 +35,7 @@ class PeopleController < ApplicationController
if diaspora_id?(search_query) if diaspora_id?(search_query)
@people = Person.where(:diaspora_handle => search_query.downcase) @people = Person.where(:diaspora_handle => search_query.downcase)
Webfinger.in_background(search_query) if @people.empty? Webfinger.in_background(search_query) if @people.empty?
@background_query = search_query
end end
@people = @people.paginate(:page => params[:page], :per_page => 15) @people = @people.paginate(:page => params[:page], :per_page => 15)
@hashes = hashes_for_people(@people, @aspects) @hashes = hashes_for_people(@people, @aspects)
@ -42,6 +43,22 @@ class PeopleController < ApplicationController
end end
end end
def refresh_search
@aspect = :search
@people = Person.where(:diaspora_handle => search_query.downcase)
@people = @people.paginate(:page => params[:page], :per_page => 15)
@hashes = hashes_for_people(@people, @aspects)
@answer_html = ""
self.formats = self.formats + [:html]
@hashes.each do |hash|
@answer_html += render_to_string :partial => 'people/person', :locals => hash
end
render :json => { :search_count => @people.count, :search_html => @answer_html }.to_json
end
def tag_index def tag_index
profiles = Profile.tagged_with(params[:name]).where(:searchable => true).select('profiles.id, profiles.person_id') profiles = Profile.tagged_with(params[:name]).where(:searchable => true).select('profiles.id, profiles.person_id')
@people = Person.where(:id => profiles.map{|p| p.person_id}).paginate(:page => params[:page], :per_page => 15) @people = Person.where(:id => profiles.map{|p| p.person_id}).paginate(:page => params[:page], :per_page => 15)

View file

@ -18,12 +18,16 @@
.span-15.append-1 .span-15.append-1
#people_stream.stream #people_stream.stream
- if @hashes.empty? - if @hashes.empty?
%p %p#not_found{:class => @background_query.nil? ? "" : "hidden" }
=t('.no_one_found') =t('.no_one_found')
%p#searching{:class => @background_query.nil? ? "hidden" : "" }
=t('.searching')
- if ! @background_query.nil?
:javascript
$(document).ready(function() { setTimeout("runDelayedSearch('#{@background_query}')", 10000); });
- else - else
- for hash in @hashes - for hash in @hashes
= render :partial => 'people/person', :locals => hash = render :partial => 'people/person.html', :locals => hash
= will_paginate @people = will_paginate @people

View file

@ -536,6 +536,7 @@ en:
no_results: "Hey! You need to search for something." no_results: "Hey! You need to search for something."
couldnt_find_them_send_invite: "Couldn't find them? Send an invite!" couldnt_find_them_send_invite: "Couldn't find them? Send an invite!"
no_one_found: "...and no one was found." no_one_found: "...and no one was found."
searching: "searching, please be patient..."
looking_for: "Looking for posts tagged %{tag_link}?" looking_for: "Looking for posts tagged %{tag_link}?"
webfinger: webfinger:
fail: "Sorry, we couldn't find %{handle}." fail: "Sorry, we couldn't find %{handle}."

View file

@ -124,6 +124,7 @@ Diaspora::Application.routes.draw do
get 'community_spotlight' => "contacts#spotlight", :as => 'community_spotlight' get 'community_spotlight' => "contacts#spotlight", :as => 'community_spotlight'
get 'people/refresh_search' => "people#refresh_search"
resources :people, :except => [:edit, :update] do resources :people, :except => [:edit, :update] do
resources :status_messages resources :status_messages
resources :photos resources :photos

View file

@ -6,3 +6,21 @@ Diaspora.Pages.ContactsIndex = function() {
$('.conversation_button').twipsy({position: 'below'}); $('.conversation_button').twipsy({position: 'below'});
}); });
}; };
function runDelayedSearch( searchTerm ) {
$.ajax({
dataType: 'json',
url: '/people/refresh_search',
data: { q: searchTerm },
success: handleSearchRefresh
});
}
function handleSearchRefresh(data) {
if ( data.search_count > 0 ) {
$("#people_stream.stream").html( data.search_html );
} else {
$("p#not_found").removeClass( 'hidden' );
$("p#searching").addClass( 'hidden' );
}
}

View file

@ -351,6 +351,35 @@ describe PeopleController do
end end
end end
describe '#refresh_search ' do
before(:each)do
@eugene = Factory(:person,
:profile => Factory.build(:profile, :first_name => "Eugene", :last_name => "w"))
@korth = Factory(:person,
:profile => Factory.build(:profile, :first_name => "Evan", :last_name => "Korth"))
end
describe 'via json' do
it 'returns a zero count when a search fails' do
get :refresh_search, :q => "weweweKorth", :format => 'json'
response.body.should == {:search_count=>0, :search_html=>""}.to_json
end
it 'returns with a zero count unless a fully composed name is sent' do
get :refresh_search, :q => "Korth"
response.body.should == {:search_count=>0, :search_html=>""}.to_json
end
it 'returns with a found name' do
get :refresh_search, :q => @korth.diaspora_handle
puts JSON.parse( response.body ).inspect
JSON.parse( response.body )["search_count"].should == 1
end
end
end
describe '#contacts' do describe '#contacts' do
it 'assigns the contacts of a person' do it 'assigns the contacts of a person' do
contact = alice.contact_for(bob.person) contact = alice.contact_for(bob.person)