Skip to content
Extraits de code Groupes Projets
Valider 13d1e685 rédigé par Maxwell Salzberg's avatar Maxwell Salzberg
Parcourir les fichiers

refactor people#index

parent 0e242c3e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -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?
......
......@@ -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
......
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
......@@ -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
......
......@@ -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
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter