diff --git a/app/controllers/people_controller.rb b/app/controllers/people_controller.rb index 3c8adf034f78cbc4961dbbaae6be8d42a3c4172f..3787749bfe2e14886c55b5827ffaeab3cf27e96d 100644 --- a/app/controllers/people_controller.rb +++ b/app/controllers/people_controller.rb @@ -6,6 +6,7 @@ require File.join(Rails.root, "lib", 'stream', "person") class PeopleController < ApplicationController before_filter :authenticate_user!, :except => [:show] + before_filter :redirect_if_tag_search, :only => [:index] respond_to :html, :except => [:tag_index] respond_to :json, :only => [:index, :show] @@ -15,50 +16,27 @@ class PeopleController < ApplicationController render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404 end + helper_method :search_query + def index @aspect = :search - params[:q] ||= params[:term] || '' - - if params[:q][0] == 35 || params[:q][0] == '#' - if params[:q].length > 1 - tag_name = params[:q].gsub(/[#\.]/, '') - redirect_to tag_path(:name => tag_name, :q => params[:q]) - return - else - flash[:error] = I18n.t('tags.show.none', :name => params[:q]) - redirect_to :back - end - end - limit = params[:limit] ? params[:limit].to_i : 15 + @people = Person.search(search_query, current_user) + respond_to do |format| format.json do - @people = Person.search(params[:q], current_user).limit(limit) + @people = @people.limit(limit) render :json => @people end - format.html do - #only do it if it is an email address - if diaspora_id?(params[:q]) - people = Person.where(:diaspora_handle => params[:q].downcase) - Webfinger.in_background(params[:q]) if people.empty? - 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 - format.mobile do + format.any(:html, :mobile) do #only do it if it is an email address - if diaspora_id?(params[:q]) - people = Person.where(:diaspora_handle => params[:q]) - Webfinger.in_background(params[:q]) if people.empty? - else - people = Person.search(params[:q], current_user) + if diaspora_id?(search_query) + @people = Person.where(:diaspora_handle => search_query.downcase) + Webfinger.in_background(search_query) if @people.empty? 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) end end @@ -70,7 +48,7 @@ class PeopleController < ApplicationController respond_with @people end - def hashes_for_people people, aspects + def hashes_for_people(people, aspects) ids = people.map{|p| p.id} contacts = {} Contact.unscoped.where(:user_id => current_user.id, :person_id => ids).each do |contact| @@ -168,6 +146,21 @@ class PeopleController < ApplicationController !query.try(:match, /^(\w)*@([a-zA-Z0-9]|[-]|[.]|[:])*$/).nil? end + def search_query + @search_query ||= params[:q] || params[:term] || '' + end + + def redirect_if_tag_search + if search_query.starts_with?('#') + if search_query.length > 1 + redirect_to tag_path(:name => search_query.delete('#.'), :q => search_query) + else + flash[:error] = I18n.t('tags.show.none', :name => search_query) + redirect_to :back + end + end + end + private def remote_profile_with_no_user_session? diff --git a/app/helpers/people_helper.rb b/app/helpers/people_helper.rb index 5a9d5be464c71fa959c69ba7387906559d271dc4..2e0851213c9050e93978822763d2b63561b9823a 100644 --- a/app/helpers/people_helper.rb +++ b/app/helpers/people_helper.rb @@ -4,6 +4,18 @@ module PeopleHelper include ERB::Util + + def search_header + if search_query.blank? + content_tag(:h2, t('people.index.no_results')) + else + content_tag(:h2, :id => 'search_title') do + t('people.index.results_for').html_safe + ' ' + + content_tag(:span, search_query, :class => 'term') + end + end + end + def request_partial single_aspect_form if single_aspect_form 'requests/new_request_with_aspect_to_person' @@ -13,8 +25,8 @@ module PeopleHelper end def search_or_index - if params[:q] - I18n.t 'people.helper.results_for',:params => params[:q] + if search_query + I18n.t 'people.helper.results_for',:params => search_query else I18n.t "people.helper.people_on_pod_are_aware_of" end diff --git a/app/helpers/tags_helper.rb b/app/helpers/tags_helper.rb new file mode 100644 index 0000000000000000000000000000000000000000..e3b882fe78c77e8351322c725b4977369077e15b --- /dev/null +++ b/app/helpers/tags_helper.rb @@ -0,0 +1,19 @@ +module TagsHelper + def looking_for_tag_link + return if @search_query.include?('@') || normalized_tag_name.blank? + content_tag('h4') do + content_tag('small') do + t('.looking_for', :tag_link => tag_link).html_safe + end + end + end + + def normalized_tag_name + ActsAsTaggableOn::Tag.normalize(search_query) + end + + def tag_link + tag = normalized_tag_name + link_to("##{tag}", tag_path(:name => tag)) + end +end \ No newline at end of file diff --git a/app/views/people/index.html.haml b/app/views/people/index.html.haml index a695b6dfd5395f2e30cd35d5946840fb0926d428..e6e4eb048bbef2bfddbc67ca3fb001ba5c53e0dc 100644 --- a/app/views/people/index.html.haml +++ b/app/views/people/index.html.haml @@ -10,18 +10,8 @@ = javascript_include_tag 'contact-list' .span-24.last - - if params[:q].blank? - %h2 - =t('.no_results') - - else - %h2#search_title - =t('.results_for') - %span.term - = params[:q] - - 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 + = search_header + = looking_for_tag_link .span-15 %hr .clearfix diff --git a/spec/controllers/people_controller_spec.rb b/spec/controllers/people_controller_spec.rb index e895286a3b6ce43988b9f8c7478b1c00d2d56051..961a106750bab2f168268e5abcb7ac5a350db220 100644 --- a/spec/controllers/people_controller_spec.rb +++ b/spec/controllers/people_controller_spec.rb @@ -85,11 +85,6 @@ describe PeopleController do 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 "succeeds if there is exactly one match" do get :index, :q => "Korth" assigns[:people].length.should == 1 @@ -104,13 +99,11 @@ describe PeopleController do 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 'succeeds if you search for punctuation' do get :index, :q => '+' - assigns[:normalized_tag_for_query].should be_empty response.should be_success end