dynamic_search 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +56 -0
- data/Rakefile +33 -0
- data/lib/dynamic_search.rb +43 -0
- data/lib/dynamic_search/version.rb +3 -0
- data/lib/tasks/dynamic_search_tasks.rake +4 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0bd92fbf6e943b1e7103d631f528bc256f1908c2
|
4
|
+
data.tar.gz: e625b99742280f234547e6d4984ece01ed830d97
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ade600ac15512d56ab78a93c77aeda6e586d8e8c730f9458b0ba2a1367157419174c44512d19cc9466c6f330a29824762fecb0661e761681717a6e0ee04ee84
|
7
|
+
data.tar.gz: df5c166699f454040155a4cc797705798136c10a2fbb48fc97c6e0c71781790f2c6e8e7ffd53403637c738ade2d03459239a0993f9d582818b8147fe2577ec7a
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Jeremy Winterberg
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# DynamicSearch
|
2
|
+
DynamicSearch provides support for multi term searches to your rails models.
|
3
|
+
|
4
|
+
## Usage
|
5
|
+
1. Include the module to any model you want DynamicSearch functionality.
|
6
|
+
```ruby
|
7
|
+
include DynamicSearch
|
8
|
+
```
|
9
|
+
|
10
|
+
2. Customize the search method to your models to specify how your search should work. In
|
11
|
+
this case, you're telling the search method which columns you want included in your
|
12
|
+
search. By default, DynamicSearch looks at every column except foreign keys, id,
|
13
|
+
and timestamps.
|
14
|
+
```ruby
|
15
|
+
def search(search)
|
16
|
+
super(search, ["first_name", "last_name", "username", "email"])
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
3. To use the search you can create a search box form in your view.
|
21
|
+
```ruby
|
22
|
+
<%= form_for :model_name, method: 'get' do |f| %>
|
23
|
+
<%= f.text_field :search, placeholder: "search" %>
|
24
|
+
<% end %>
|
25
|
+
```
|
26
|
+
Then in your controller, you can call the search method on your model.
|
27
|
+
```ruby
|
28
|
+
if params.has_key? :search
|
29
|
+
@results = Model.search(params[:search])
|
30
|
+
end
|
31
|
+
```
|
32
|
+
From there I normally would paginate the results.
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
Add this line to your application's Gemfile:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
gem 'dynamic_search'
|
39
|
+
```
|
40
|
+
|
41
|
+
And then execute:
|
42
|
+
```bash
|
43
|
+
$ bundle
|
44
|
+
```
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
```bash
|
48
|
+
$ gem install dynamic_search
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
If you find a bug, submit an issue or pull request if you've fixed it. Feel free to reach
|
53
|
+
out on twitter: [@jeremydwayne](https://www.twitter.com/jeremydwayne)
|
54
|
+
|
55
|
+
## License
|
56
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'DynamicSearch'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task default: :test
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module DynamicSearch
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
##
|
4
|
+
# This module provides the parent search method which will be available
|
5
|
+
# to any model which includes DynamicSearch `include DynamicSearch`
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def search(search, columns = nil)
|
10
|
+
##
|
11
|
+
# The search method will search all columns except foreign keys, id,
|
12
|
+
# and timestamps. The columns searched can be overwritten in an individual model
|
13
|
+
# by using the following syntax:
|
14
|
+
#
|
15
|
+
# def search(search)
|
16
|
+
# super(search, ["array", "of", "columns"])
|
17
|
+
# end
|
18
|
+
|
19
|
+
if search.present?
|
20
|
+
columns ||= self.column_names.reject{|e| e.end_with?("_id")} - ["id", "created_at", "updated_at"]
|
21
|
+
params = {}
|
22
|
+
sql = []
|
23
|
+
|
24
|
+
search.split.each_with_index do |s, i|
|
25
|
+
params["first#{i}".to_sym] = "#{sanitize_sql(s)}%"
|
26
|
+
params["nth#{i}".to_sym] = "% #{sanitize_sql(s)}%"
|
27
|
+
|
28
|
+
statements = []
|
29
|
+
for column in columns
|
30
|
+
statements << "#{column} LIKE :first#{i}"
|
31
|
+
statements << "#{column} LIKE :nth#{i}"
|
32
|
+
end
|
33
|
+
|
34
|
+
sql << "(#{statements.join(" OR ")})"
|
35
|
+
end
|
36
|
+
self.where(sql.join(" AND "), params)
|
37
|
+
else
|
38
|
+
self.all
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynamic_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Winterberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-11-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Adds a search method which allows you to search for multiple words from
|
56
|
+
multiple columns.
|
57
|
+
email:
|
58
|
+
- winterjd@uwec.edu
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- MIT-LICENSE
|
64
|
+
- README.md
|
65
|
+
- Rakefile
|
66
|
+
- lib/dynamic_search.rb
|
67
|
+
- lib/dynamic_search/version.rb
|
68
|
+
- lib/tasks/dynamic_search_tasks.rake
|
69
|
+
homepage: https://www.github.com/jeremydwayne/dynamic_search
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.6.13
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: DynamicSearch provides support for multi term searches to your rails models.
|
93
|
+
test_files: []
|