Skip to content

Commit

Permalink
Searching works, but currently only on zones. [#2 state:open]
Browse files Browse the repository at this point in the history
  • Loading branch information
keegan committed Jul 9, 2008
1 parent f2c72a9 commit 7e388b0
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/models/zone.rb
Expand Up @@ -12,6 +12,8 @@
#
class Zone < ActiveRecord::Base

acts_as_searchable

belongs_to :user

has_many :records, :dependent => :destroy
Expand Down
8 changes: 8 additions & 0 deletions app/views/search/results.html.haml
@@ -0,0 +1,8 @@
%h1.underline= "Results for \"#{@search_parameters}\""
%br/

%h2.underline Zones

- unless @zones.blank?
- for zone in @zones do
= link_to zone.name, zone_path( zone )
Empty file.
20 changes: 20 additions & 0 deletions vendor/plugins/acts_as_searchable/MIT-LICENSE
@@ -0,0 +1,20 @@
Copyright (c) 2008 Josh Clayton

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 changes: 23 additions & 0 deletions vendor/plugins/acts_as_searchable/README
@@ -0,0 +1,23 @@
ActsAsSearchable
================

ActsAsSearchable provides a simple way to search upon a model. It uses named_scope if available; otherwise, it will accept a block or return find(:all) with applicable conditions applied


Example
=======

class Entry < ActiveRecord::Base
acts_as_searchable :on => [:title, :body, "comments.body", "CONCAT(users.first_name, ' ', users.last_name)", "users.login"], :include => [:comments, :user]

belongs_to :user
has_many :comments
end

# within console
Entry.search('Josh Clayton') #=> [<Entry>, <Entry>]
Entry.search('%ruby%') #=> [<Entry>, <Entry>, <Entry>]

=======

Copyright (c) 2008 Josh Clayton, released under the MIT license
22 changes: 22 additions & 0 deletions vendor/plugins/acts_as_searchable/Rakefile
@@ -0,0 +1,22 @@
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

desc 'Default: run unit tests.'
task :default => :test

desc 'Test the acts_as_searchable plugin.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

desc 'Generate documentation for the acts_as_searchable plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'ActsAsSearchable'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
2 changes: 2 additions & 0 deletions vendor/plugins/acts_as_searchable/init.rb
@@ -0,0 +1,2 @@
require 'acts_as_searchable'
ActiveRecord::Base.send(:include, Shooter::Acts::Searchable)
47 changes: 47 additions & 0 deletions vendor/plugins/acts_as_searchable/lib/acts_as_searchable.rb
@@ -0,0 +1,47 @@
module Shooter
module Acts
module Searchable
def self.included(base)
base.extend ClassMethods
end

module ClassMethods
def acts_as_searchable(options = {})
unless searchable?
cattr_accessor :searchable_attributes, :comparison_operator, :searchable_scope_options
include InstanceMethods
end

self.searchable_attributes ||= []
self.searchable_attributes << (options.delete(:on) || [])
self.searchable_attributes.flatten!
self.comparison_operator = options.delete(:comparison) || :like
self.searchable_scope_options ||= {}
self.searchable_scope_options.update(options) {|k,v1,v2| k.to_sym == :include ? [v1 << v2].flatten.compact.uniq : v2 }
end

def searchable?
self.included_modules.include?(InstanceMethods)
end
end

module InstanceMethods
def self.included(base)
if base.respond_to?(:named_scope)
base.named_scope :search, lambda { |query| base.searchable_scope_options.merge(:conditions => [base.searchable_attributes.map {|attribute| "#{"#{base.table_name}." unless attribute.to_s.include?(".")}#{attribute} #{base.comparison_operator.to_s.upcase} :query"}.join(" OR "), {:query => query}])}
else
base.extend ClassMethods
end
end

module ClassMethods
def search(query = nil, options = {})
with_scope(:find => self.searchable_scope_options.merge(:conditions => [self.searchable_attributes.map {|attribute| "#{"#{table_name}." unless attribute.to_s.include?(".")}#{attribute} #{self.comparison_operator.to_s.upcase} :query"}.join(" OR "), {:query => query}])) do
block_given? ? yield(options) : find(:all, options)
end
end
end
end
end
end
end
@@ -0,0 +1,4 @@
# desc "Explaining what the task does"
# task :acts_as_searchable do
# # Task goes here
# end
@@ -0,0 +1,8 @@
require 'test/unit'

class ActsAsSearchableTest < Test::Unit::TestCase
# Replace this with your real tests.
def test_this_plugin
flunk
end
end

0 comments on commit 7e388b0

Please sign in to comment.