Skip to content

Commit

Permalink
basic search plugin complete
Browse files Browse the repository at this point in the history
  • Loading branch information
djones committed Feb 5, 2010
0 parents commit dc8786c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/controllers/search_controller.rb
@@ -0,0 +1,17 @@
class SearchController < ApplicationController

before_filter :find_page

# Display search results given the query supplied
def show
@results = SearchEngine.search(params[:query], params[:page])
present(@page)
end

protected

def find_page
@page = Page.find_by_link_url("/search")
end

end
15 changes: 15 additions & 0 deletions app/helpers/search_helper.rb
@@ -0,0 +1,15 @@
module SearchHelper

# this is where you register your result URLs based on the
# type of result we're dealing with
def result_url(result)
table_name = result.class.to_s.tableize
case table_name
when "news_items"
"/news/#{result.to_param}"
else
"/#{table_name}/#{result.to_param}"
end
end

end
24 changes: 24 additions & 0 deletions app/models/search_engine.rb
@@ -0,0 +1,24 @@
class SearchEngine

# How many results should we show per page
RESULTS_LIMIT = 10

# Perform search over the specified models
def self.search(query, page = 1)
if query.present?
results = []

# TODO need to come up with something clever here to allow it to search
# over multiple plugins that are registered with Refinery to do so
# rather than hardcoding it
[Page, NewsItem].each do |model|
results << model.find_with_index(query, :limit => RESULTS_LIMIT)
end

results.flatten[0..(RESULTS_LIMIT - 1)]
else
[] # not results for no query
end
end

end
36 changes: 36 additions & 0 deletions app/views/search/show.html.erb
@@ -0,0 +1,36 @@
<% content_for :body_content_page_title do %>
Search Results for '<%=h params[:query] %>'
<% end %>
<% content_for :body_content_left do %>
<ul id="search_results">
<% @results.each do |result| %>
<li>
<span class='result_type'>
<%= result.class.to_s.titleize %>
</span>
<%= link_to result.title, result_url(result) %>
</li>
<% end %>
</ul>

<% end %>
<%= render :partial => "/shared/content_page" %>

<style type='text/css'>
#search_results {
list-style: none;
padding: 0;
margin: 0;
}

#search_results li {
border-bottom: 1px solid #CCC;
}

#search_results li span.result_type {
float: right;
color: #CCC;
}
</style>
5 changes: 5 additions & 0 deletions app/views/shared/_search.html.erb
@@ -0,0 +1,5 @@
<% form_tag search_url do -%>
<%= text_field_tag :query, {}, {:type => "search",
:placeholder => (params[:query] || "Search site for...")} %>
<%= submit_tag 'Go' %>
<% end %>
7 changes: 7 additions & 0 deletions config/routes.rb
@@ -0,0 +1,7 @@
ActionController::Routing::Routes.draw do |map|

map.search "/search", :controller => 'search', :action => 'show'

map.resource :search_result, :controller => 'search'

end
6 changes: 6 additions & 0 deletions rails/init.rb
@@ -0,0 +1,6 @@
Refinery::Plugin.register do |plugin|
plugin.title = "Search"
plugin.description = "Add a site search engine to your Refinery site."
plugin.version = 1.0
plugin.hide_from_menu = true
end

0 comments on commit dc8786c

Please sign in to comment.