Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Converted to a Rails3/Refinery CMS engine.

  • Loading branch information...
commit a8e679d9ace447e459ef2150e715e5ade18837b6 1 parent 3c91b1b
@parndt parndt authored
View
9 app/controllers/search_controller.rb
@@ -1,17 +1,10 @@
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")
+ present(@page = Page.find_by_link_url("/search"))
end
end
View
22 app/models/search_engine.rb
@@ -1,21 +1,17 @@
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 = []
-
- [Page].each do |model|
- results << model.with_query(query).find(:all, :limit => RESULTS_LIMIT)
- end
-
- results.flatten[0..(RESULTS_LIMIT - 1)]
- else
- [] # not results for no query
- end
+ results = []
+
+ Refinery.searchable_models.each do |model|
+ results << model.limit(RESULTS_LIMIT).with_query(query)
+ end if query.present?
+
+ results.flatten[0..(RESULTS_LIMIT - 1)]
end
end
View
5 config/locales/en.yml
@@ -0,0 +1,5 @@
+en:
+ plugins:
+ refinerycms_search:
+ title: Search
+ description: Extra search handling for Refinery CMS
View
8 config/routes.rb
@@ -1,7 +1,5 @@
-ActionController::Routing::Routes.draw do |map|
-
- map.search "/search", :controller => 'search', :action => 'show'
+Refinery::Application.routes.draw do
+
+ match "/search", :to => 'search#show', :as => 'search'
- map.resource :search_result, :controller => 'search'
-
end
View
29 lib/gemspec.rb
@@ -0,0 +1,29 @@
+#!/usr/bin/env ruby
+version = '0.9.8'
+raise "Could not get version so gemspec can not be built" if version.nil?
+files = Dir.glob("**/*").flatten.reject do |file|
+ file =~ /\.gem$/
+end
+
+gemspec = <<EOF
+Gem::Specification.new do |s|
+ s.name = %q{refinerycms-search}
+ s.version = %q{#{version}}
+ s.date = %q{#{Time.now.strftime('%Y-%m-%d')}}
+ s.summary = %q{Extra search handling for Refinery CMS}
+ s.description = %q{Provides extra functionality for searching your frontend website using Refinery CMS.}
+ s.homepage = %q{http://refinerycms.com}
+ s.email = %q{info@refinerycms.com}
+ s.authors = ["Resolve Digital"]
+ s.require_paths = %w(lib)
+
+ s.add_dependency 'refinerycms', '>= 0.9.8'
+
+ s.files = [
+ '#{files.join("',\n '")}'
+ ]
+ s.require_path = 'lib'
+end
+EOF
+
+File.open(File.expand_path("../../refinerycms-search.gemspec", __FILE__), 'w').puts(gemspec)
View
27 lib/refinerycms-search.rb
@@ -0,0 +1,27 @@
+require 'refinery'
+
+module Refinery
+ module Search
+ class Engine < Rails::Engine
+ config.to_prepare do
+ module ::Refinery
+ class << self
+ attr_accessor :searchable_models
+
+ def searchable_models
+ @searchable_models ||= [Page]
+ end
+ end
+ end
+ end
+
+ config.after_initialize do
+ ::Refinery::Plugin.register do |plugin|
+ plugin.name = 'refinerycms_search'
+ plugin.version = 1.0
+ plugin.hide_from_menu = true
+ end
+ end
+ end
+ end
+end
View
43 readme.md
@@ -3,50 +3,41 @@
By: [Resolve Digital](http://www.resolvedigital.com)
-## Plugin Installation
+## Installation
-Just 'git clone' Refinery, install this as a plugin using:
+Simply use this by adding the following to your ``Gemfile``:
- script/plugin install git://github.com/resolve/refinerycms-search.git
-
-*Rails 3*
-
- rails plugin install git://github.com/resolve/refinerycms-search.git
+ gem 'refinerycms-search', '~> 0.9.8'
-You'll also need to create a page (from the 'Pages' tab) with a custom URL of '/search'. You can set a custom URL for a page in the Advanced Options. It's probably also a good idea to uncheck the 'show in menu' option for this page.
+You'll also need to create a page (from the 'Pages' tab) with a custom URL of '/search'.
+You can set a custom URL for a page in the Advanced Options.
+It's probably also a good idea to uncheck the 'show in menu' option for this page.
-Remember to restart your server.
+Remember to restart your web server.
-A sample search form can be found in views/shared/_search.html.erb . You can either use this partial directly, or copy the appropriate parts.
+A sample search form can be found in [views/shared/_search.html.erb](http://github.com/blob/master/app/views/shared/_search.html.erb).
+You can either use this partial directly, or copy the appropriate parts.
-## Searching in plugins
+## Searching
-The default installation will search in Pages. If you wish to find results in other plugins you have created or installed, you can specify these in 'config/settings.rb' like so:
+The default installation will search in Pages.
+If you wish to find results in other plugins you have created or installed, you can specify these in ``config/settings.rb`` like so:
Refinery.searchable_models = [Page]
Simply add any additional models you wish to search to this array. For example, if you have the [portfolio plugin](http://github.com/resolve/refinerycms-portfolio) installed:
Refinery.searchable_models = [Page, PortfolioEntry]
-
-*Rails 3*
-Update ln 11 in search_engine.rb
-
- [Page, PortfolioEntry].each do |model|
-
-Any model you wish to search will need to be indexed using acts as indexed. To add indexing, simple add
+Any model you wish to search will need to be indexed using acts as indexed. To add indexing, simple add:
acts_as_indexed :fields => [:title, :body]
-If your model doesn't use a :title attribute, remember to use an alias_attribute:
+If your model doesn't use a ``:title attribute``, remember to add an ``alias_attribute``:
alias_attribute :title, :name #for example
-to the model. Obviously, replaced the indexed fields with those appropriate for your model. The above line will add indexing to PortfolioEntry in the portfolio plugin, which does not come indexed.
-
-If you wish to override the url used in the search results, add a 'url' method to your model, and the result of this method will be used instead.
-
-*Rails 3 Notes*
+You will need to replace the indexed fields with those appropriate for your model.
+The above line will add indexing to PortfolioEntry in the portfolio plugin, which does not come indexed.
-This was a quick patch to make this plugin work acceptably for RefineryCMS in rails3. Conversion to an engine would be preferable.
+If you wish to override the url used in the search results just add a ``url`` method to your model and the result of this method will be used instead.
View
38 refinerycms-search.gemspec
@@ -0,0 +1,38 @@
+Gem::Specification.new do |s|
+ s.name = %q{refinerycms-search}
+ s.version = %q{0.9.8}
+ s.date = %q{2010-10-01}
+ s.summary = %q{Extra search handling for Refinery CMS}
+ s.description = %q{Provides extra functionality for searching your frontend website using Refinery CMS.}
+ s.homepage = %q{http://refinerycms.com}
+ s.email = %q{info@refinerycms.com}
+ s.authors = ["Resolve Digital"]
+ s.require_paths = %w(lib)
+
+ s.add_dependency 'refinerycms', '>= 0.9.8'
+
+ s.files = [
+ 'app',
+ 'app/controllers',
+ 'app/controllers/search_controller.rb',
+ 'app/helpers',
+ 'app/helpers/search_helper.rb',
+ 'app/models',
+ 'app/models/search_engine.rb',
+ 'app/views',
+ 'app/views/search',
+ 'app/views/search/show.html.erb',
+ 'app/views/shared',
+ 'app/views/shared/_search.html.erb',
+ 'config',
+ 'config/locales',
+ 'config/locales/en.yml',
+ 'config/routes.rb',
+ 'lib',
+ 'lib/gemspec.rb',
+ 'lib/refinerycms-search.rb',
+ 'readme.md',
+ 'refinerycms-search.gemspec'
+ ]
+ s.require_path = 'lib'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.