From 23aff2af5e403ab040b27d7a4e68792316c1b7a1 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Sat, 4 Feb 2012 22:49:25 -0500 Subject: [PATCH 1/9] 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) From 8344422aef067bb4f9accc03cc8e910d936806ab Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Sat, 4 Feb 2012 22:52:05 -0500 Subject: [PATCH 2/9] partial rename no longer needed --- app/views/people/index.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index 651251ff2..67e31715b 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -27,7 +27,7 @@ $(document).ready(function() { setTimeout("runDelayedSearch('#{@background_query}')", 10000); }); - else - for hash in @hashes - = render :partial => 'people/person.html', :locals => hash + = render :partial => 'people/person', :locals => hash = will_paginate @people From 7048fa6d505b102cd74cc59842ac39f05bf19f06 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Wed, 7 Mar 2012 20:57:26 -0500 Subject: [PATCH 3/9] fixing conflicts in merge --- app/controllers/people_controller.rb | 13 +++++++------ app/views/people/index.html.haml | 11 ++++++----- config/locales/javascript/javascript.en.yml | 6 ++++-- public/javascripts/pages/contacts-index.js | 3 +-- .../jasmine_fixtures/people.spec.rb | 18 ++++++++++++++++++ spec/controllers/people_controller_spec.rb | 1 - 6 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 spec/controllers/jasmine_fixtures/people.spec.rb diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index c3f45c095..840ce0bc3 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -46,15 +46,16 @@ class PeopleController < ApplicationController 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 = "" + unless @people.empty? + @people = @people.paginate(:page => params[:page], :per_page => 15) + @hashes = hashes_for_people(@people, @aspects) - self.formats = self.formats + [:html] - @hashes.each do |hash| - @answer_html += render_to_string :partial => 'people/person', :locals => hash + self.formats = self.formats + [:html] + @hashes.each do |hash| + @answer_html += render_to_string :partial => 'people/person', :locals => hash + end end - render :json => { :search_count => @people.count, :search_html => @answer_html }.to_json end diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index 67e31715b..f01484c65 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -18,13 +18,14 @@ .span-15.append-1 #people_stream.stream - if @hashes.empty? - %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? + - if @background_query.present? + %p + =t('.searching') :javascript $(document).ready(function() { setTimeout("runDelayedSearch('#{@background_query}')", 10000); }); + - else + %p + =t('.no_one_found') - else - for hash in @hashes = render :partial => 'people/person', :locals => hash diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index f0cba1913..a04f3e186 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -29,8 +29,8 @@ en: my_stream: "Stream" videos: - watch: "Watch this video on <%= provider %>" - unknown: "Unknown video type" + watch: "Watch this video on <%= provider %>" + unknown: "Unknown video type" search_for: "Search for <%= name %>" publisher: at_least_one_aspect: "You must publish to at least one aspect" @@ -73,6 +73,8 @@ en: looking_good: "OMG, you look awesome!" tags: wasnt_that_interesting: "OK, I suppose #<%= tagName %> wasn't all that interesting..." + people: + not_found: "and no one was found..." stream: hide: "Hide" diff --git a/public/javascripts/pages/contacts-index.js b/public/javascripts/pages/contacts-index.js index 2c55b1494..d75ac9b66 100644 --- a/public/javascripts/pages/contacts-index.js +++ b/public/javascripts/pages/contacts-index.js @@ -20,7 +20,6 @@ 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' ); + $("#people_stream.stream").html( "

" + Diaspora.I18n.t("people.not_found") + "

" ); } } \ No newline at end of file diff --git a/spec/controllers/jasmine_fixtures/people.spec.rb b/spec/controllers/jasmine_fixtures/people.spec.rb new file mode 100644 index 000000000..70ee513d3 --- /dev/null +++ b/spec/controllers/jasmine_fixtures/people.spec.rb @@ -0,0 +1,18 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require 'spec_helper' + +describe PeopleController do + describe '#index' do + before do + sign_in :user, bob + end + + it "generates a jasmine fixture", :fixture => true do + get :index + save_fixture(html_for("body"), "empty_people_search") + end + end +end diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index 6a3da53b8..a03b8e86a 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -373,7 +373,6 @@ describe PeopleController do 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 From ac8cd8368effabbf1cbf6788b07327e0d0f5e451 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Wed, 7 Mar 2012 21:01:15 -0500 Subject: [PATCH 4/9] fixing conflicts in merge --- spec/javascripts/aspect-spec.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 spec/javascripts/aspect-spec.js diff --git a/spec/javascripts/aspect-spec.js b/spec/javascripts/aspect-spec.js new file mode 100644 index 000000000..36dac1f17 --- /dev/null +++ b/spec/javascripts/aspect-spec.js @@ -0,0 +1,12 @@ +describe("Diaspora.Aspects", function() { + beforeEach(function() { + spec.loadFixture("aspects_index"); + }); + + + describe("edit aspect name", function() { + it("it should ", function() { + var startName = $("a [data-aspect_id=13]").text(); + }); + }); +}); From 7d3a6a061c97b604a08350aff3c7d8f9b9939ca5 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Thu, 16 Feb 2012 07:03:20 -0500 Subject: [PATCH 5/9] add spec to ake sure background query is only set when a full diaspora id is given. --- app/controllers/people_controller.rb | 2 +- spec/controllers/people_controller_spec.rb | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 840ce0bc3..4a0c511fc 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -35,7 +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 + @background_query = search_query.downcase end @people = @people.paginate(:page => params[:page], :per_page => 15) @hashes = hashes_for_people(@people, @aspects) diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index a03b8e86a..d195a1442 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -52,6 +52,11 @@ describe PeopleController do get :index, :q => "Eugene@Example.ORG" assigns[:people][0].id.should == @unsearchable_eugene.id end + + it 'sets the background query task' do + get :index, :q => "Eugene@Example.ORG" + assigns[:background_query].should == "eugene@example.org" + end end context 'query is a tag' do @@ -77,6 +82,11 @@ describe PeopleController do assigns[:hashes].should_not be_nil end + it 'does not set the background query task' do + get :index, :q => "Korth" + assigns[:background_query].should_not be_present + end + it "assigns people" do eugene2 = Factory(:person, :profile => Factory.build(:profile, :first_name => "Eugene", From ee237c6c5f1f82f1c18bc5125b32b0b37d51c9b2 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Thu, 16 Feb 2012 07:36:03 -0500 Subject: [PATCH 6/9] jasmine spec for stimulating deferred searches --- .../jasmine_fixtures/people.spec.rb | 4 +++ spec/javascripts/search-spec.js | 34 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 spec/javascripts/search-spec.js diff --git a/spec/controllers/jasmine_fixtures/people.spec.rb b/spec/controllers/jasmine_fixtures/people.spec.rb index 70ee513d3..bdcaebba6 100644 --- a/spec/controllers/jasmine_fixtures/people.spec.rb +++ b/spec/controllers/jasmine_fixtures/people.spec.rb @@ -14,5 +14,9 @@ describe PeopleController do get :index save_fixture(html_for("body"), "empty_people_search") end + it "generates a jasmine fixture", :fixture => true do + get :index, :id => "sample@diaspor.us" + save_fixture(html_for("body"), "pending_external_people_search") + end end end diff --git a/spec/javascripts/search-spec.js b/spec/javascripts/search-spec.js new file mode 100644 index 000000000..309ce8309 --- /dev/null +++ b/spec/javascripts/search-spec.js @@ -0,0 +1,34 @@ +/* Copyright (c) 2010-2011, Diaspora Inc. This file is +* licensed under the Affero General Public License version 3 or later. See +* the COPYRIGHT file. +*/ + +describe("Publisher", function() { + + describe("runDelayedSearch", function() { + beforeEach( function(){ + spec.loadFixture('pending_external_people_search'); + Publisher.open(); + }); + + it('gets called on initialize', function(){ + spyOn(Publisher, 'runDelayedSearch'); + Publisher.initialize(); + expect(Publisher.runDelayedSearch).toHaveBeenCalled(); + }); + }); + + describe("runDelayedSearch", function() { + beforeEach( function(){ + spec.loadFixture('empty_people_search'); + Publisher.open(); + }); + + it('inserts contact html', function(){ + Publisher.initialize(); + Publisher.handleSearchRefresh( "
hello
"); + expect($(".testing_insert_div").text().toEqual( "hello" )); + + }); + }); +}); From 9de477cf5a116ea457e34311a756aa071afef919 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Thu, 16 Feb 2012 16:49:50 -0500 Subject: [PATCH 7/9] jasmine spec up and working. code out of (no longer called) contacts info page object. Fixed namespaces and created jasmine fixture for empty and pending searches. --- app/views/people/index.html.haml | 2 +- public/javascripts/contact-list.js | 19 +++++++++++++++++++ public/javascripts/pages/contacts-index.js | 18 +----------------- .../{people.spec.rb => people_spec.rb} | 6 +++--- spec/javascripts/search-spec.js | 15 ++++++--------- 5 files changed, 30 insertions(+), 30 deletions(-) rename spec/controllers/jasmine_fixtures/{people.spec.rb => people_spec.rb} (69%) diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index f01484c65..277c0629c 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -22,7 +22,7 @@ %p =t('.searching') :javascript - $(document).ready(function() { setTimeout("runDelayedSearch('#{@background_query}')", 10000); }); + $(document).ready(function() { List.startSearchDelay('#{@background_query}') } ); - else %p =t('.no_one_found') diff --git a/public/javascripts/contact-list.js b/public/javascripts/contact-list.js index 17472e762..9321dc4b0 100644 --- a/public/javascripts/contact-list.js +++ b/public/javascripts/contact-list.js @@ -32,7 +32,26 @@ var List = { }; } }); + }, + runDelayedSearch: function( searchTerm ) { + $.ajax({ + dataType: 'json', + url: '/people/refresh_search', + data: { q: searchTerm }, + success: List.handleSearchRefresh + }); + }, + handleSearchRefresh: function( data ) { + if ( data.search_count > 0 ) { + $("#people_stream.stream").html( data.search_html ); + } else { + $("#people_stream.stream").html( "

" + Diaspora.I18n.t("people.not_found") + "

" ); + } + }, + startSearchDelay: function ( theSearch ) { + setTimeout( "List.runDelayedSearch('" + theSearch + "')", 10000); } + }; $(document).ready(function() { diff --git a/public/javascripts/pages/contacts-index.js b/public/javascripts/pages/contacts-index.js index d75ac9b66..ac2e37eeb 100644 --- a/public/javascripts/pages/contacts-index.js +++ b/public/javascripts/pages/contacts-index.js @@ -5,21 +5,5 @@ Diaspora.Pages.ContactsIndex = function() { self.infiniteScroll = self.instantiate("InfiniteScroll"); $('.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 { - $("#people_stream.stream").html( "

" + Diaspora.I18n.t("people.not_found") + "

" ); - } -} \ No newline at end of file diff --git a/spec/controllers/jasmine_fixtures/people.spec.rb b/spec/controllers/jasmine_fixtures/people_spec.rb similarity index 69% rename from spec/controllers/jasmine_fixtures/people.spec.rb rename to spec/controllers/jasmine_fixtures/people_spec.rb index bdcaebba6..8216656ff 100644 --- a/spec/controllers/jasmine_fixtures/people.spec.rb +++ b/spec/controllers/jasmine_fixtures/people_spec.rb @@ -10,12 +10,12 @@ describe PeopleController do sign_in :user, bob end - it "generates a jasmine fixture", :fixture => true do + it "generates a jasmine fixture with no query", :fixture => true do get :index save_fixture(html_for("body"), "empty_people_search") end - it "generates a jasmine fixture", :fixture => true do - get :index, :id => "sample@diaspor.us" + it "generates a jasmine fixture trying an external search", :fixture => true do + get :index, :q => "sample@diaspor.us" save_fixture(html_for("body"), "pending_external_people_search") end end diff --git a/spec/javascripts/search-spec.js b/spec/javascripts/search-spec.js index 309ce8309..7a6f9751f 100644 --- a/spec/javascripts/search-spec.js +++ b/spec/javascripts/search-spec.js @@ -3,30 +3,27 @@ * the COPYRIGHT file. */ -describe("Publisher", function() { +describe("List", function() { describe("runDelayedSearch", function() { beforeEach( function(){ - spec.loadFixture('pending_external_people_search'); - Publisher.open(); }); it('gets called on initialize', function(){ - spyOn(Publisher, 'runDelayedSearch'); - Publisher.initialize(); - expect(Publisher.runDelayedSearch).toHaveBeenCalled(); + spyOn( List, 'startSearchDelay'); + spec.loadFixture('pending_external_people_search'); + expect(List.startSearchDelay).toHaveBeenCalled(); }); }); describe("runDelayedSearch", function() { beforeEach( function(){ spec.loadFixture('empty_people_search'); - Publisher.open(); + List.initialize(); }); it('inserts contact html', function(){ - Publisher.initialize(); - Publisher.handleSearchRefresh( "
hello
"); + List.handleSearchRefresh( { count:1,search_html: '
hello
' } ); expect($(".testing_insert_div").text().toEqual( "hello" )); }); From d84b0c58bdda93af3f6b44181b3f2fc6657e61d4 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Wed, 7 Mar 2012 00:23:28 -0500 Subject: [PATCH 8/9] simplify search results processing since only one person can be returned --- app/controllers/people_controller.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 4a0c511fc..a83e34119 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -48,13 +48,10 @@ class PeopleController < ApplicationController @people = Person.where(:diaspora_handle => search_query.downcase) @answer_html = "" unless @people.empty? - @people = @people.paginate(:page => params[:page], :per_page => 15) @hashes = hashes_for_people(@people, @aspects) self.formats = self.formats + [:html] - @hashes.each do |hash| - @answer_html += render_to_string :partial => 'people/person', :locals => hash - end + @answer_html = render_to_string :partial => 'people/person', :locals => @hashes.first end render :json => { :search_count => @people.count, :search_html => @answer_html }.to_json end From 73fd96d439240685030f8f13b9a95b5195b9ff92 Mon Sep 17 00:00:00 2001 From: Steven Fuchs Date: Sat, 10 Mar 2012 14:21:13 -0500 Subject: [PATCH 9/9] this file should have never been added... --- spec/javascripts/aspect-spec.js | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 spec/javascripts/aspect-spec.js diff --git a/spec/javascripts/aspect-spec.js b/spec/javascripts/aspect-spec.js deleted file mode 100644 index 36dac1f17..000000000 --- a/spec/javascripts/aspect-spec.js +++ /dev/null @@ -1,12 +0,0 @@ -describe("Diaspora.Aspects", function() { - beforeEach(function() { - spec.loadFixture("aspects_index"); - }); - - - describe("edit aspect name", function() { - it("it should ", function() { - var startName = $("a [data-aspect_id=13]").text(); - }); - }); -});