forked from refinery/refinerycms-search
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit dc8786c
Showing
7 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ActionController::Routing::Routes.draw do |map| | ||
|
||
map.search "/search", :controller => 'search', :action => 'show' | ||
|
||
map.resource :search_result, :controller => 'search' | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |