diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 0c88f0ef576491a55bd16d2db3db7498cf5da4e8..3c8adf034f78cbc4961dbbaae6be8d42a3c4172f 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -46,6 +46,7 @@ class PeopleController < ApplicationController else people = Person.search(params[:q], current_user) end + @normalized_tag_for_query = ActsAsTaggableOn::Tag.normalize(params[:q]) @people = people.paginate( :page => params[:page], :per_page => 15) @hashes = hashes_for_people(@people, @aspects) end diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb deleted file mode 100644 index 8e4cabd74afe721a62ca35d2d0c88c8ff4e06e63..0000000000000000000000000000000000000000 --- a/app/helpers/tags_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -# Copyright (c) 2010-2011, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -module TagsHelper - def tag_page_link(tag) - tag_name = ActsAsTaggableOn::Tag.normalize(tag) - link_to("##{tag_name}", tag_path(:name => tag_name)) - end -end diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index 3b2e45411adf7f6220ea608608da5ec453501f43..a695b6dfd5395f2e30cd35d5946840fb0926d428 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -18,9 +18,10 @@ =t('.results_for') %span.term = params[:q] - %h4 - %small - = t('.looking_for', :tag_link => tag_page_link(params[:q])).html_safe + - if @normalized_tag_for_query.present? + %h4 + %small + = t('.looking_for', :tag_link => link_to("##{@normalized_tag_for_query}", tag_path(:name => @normalized_tag_for_query))).html_safe .span-15 %hr .clearfix diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index fe5e0424ec904383b102c4dd04b6460e6c8d74be..e895286a3b6ce43988b9f8c7478b1c00d2d56051 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -14,92 +14,114 @@ describe PeopleController do describe '#index (search)' do before do @eugene = Factory(:person, - :profile => Factory.build(:profile, :first_name => "Eugene", :last_name => "w")) + :profile => Factory.build(:profile, :first_name => "Eugene", :last_name => "w")) @korth = Factory(:person, - :profile => Factory.build(:profile, :first_name => "Evan", :last_name => "Korth")) + :profile => Factory.build(:profile, :first_name => "Evan", :last_name => "Korth")) end - it 'responds with json' do - get :index, :q => "Korth", :format => 'json' - response.body.should == [@korth].to_json - end + describe 'via json' do + it 'succeeds' do + get :index, :q => "Korth", :format => 'json' + response.should be_success + end - it 'does not set @hashes in a json request' do - get :index, :q => "Korth", :format => 'json' - assigns[:hashes].should be_nil - end + it 'responds with json' do + get :index, :q => "Korth", :format => 'json' + response.body.should == [@korth].to_json + end - it 'sets @hashes in an html request' do - get :index, :q => "Korth" - assigns[:hashes].should_not be_nil + it 'does not assign hashes' do + get :index, :q => "Korth", :format => 'json' + assigns[:hashes].should be_nil + end end - it "assigns people" do - eugene2 = Factory(:person, - :profile => Factory.build(:profile, :first_name => "Eugene", - :last_name => "w")) - get :index, :q => "Eug" - assigns[:people].map{|x| x.id}.should =~ [@eugene.id, eugene2.id] - end + describe 'via html' do + context 'query is a diaspora ID' do + before do + @unsearchable_eugene = Factory(:person, :diaspora_handle => "eugene@example.org", + :profile => Factory.build(:profile, :first_name => "Eugene", + :last_name => "w", :searchable => false)) + end + it 'finds people even if they have searchable off' do + get :index, :q => "eugene@example.org" + assigns[:people][0].id.should == @unsearchable_eugene.id + end - it "excludes people that are not searchable" do - eugene2 = Factory(:person, - :profile => Factory.build(:profile, :first_name => "Eugene", - :last_name => "w", :searchable => false)) - get :index, :q => "Eug" - assigns[:people].should_not =~ [eugene2] - end + it 'downcases the query term' do + get :index, :q => "Eugene@Example.ORG" + assigns[:people][0].id.should == @unsearchable_eugene.id + end + end - it "allows unsearchable people to be found by handle" do - eugene2 = Factory(:person, :diaspora_handle => "eugene@example.org", - :profile => Factory.build(:profile, :first_name => "Eugene", - :last_name => "w", :searchable => false)) - get :index, :q => "eugene@example.org" - assigns[:people][0].id.should == eugene2.id - end + context 'query is a tag' do + it 'goes to a tag page' do + get :index, :q => '#babies' + response.should redirect_to(tag_path('babies', :q => '#babies')) + end - it "allows unsearchable people to be found by handle" do - d_id = "eugene@example.org" - @controller.should_receive(:diaspora_id?).with(d_id) - get :index, :q => d_id - end + it 'removes dots from the query' do + get :index, :q => '#babi.es' + response.should redirect_to(tag_path('babies', :q => '#babi.es')) + end - it "downcases the handle before trying to find someone by it" do - eugene2 = Factory(:person, :diaspora_handle => "eugene@example.org", - :profile => Factory.build(:profile, :first_name => "Eugene", - :last_name => "w", :searchable => false)) - get :index, :q => "Eugene@Example.ORG" - assigns[:people][0].id.should == eugene2.id - end + it 'stay on the page if you search for the empty hash' do + get :index, :q => '#' + flash[:error].should be_present + end + end - it "does not redirect to person page if there is exactly one match" do - get :index, :q => "Korth" - response.should_not redirect_to @korth - end + context 'query is not a tag or a diaspora ID' do + it 'assigns hashes' do + get :index, :q => "Korth" + assigns[:hashes].should_not be_nil + end - it "does not redirect if there are no matches" do - get :index, :q => "Korthsauce" - response.should_not be_redirect - end + it "assigns people" do + eugene2 = Factory(:person, + :profile => Factory.build(:profile, :first_name => "Eugene", + :last_name => "w")) + get :index, :q => "Eug" + assigns[:people].map { |x| x.id }.should =~ [@eugene.id, eugene2.id] + end + + it "assigns a normalized tag" do + get :index, :q => "foo" + assigns[:normalized_tag_for_query].should == "foo" + end - it 'goes to a tag page if you search for a hash' do - get :index, :q => '#babies' - response.should redirect_to(tag_path('babies', :q => '#babies')) - end + it "succeeds if there is exactly one match" do + get :index, :q => "Korth" + assigns[:people].length.should == 1 + response.should be_success + end - it 'goes to a tag page if you search for a hash with dots' do - get :index, :q => '#babi.es' - response.should redirect_to(tag_path('babies', :q => '#babi.es')) - end + it "succeeds if there are no matches" do + get :index, :q => "Korthsauce" + assigns[:people].length.should == 0 + response.should be_success + end - it 'stay on the page if you search for the empty hash' do - get :index, :q => '#' - flash[:error].should be_present - end + it 'succeeds if you search for the empty term' do + get :index, :q => '' + assigns[:normalized_tag_for_query].should be_empty + response.should be_success + end - it 'does not fails if you search for the empty term' do - get :index, :q => '' - response.should be_success + it 'succeeds if you search for punctuation' do + get :index, :q => '+' + assigns[:normalized_tag_for_query].should be_empty + response.should be_success + end + + it "excludes people who have searchable off" do + eugene2 = Factory(:person, + :profile => Factory.build(:profile, :first_name => "Eugene", + :last_name => "w", :searchable => false)) + get :index, :q => "Eug" + assigns[:people].should_not =~ [eugene2] + end + end end end @@ -243,7 +265,7 @@ describe PeopleController do it "posts include reshares" do reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects) get :show, :id => @user.person.to_param - assigns[:stream].posts.map{|x| x.id}.should include(reshare.id) + assigns[:stream].posts.map { |x| x.id }.should include(reshare.id) end it "assigns only public posts" do @@ -253,7 +275,7 @@ describe PeopleController do it 'is sorted by created_at desc' do get :show, :id => @person.to_param - assigns[:stream].stream_posts.should == @public_posts.sort_by{|p| p.created_at}.reverse + assigns[:stream].stream_posts.should == @public_posts.sort_by { |p| p.created_at }.reverse end end @@ -298,7 +320,7 @@ describe PeopleController do it "posts include reshares" do reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects) get :show, :id => @user.person.to_param - assigns[:stream].posts.map{|x| x.id}.should include(reshare.id) + assigns[:stream].posts.map { |x| x.id }.should include(reshare.id) end end @@ -331,7 +353,7 @@ describe PeopleController do it "posts include reshares" do reshare = @user.post(:reshare, :public => true, :root_guid => Factory(:status_message, :public => true).guid, :to => alice.aspects) get :show, :id => @user.person.to_param - assigns[:stream].posts.map{|x| x.id}.should include(reshare.id) + assigns[:stream].posts.map { |x| x.id }.should include(reshare.id) end end end