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? %>