From 38da9b9ed0bf2ea087f1ed67984851d2320dbb33 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Thu, 5 Jun 2008 18:19:07 +0000 Subject: [PATCH] ActionItem410: refactoring search controller --- app/controllers/public/search_controller.rb | 17 +++++++++++++---- app/models/category_finder.rb | 12 ++++++++---- app/models/environment_finder.rb | 19 ++++++++++++------- app/views/search/_display_results.rhtml | 6 +++++- test/functional/search_controller_test.rb | 19 ++++++++++++++----- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 0ef0f90..20ab6b9 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -69,6 +69,8 @@ class SearchController < ApplicationController def index @query = params[:query] || '' @filtered_query = remove_stop_words(@query) + + # FIXME name is not unique @region = Region.find_by_name(params[:region][:name]) if params[:region] @results = {} @@ -81,6 +83,12 @@ class SearchController < ApplicationController end @names[key] = gettext(description) end + + if @results.keys.size == 1 + specific_action = @results.keys.first + send(specific_action) + render :action => specific_action + end end ####################################################### @@ -90,12 +98,13 @@ class SearchController < ApplicationController @results = {} @names = {} [ - [ :recent_people, _('Recently registered people'), @finder.recent('people') ], - [ :recent_communities, _('Recently created communities'), @finder.recent('communities') ], - [ :recent_articles, _('Recent articles'), @finder.recent('articles') ], + [ :people, _('Recently registered people'), @finder.recent('people') ], + [ :communities, _('Recently created communities'), @finder.recent('communities') ], + [ :articles, _('Recent articles'), @finder.recent('articles') ], [ :comments, _('Recent comments'), @finder.recent('comments') ], [ :most_commented_articles, _('Most commented articles'), @finder.most_commented_articles ], - [ :recent_enterptises, _('Recently created enterprises'), @finder.recent('enterprises') ] + [ :enterprises, _('Recently created enterprises'), @finder.recent('enterprises') ], + [ :events, _('Recently added events'), @finder.recent('events') ] ].each do |key, name, list| @results[key] = list @names[key] = name diff --git a/app/models/category_finder.rb b/app/models/category_finder.rb index 2a50555..7f9bebb 100644 --- a/app/models/category_finder.rb +++ b/app/models/category_finder.rb @@ -7,12 +7,16 @@ class CategoryFinder attr_reader :category_ids - def find(asset, query, options={}) - find_in_categorized(asset.to_s.singularize.camelize.constantize, query, options) + def find(asset, query = nil, options={}, limit = nil) + if query.blank? + asset_class(asset).find(:all, options_for_find(asset_class(asset), {:limit => limit, :order => "created_at desc, #{asset_table(asset)}.id desc"})) + else + find_in_categorized(asset.to_s.singularize.camelize.constantize, query, options) + end end - def recent(asset, limit = 10) - asset_class(asset).find(:all, options_for_find(asset_class(asset), {:limit => limit, :order => "created_at desc, #{asset_table(asset)}.id desc"})) + def recent(asset, limit = nil) + find(asset, nil, {}, limit) end def find_by_initial(asset, initial) diff --git a/app/models/environment_finder.rb b/app/models/environment_finder.rb index dd1eaa2..b686943 100644 --- a/app/models/environment_finder.rb +++ b/app/models/environment_finder.rb @@ -4,22 +4,27 @@ class EnvironmentFinder @environment = env end - def find(asset, query, options={}) - @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) + def find(asset, query = nil, options={}, limit = nil) + @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region) if @region && options[:within] options[:origin] = [@region.lat, @region.lng] else options.delete(:within) end - @environment.send(asset).find_by_contents(query, {}, options) - end - def recent(asset, limit = 10) - with_options :limit => limit, :order => 'created_at desc, id desc' do |finder| - @environment.send(asset).recent(limit) + if query.blank? + with_options :limit => limit, :order => 'created_at desc, id desc' do |finder| + @environment.send(asset).recent(limit) + end + else + @environment.send(asset).find_by_contents(query, {}, options) end end + def recent(asset, limit = nil) + find(asset, nil, {}, limit) + end + def find_by_initial(asset, initial) @environment.send(asset).find_by_initial(initial) end diff --git a/app/views/search/_display_results.rhtml b/app/views/search/_display_results.rhtml index cf0d7f1..9493cdb 100644 --- a/app/views/search/_display_results.rhtml +++ b/app/views/search/_display_results.rhtml @@ -12,7 +12,10 @@ <% if !results.nil? and !results.empty? %>
<% if @controller.action_name != 'assets' %> -

<%= @names[name] %>

+

+ <%= @names[name] %> + <%= link_to _('(see more ...)'), params.merge(:action => 'index', :find_in => [ name ]) %> +

<% end %> <% partial = partial_for_class results.first.class %>
@@ -22,6 +25,7 @@ <% end %>
+
<% else %>
diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index 11c7d59..a4f036e 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -362,7 +362,7 @@ class SearchControllerTest < Test::Unit::TestCase :products => 'Products', } names.each do |thing, description| - assert_tag :tag => 'div', :attributes => { :class => /search-results-#{thing}/ }, :descendant => { :tag => 'h3', :content => description } + assert_tag :tag => 'div', :attributes => { :class => /search-results-#{thing}/ }, :descendant => { :tag => 'h3', :content => Regexp.new(description) } assert_tag :tag => 'a', :content => "display #{thing.to_s.singularize}" end end @@ -447,7 +447,7 @@ class SearchControllerTest < Test::Unit::TestCase CategoryFinder.expects(:new).with(@category).returns(finger) get :category_index, :category_path => [ 'my-category' ] - assert_same recent, assigns(:results)[:recent_articles] + assert_same recent, assigns(:results)[:articles] end should 'list recent comments in the category' do @@ -479,7 +479,7 @@ class SearchControllerTest < Test::Unit::TestCase CategoryFinder.expects(:new).with(@category).returns(finger) get :category_index, :category_path => [ 'my-category' ] - assert_same recent_people, assigns(:results)[:recent_people] + assert_same recent_people, assigns(:results)[:people] end should 'list recently registered communities in the category' do @@ -490,7 +490,7 @@ class SearchControllerTest < Test::Unit::TestCase CategoryFinder.expects(:new).with(@category).returns(finger) get :category_index, :category_path => [ 'my-category' ] - assert_same recent_communities, assigns(:results)[:recent_communities] + assert_same recent_communities, assigns(:results)[:communities] end should 'list recently registered enterprises in the category' do @@ -501,7 +501,7 @@ class SearchControllerTest < Test::Unit::TestCase CategoryFinder.expects(:new).with(@category).returns(finger) get :category_index, :category_path => [ 'my-category' ] - assert_same recent_enterptises, assigns(:results)[:recent_enterptises] + assert_same recent_enterptises, assigns(:results)[:enterprises] end should 'not list "Search for ..." in category_index' do @@ -900,6 +900,15 @@ class SearchControllerTest < Test::Unit::TestCase assert_not_includes assigns(:results)[:events], ev3 end + %w[ people enterprises articles events communities products comments ].each do |asset| + + should "render asset-specific template when searching for #{asset}" do + get :index, :find_in => [ asset ] + assert_template asset + end + + end + ################################################################## ################################################################## -- libgit2 0.21.2