From 23aff2af5e403ab040b27d7a4e68792316c1b7a1 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Sat, 4 Feb 2012 22:49:25 -0500 Subject: [PATCH] run second search ajaxily when no results are found from remote pod. --- app/controllers/people_controller.rb | 17 +++++++++++++ app/views/people/index.html.haml | 10 +++++--- config/locales/diaspora/en.yml | 1 + config/routes.rb | 1 + public/javascripts/pages/contacts-index.js | 18 ++++++++++++++ spec/controllers/people_controller_spec.rb | 29 ++++++++++++++++++++++ 6 files changed, 73 insertions(+), 3 deletions(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 4abf12a75..c3f45c095 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -35,6 +35,7 @@ class PeopleController < ApplicationController if diaspora_id?(search_query) @people = Person.where(:diaspora_handle => search_query.downcase) Webfinger.in_background(search_query) if @people.empty? + @background_query = search_query end @people = @people.paginate(:page => params[:page], :per_page => 15) @hashes = hashes_for_people(@people, @aspects) @@ -42,6 +43,22 @@ class PeopleController < ApplicationController 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 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) diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index e6e4eb048..651251ff2 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -18,12 +18,16 @@ .span-15.append-1 #people_stream.stream - if @hashes.empty? - %p + %p#not_found{:class => @background_query.nil? ? "" : "hidden" } =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 - for hash in @hashes - = render :partial => 'people/person', :locals => hash + = render :partial => 'people/person.html', :locals => hash = will_paginate @people diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 5395aff1f..dc8661697 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -536,6 +536,7 @@ en: no_results: "Hey! You need to search for something." couldnt_find_them_send_invite: "Couldn't find them? Send an invite!" no_one_found: "...and no one was found." + searching: "searching, please be patient..." looking_for: "Looking for posts tagged %{tag_link}?" webfinger: fail: "Sorry, we couldn't find %{handle}." diff --git a/config/routes.rb b/config/routes.rb index cdeff6936..44e6aa3b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -124,6 +124,7 @@ Diaspora::Application.routes.draw do get 'community_spotlight' => "contacts#spotlight", :as => 'community_spotlight' + get 'people/refresh_search' => "people#refresh_search" resources :people, :except => [:edit, :update] do resources :status_messages resources :photos diff --git a/public/javascripts/pages/contacts-index.js b/public/javascripts/pages/contacts-index.js index 0e915097a..2c55b1494 100644 --- a/public/javascripts/pages/contacts-index.js +++ b/public/javascripts/pages/contacts-index.js @@ -6,3 +6,21 @@ Diaspora.Pages.ContactsIndex = function() { $('.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' ); + } +} \ No newline at end of file diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index ea1f832e3..6a3da53b8 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -351,6 +351,35 @@ describe PeopleController do 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 it 'assigns the contacts of a person' do contact = alice.contact_for(bob.person)