search_for 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +37 -0
- data/lib/search_for.rb +3 -0
- data/lib/search_for/activerecord.rb +3 -0
- data/lib/search_for/search_for.rb +10 -0
- metadata +86 -0
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
=Introduction
|
2
|
+
|
3
|
+
A simple gem for adding a chainable search method to ActiveRecord::Base (rails 3)
|
4
|
+
|
5
|
+
=Installation
|
6
|
+
|
7
|
+
To install, add this to your bundler Gemfile
|
8
|
+
|
9
|
+
gem 'search_for'
|
10
|
+
|
11
|
+
Next, go to the root of your project in a shell and run this command (you may need to use "sudo" depending on your environment):
|
12
|
+
|
13
|
+
λ bundle install
|
14
|
+
|
15
|
+
=Usage
|
16
|
+
|
17
|
+
Suppose you'd like to create a simple quicksearch form for searching your Book database.
|
18
|
+
We'll assume your books have the following attributes: "title, author, description, genre"
|
19
|
+
|
20
|
+
If someone searches for "romance", we'd like to it to return any books where the title, author, description, or genre matches "romance"
|
21
|
+
|
22
|
+
If someone searches for "space odyssey", we want it to narrow results: now a column attribute matches only if that attributes contains both "Space" and "Odyssey".
|
23
|
+
|
24
|
+
How does it work? Assuming our form posts a 'query' param, then we could do something like this in our controller:
|
25
|
+
|
26
|
+
Book.search_for params[:query], :on => [:title, :author, :description, :genre]
|
27
|
+
|
28
|
+
The sql generated would look something like this (assuming params[:query] = "space odyseey"):
|
29
|
+
|
30
|
+
Book.search_for(params[:query], :on => [:title, :author, :description, :genre]).to_sql
|
31
|
+
# ==>
|
32
|
+
# SELECT * FROM "books" WHERE (
|
33
|
+
# (title LIKE '%space%' AND title LIKE '%odyssey%')
|
34
|
+
# OR (author LIKE '%space%' AND author LIKE '%odyssey%')
|
35
|
+
# OR (description LIKE '%space%' AND description LIKE '%odyssey%')
|
36
|
+
# OR (genre LIKE '%space%' AND genre LIKE '%odyssey%')
|
37
|
+
# )
|
data/lib/search_for.rb
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
module SearchFor
|
2
|
+
def search_for(query, on_fields)
|
3
|
+
raise ArgumentError, "You must provide an :on => [..fields..] argument to #search_for." unless on_fields[:on]
|
4
|
+
on_fields = on_fields[:on].kind_of?(Array) ? on_fields[:on] : [on_fields[:on]]
|
5
|
+
query_terms = query.split(/\s+/)
|
6
|
+
fields = on_fields.map {|f| "(" + (["#{f} LIKE ?"] * query_terms.size).join(" AND ") + ")"}.join(" OR ")
|
7
|
+
query_terms = (query_terms.map {|qt| "%#{qt}%"}) * on_fields.size
|
8
|
+
where(*([fields] + query_terms))
|
9
|
+
end
|
10
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: search_for
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Matt Parker
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-21 00:00:00 -04:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 299253627
|
30
|
+
segments:
|
31
|
+
- 3
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta3
|
35
|
+
version: 3.0.0.beta3
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id001
|
38
|
+
description: This gem will add a chainable "search_for" method to your ActiveResource classes, allowingallowing you to pass in a search query and fields to search on.
|
39
|
+
email: moonmaster9000@gmail.com
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions: []
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
45
|
+
- README.rdoc
|
46
|
+
files:
|
47
|
+
- README.rdoc
|
48
|
+
- lib/search_for.rb
|
49
|
+
- lib/search_for/activerecord.rb
|
50
|
+
- lib/search_for/search_for.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/moonmaster9000/search_for
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
hash: 3
|
66
|
+
segments:
|
67
|
+
- 0
|
68
|
+
version: "0"
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
hash: 3
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.7
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A simple search generator for ActiveRecord in Rails 3
|
85
|
+
test_files: []
|
86
|
+
|