diff --git a/app/controllers/application.rb b/app/controllers/application.rb
index 3932f17..26c9e4a 100644
--- a/app/controllers/application.rb
+++ b/app/controllers/application.rb
@@ -103,10 +103,12 @@ class ApplicationController < ActionController::Base
end
def load_category
- path = params[:category_path].join('/')
- @category = environment.categories.find_by_path(path)
- if @category.nil?
- render_not_found(path)
+ unless params[:category_path].blank?
+ path = params[:category_path].join('/')
+ @category = environment.categories.find_by_path(path)
+ if @category.nil?
+ render_not_found(path)
+ end
end
end
diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb
index c96a145..7985b06 100644
--- a/app/controllers/public/search_controller.rb
+++ b/app/controllers/public/search_controller.rb
@@ -2,6 +2,10 @@ class SearchController < ApplicationController
helper TagsHelper
+ before_filter :load_category
+ before_filter :prepare_filter
+ before_filter :check_search_whole_site
+
protected
def search(finder, query)
@@ -10,40 +14,43 @@ class SearchController < ApplicationController
end
end
+ def prepare_filter
+ @finder = @category || @environment
+ end
+
+ def check_search_whole_site
+ if params[:search_whole_site] == 'yes'
+ redirect_to params.merge(:category_path => [], :search_whole_site => nil)
+ end
+ end
+
public
include SearchHelper
######################################################
+ SEARCH_IN = [
+ [ :articles, _('Articles') ],
+ [ :comments, _('Comments') ],
+ [ :enterprises, _('Enterprises') ],
+ [ :people, _('People') ],
+ [ :communities, _('Communities') ],
+ [ :products, _('Products') ]
+ ]
+
def index
@query = params[:query] || ''
@filtered_query = remove_stop_words(@query)
- @finder ||= @environment
-
@results = {}
@names = {}
- [
- [ :articles, _('Articles') ],
- [ :comments, _('Comments') ],
- [ :enterprises, _('Enterprises') ],
- [ :people, _('People') ],
- [ :communities, _('Communities') ],
- [ :products, _('Products') ]
- ].each do |key, description|
+ SEARCH_IN.each do |key, description|
@results[key] = search(@finder.send(key), @filtered_query) if params[:find_in].nil? || params[:find_in].empty? || params[:find_in].include?(key.to_s)
@names[key] = description
end
end
- before_filter :load_category, :only => :filter
- def filter
- @finder = @category
- index
- render :action => 'index'
- end
-
#######################################################
def tags
@@ -61,6 +68,7 @@ class SearchController < ApplicationController
#######################################################
def popup
+ @search_in = SEARCH_IN
render :action => 'popup', :layout => false
end
diff --git a/app/views/layouts/application.rhtml b/app/views/layouts/application.rhtml
index 4cd530f..4f6f355 100644
--- a/app/views/layouts/application.rhtml
+++ b/app/views/layouts/application.rhtml
@@ -99,7 +99,7 @@
<%= render :file => 'shared/user_menu' %>
<%=
lightbox_link_to ''+ _('Search'),
- { :controller => 'search', :action => 'popup' }, :id => 'open_search'
+ { :controller => 'search', :action => 'popup', :category_path => (@category ? @category.explode_path : []) }, :id => 'open_search'
%>
diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml
index c249d22..3b5e892 100644
--- a/app/views/search/index.rhtml
+++ b/app/views/search/index.rhtml
@@ -1,4 +1,4 @@
-
<%= _('Search results for "%s"') % @query %>
+ <%= @category ? (_('Search results for "%{query}" in %{category}') % { :query => @query, :category => @category.full_name}) : (_('Search results for "%s"') % @query) %>
<% @results.each do |name,results| %>
<% if !results.nil? and !results.empty? %>
diff --git a/app/views/search/popup.rhtml b/app/views/search/popup.rhtml
index 7388c23..2f8c0ba 100644
--- a/app/views/search/popup.rhtml
+++ b/app/views/search/popup.rhtml
@@ -1,13 +1,36 @@
<%= _('Search %s') % @environment.name %>
-<% form_tag(:action => 'index') do%>
+<% form_tag({:action => 'index', :category_path => (@category ? @category.explode_path : [])}, :method => 'get') do%>
<%= text_field_tag('query', '', :size => 50) %>
<%= submit_button(:search, _('Search')) %>
- <%= _('Search options') %>
+ <% if @category %>
+ <%= _('Search in:') %>
+
+ -
+ <%= radio_button_tag :search_whole_site, 'no', true %>
+ <%= _('Only in %{category}') % { :category => @category.full_name } %>
+
+ -
+ <%= radio_button_tag :search_whole_site, 'yes' %>
+ <%= _('Whole site') %>
+
+
+ <% end %>
+
+ <%= _('Search for:') %>
+
+
+ <% @search_in.each do |thing, name| %>
+ -
+ <%= check_box_tag 'find_in[]', thing.to_s, true %>
+ <%= name %>
+
+ <% end %>
+
<% button_bar do %>
diff --git a/lib/noosfero.rb b/lib/noosfero.rb
index 8dadf19..8a5ce2a 100644
--- a/lib/noosfero.rb
+++ b/lib/noosfero.rb
@@ -9,13 +9,6 @@ module Noosfero
Regexp.new(pattern)
end
- def self.pattern_for_controllers_from_design_blocks
- items = Dir.glob(File.join(RAILS_ROOT, 'app', 'design_blocks', '*', 'controllers', '*_controller.rb')).map do |item|
- item.gsub(/^.*\/([^\/]+)_controller.rb$/, '\1')
- end.join('|')
- Regexp.new(items.blank? ? '' : ('(' + items + ')'))
- end
-
class << self
attr_accessor :locales
attr_accessor :default_locale
diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb
index 4f754e1..ec64992 100644
--- a/test/functional/search_controller_test.rb
+++ b/test/functional/search_controller_test.rb
@@ -63,7 +63,7 @@ class SearchControllerTest < Test::Unit::TestCase
art2 = person.articles.build(:name => 'another article to be found')
art2.save!
- get :filter, :category_path => [ 'my-category' ], :query => 'article found', :find_in => [ 'articles' ]
+ get :index, :category_path => [ 'my-category' ], :query => 'article found', :find_in => [ 'articles' ]
assert_includes assigns(:results)[:articles], art1
assert_not_includes assigns(:results)[:articles], art2
@@ -95,7 +95,7 @@ class SearchControllerTest < Test::Unit::TestCase
art2 = person.articles.build(:name => 'another article to be found')
art2.save!
comment2 = art2.comments.build(:title => 'comment to be found', :body => 'hfyfyh', :author => person); comment2.save!
- get 'filter', :category_path => ['my-category'], :query => 'found', :find_in => [ 'comments' ]
+ get :index, :category_path => ['my-category'], :query => 'found', :find_in => [ 'comments' ]
assert_includes assigns(:results)[:comments], comment1
assert_not_includes assigns(:results)[:comments], comment2
@@ -116,7 +116,7 @@ class SearchControllerTest < Test::Unit::TestCase
# not in category
ent2 = Enterprise.create!(:name => 'testing enterprise 2', :identifier => 'test2')
- get :filter, :category_path => [ 'my-category' ], :query => 'testing', :find_in => [ 'enterprises' ]
+ get :index, :category_path => [ 'my-category' ], :query => 'testing', :find_in => [ 'enterprises' ]
assert_includes assigns(:results)[:enterprises], ent1
assert_not_includes assigns(:results)[:enterprises], ent2
@@ -135,7 +135,7 @@ class SearchControllerTest < Test::Unit::TestCase
c = Category.create!(:name => 'my category', :environment => Environment.default)
p1 = create_user('people_1').person; p1.name = 'a beautiful person'; p1.categories << c; p1.save!
p2 = create_user('people_2').person; p2.name = 'another beautiful person'; p2.save!
- get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'people' ]
+ get :index, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'people' ]
assert_includes assigns(:results)[:people], p1
assert_not_includes assigns(:results)[:people], p2
end
@@ -154,7 +154,7 @@ class SearchControllerTest < Test::Unit::TestCase
c1 = Community.create!(:name => 'a beautiful community', :identifier => 'bea_comm', :environment => Environment.default)
c2 = Community.create!(:name => 'another beautiful community', :identifier => 'an_bea_comm', :environment => Environment.default)
c1.categories << c; c1.save!
- get :filter, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'communities' ]
+ get :index, :category_path => [ 'my-category' ], :query => 'beautiful', :find_in => [ 'communities' ]
assert_includes assigns(:results)[:communities], c1
assert_not_includes assigns(:results)[:communities], c2
end
@@ -174,7 +174,7 @@ class SearchControllerTest < Test::Unit::TestCase
ent2 = Enterprise.create!(:name => 'teste2', :identifier => 'teste2')
prod1 = ent1.products.create!(:name => 'a beautiful product')
prod2 = ent2.products.create!(:name => 'another beautiful product')
- get 'filter', :category_path => ['my-category'], :query => 'beautiful', :find_in => ['products']
+ get :index, :category_path => ['my-category'], :query => 'beautiful', :find_in => ['products']
assert_includes assigns(:results)[:products], prod1
assert_not_includes assigns(:results)[:products], prod2
end
@@ -206,4 +206,59 @@ class SearchControllerTest < Test::Unit::TestCase
end
end
+ should 'present options of where to search' do
+ get :popup
+ names = {
+ :articles => 'Articles',
+ :comments => 'Comments',
+ :people => 'People',
+ :enterprises => 'Enterprises',
+ :communities => 'Communities',
+ :products => 'Products',
+ }
+ names.each do |thing,description|
+ assert_tag :tag => 'input', :attributes => { :type => 'checkbox', :name => "find_in[]", :value => thing.to_s, :checked => 'checked' }
+ assert_tag :tag => 'span', :content => description
+ end
+ end
+
+ should 'not display option to choose where to search when not inside filter' do
+ get :popup
+ assert_no_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'search_whole_site', :value => 'yes' }
+ end
+
+ should 'display option to choose searching in whole site or in current category' do
+ parent = Category.create!(:name => 'cat', :environment => Environment.default)
+ Category.create!(:name => 'sub', :environment => Environment.default, :parent => parent)
+
+ get :popup, :category_path => [ 'cat', 'sub']
+ assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'search_whole_site', :value => 'yes' }
+ assert_tag :tag => 'input', :attributes => { :type => 'radio', :name => 'search_whole_site', :value => 'no', :checked => 'checked' }
+ end
+
+ should 'search in whole site when told so' do
+ parent = Category.create!(:name => 'randomcat', :environment => Environment.default)
+ Category.create!(:name => 'randomchild', :environment => Environment.default, :parent => parent)
+
+ get :index, :category_path => [ 'randomcat', 'randomchild' ], :query => 'some random query', :search_whole_site => 'yes'
+
+ # search_whole_site must be removed to precent a infinite redirect loop
+ assert_redirected_to :action => 'index', :category_path => [], :query => 'some random query', :search_whole_site => nil
+ end
+
+ should 'submit form to root when not inside a filter' do
+ get :popup
+ assert_tag :tag => 'form', :attributes => { :action => '/search' }
+ end
+
+ should 'submit form to category path when inside a filter' do
+ get :popup, :category_path => Category.create!(:name => 'mycat', :environment => Environment.default).explode_path
+ assert_tag :tag => 'form', :attributes => { :action => '/search/index/mycat' }
+ end
+
+ should 'use GET method to search' do
+ get :popup
+ assert_tag :tag => 'form' , :attributes => { :method => 'get' }
+ end
+
end
--
libgit2 0.21.2