Commit 13a3e7cbd5d9cf55a47381f71925bdeb3b96738c
1 parent
b910579d
Exists in
master
and in
29 other branches
ActionItem25: providing a nice-looking and extensible search engine
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1002 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
13 changed files
with
88 additions
and
15 deletions
Show diff stats
app/controllers/public/search_controller.rb
1 | 1 | class SearchController < ApplicationController |
2 | + | |
3 | + SEARCHES = [] | |
4 | + | |
5 | + def self.search(&block) | |
6 | + SEARCHES << block | |
7 | + end | |
8 | + | |
9 | + protected | |
10 | + | |
11 | + ############################################# | |
12 | + # XXX add yours searches here | |
13 | + ############################################# | |
14 | + | |
15 | + search do |query| | |
16 | + Article.find_tagged_with(query) | |
17 | + end | |
18 | + | |
19 | + search do |query| | |
20 | + Article.find_by_contents(query) | |
21 | + end | |
22 | + | |
23 | + search do |query| | |
24 | + Profile.find_by_contents(query) | |
25 | + end | |
26 | + | |
27 | + # auxiliary method to search in all defined searches and collect the results | |
28 | + def search(query) | |
29 | + SEARCHES.inject([]) do |acc,finder| | |
30 | + acc += finder.call(query) | |
31 | + end.sort_by do |hit| | |
32 | + (hit.respond_to? :ferret_score) ? (1.0 - hit.ferret_score) : (-1.0) | |
33 | + end | |
34 | + end | |
35 | + | |
36 | + public | |
37 | + | |
2 | 38 | def index |
3 | 39 | @query = params[:query] || '' |
4 | - # TODO: uncomment find_by_contents when ferret start working | |
5 | - @results = Article.find_tagged_with(@query) + Article.find_all_by_title(@query) + Profile.find_all_by_name(@query) | |
40 | + @results = search(@query) | |
6 | 41 | end |
42 | + | |
7 | 43 | end | ... | ... |
app/helpers/search_helper.rb
app/models/article.rb
... | ... | @@ -14,6 +14,8 @@ class Article < ActiveRecord::Base |
14 | 14 | |
15 | 15 | acts_as_versioned |
16 | 16 | |
17 | + acts_as_searchable :fields => [ :name, :abstract, :body ] | |
18 | + | |
17 | 19 | # retrieves all articles belonging to the given +profile+ that are not |
18 | 20 | # sub-articles of any other article. |
19 | 21 | def self.top_level_for(profile) | ... | ... |
app/models/profile.rb
... | ... | @@ -15,7 +15,7 @@ class Profile < ActiveRecord::Base |
15 | 15 | |
16 | 16 | acts_as_design |
17 | 17 | |
18 | - acts_as_ferret :fields => [ :name ] | |
18 | + acts_as_searchable :fields => [ :name, :identifier ] | |
19 | 19 | |
20 | 20 | # Valid identifiers must match this format. |
21 | 21 | IDENTIFIER_FORMAT = /^[a-z][a-z0-9_]*[a-z0-9]$/ | ... | ... |
app/views/search/index.rhtml
1 | 1 | <h2> <%= _('Listing articles found') %> </h2> |
2 | 2 | |
3 | 3 | <h3> <%= _('Searching for ') + @query %></h3> |
4 | -<% @results.each do |a| %> | |
5 | - <h4> <%= link_to_document(a) if a.kind_of?(Article) %> </h4> | |
6 | - <h4> <%= link_to_homepage(a.name, a.identifier) if a.kind_of?(Profile) %> </h4> | |
7 | - | |
4 | +<% @results.each do |hit| %> | |
5 | + <%= render :partial => partial_for_hit(hit.class), :locals => { :hit => hit } %> | |
6 | + <div class='search-relevance'> | |
7 | + <%= _('Relevance: %d%%') % (hit.respond_to?(:ferret_score) ? (hit.ferret_score * 100.0).round : 100) %> | |
8 | + </div> | |
9 | + <br style='clear: left'/> | |
8 | 10 | <% end %> | ... | ... |
config/ferret_server.yml
public/designs/icons/default/README
public/designs/icons/default/style.css
851 Bytes