searchable 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.rvmrc +1 -0
- data/README.md +47 -2
- data/lib/searchable.rb +32 -23
- data/lib/searchable/version.rb +1 -1
- data/searchable.gemspec +2 -2
- metadata +4 -3
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.3@searchable --create
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Searchable
|
2
2
|
|
3
|
-
|
3
|
+
With this gem you can implement
|
4
|
+
|
5
|
+
1. search by string
|
6
|
+
2. filter by starting letter or number
|
7
|
+
|
8
|
+
Searching is using "LIKE" syntax.
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -18,7 +23,47 @@ Or install it yourself as:
|
|
18
23
|
|
19
24
|
## Usage
|
20
25
|
|
21
|
-
|
26
|
+
Activate gem in your model:
|
27
|
+
|
28
|
+
has_searchable :name
|
29
|
+
|
30
|
+
Update your controller action:
|
31
|
+
|
32
|
+
@projects = Project.search(search_options)
|
33
|
+
|
34
|
+
Add @search@ param to url to search. This will find any project with "car" in the name:
|
35
|
+
|
36
|
+
http://localhost:3000/projects?search=car
|
37
|
+
|
38
|
+
Add @letter@ param to url to search by starting letter. This will find any project with first letter "C":
|
39
|
+
|
40
|
+
http://localhost:3000/projects?letter=c
|
41
|
+
|
42
|
+
Add @letter@ param to url with value "num" to search by numbers. This will find projects with any number in the beginning:
|
43
|
+
|
44
|
+
http://localhost:3000/projects?letter=num
|
45
|
+
|
46
|
+
|
47
|
+
### Search form
|
48
|
+
|
49
|
+
Here's the simple HAML snippet for search form. You can add it to your index action:
|
50
|
+
|
51
|
+
= form_tag request.path, :method => :get do
|
52
|
+
= text_field_tag :search, params[:search]
|
53
|
+
= submit_tag "Search"
|
54
|
+
|
55
|
+
### Letter filter
|
56
|
+
|
57
|
+
Here's the simple HAML letter filter snippet. It is generating A, B, C, etc links, "#" link for numbers and reset link (visible only when filter is active).
|
58
|
+
|
59
|
+
- if params[:letter]
|
60
|
+
= link_to "Reset filter", {}
|
61
|
+
|
62
|
+
- if params[:letter] == "num"
|
63
|
+
= link_to "#", {:letter => "num"}
|
64
|
+
|
65
|
+
- ("A".."Z").each do |c|
|
66
|
+
= link_to c, {:letter => c.downcase}
|
22
67
|
|
23
68
|
## Contributing
|
24
69
|
|
data/lib/searchable.rb
CHANGED
@@ -1,34 +1,43 @@
|
|
1
1
|
require "searchable/version"
|
2
2
|
|
3
3
|
module Searchable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module ActiveRecord
|
5
|
+
def has_searchable field
|
6
|
+
@field = field
|
7
|
+
extend InstanceMethods
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
10
|
+
module InstanceMethods
|
11
|
+
def search(options = {})
|
12
|
+
case search_method(options)
|
13
|
+
when :string
|
14
|
+
self.where(["#{@field} LIKE ?", "%#{options[:by]}%"])
|
15
|
+
when :letter
|
16
|
+
self.where(["#{@field} LIKE ?", "#{options[:by_letter]}%"])
|
17
|
+
when :numbers
|
18
|
+
self.where(["#{@field} RLIKE ?", '^[0-9]'])
|
19
|
+
else
|
20
|
+
self.where(1)
|
21
|
+
end
|
20
22
|
end
|
21
|
-
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
24
|
+
private
|
25
|
+
def search_method(options)
|
26
|
+
if options[:by]
|
27
|
+
:string
|
28
|
+
elsif options[:by_letter]
|
29
|
+
options[:by_letter] == "num" ? :numbers : :letter
|
30
|
+
end
|
29
31
|
end
|
30
32
|
end
|
31
33
|
end
|
34
|
+
|
35
|
+
module Controller
|
36
|
+
def search_options
|
37
|
+
{:by => params[:search], :by_letter => params[:letter]}
|
38
|
+
end
|
39
|
+
end
|
32
40
|
end
|
33
41
|
|
34
|
-
ActiveRecord::Base.extend Searchable
|
42
|
+
ActiveRecord::Base.extend Searchable::ActiveRecord
|
43
|
+
ActionController::Base.send :include, Searchable::Controller
|
data/lib/searchable/version.rb
CHANGED
data/searchable.gemspec
CHANGED
@@ -5,8 +5,8 @@ Gem::Specification.new do |gem|
|
|
5
5
|
gem.authors = ["Stefan Huska"]
|
6
6
|
gem.email = ["stefan.huska@gmail.com"]
|
7
7
|
gem.description = %q{Simple search by LIKE queries}
|
8
|
-
gem.summary = %q{
|
9
|
-
gem.homepage = "http://
|
8
|
+
gem.summary = %q{Simple search by LIKE queries}
|
9
|
+
gem.homepage = "http://github.com/kelso/searchable"
|
10
10
|
|
11
11
|
gem.files = `git ls-files`.split($\)
|
12
12
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: searchable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,6 +19,7 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- .rvmrc
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
@@ -26,7 +27,7 @@ files:
|
|
26
27
|
- lib/searchable.rb
|
27
28
|
- lib/searchable/version.rb
|
28
29
|
- searchable.gemspec
|
29
|
-
homepage: http://
|
30
|
+
homepage: http://github.com/kelso/searchable
|
30
31
|
licenses: []
|
31
32
|
post_install_message:
|
32
33
|
rdoc_options: []
|
@@ -49,5 +50,5 @@ rubyforge_project:
|
|
49
50
|
rubygems_version: 1.8.24
|
50
51
|
signing_key:
|
51
52
|
specification_version: 3
|
52
|
-
summary:
|
53
|
+
summary: Simple search by LIKE queries
|
53
54
|
test_files: []
|