dynamic_search 0.1.6 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +38 -32
- data/lib/dynamic_search.rb +3 -5
- data/lib/dynamic_search/version.rb +1 -1
- metadata +5 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c4907236d9dfc36c9d2c2b6952691a4c8215a3cfe6a892b9c79bde7ca3399651
|
4
|
+
data.tar.gz: e4806fe172ab17bb0b208d0ba0a85d3fb3ea2bdd39744ab8c9d60008ade88a2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38a3ab629070c8382a38e7e3bb0487953aca0e19f7a4da3d7b43bae2243391ec35aa931529d54a72df450bc8d4daa4cadfa49b6eb8cfba8e449e1220b7d6185d
|
7
|
+
data.tar.gz: 40d59361d6d914e6ebc2475d580645f5097a472abdf023c3c9874560a6695a00aae80bf53b5bf145d51e7440b7d19c4af664e2783ce9f86fa1b1d5bbdada66be
|
data/README.md
CHANGED
@@ -1,35 +1,8 @@
|
|
1
1
|
# DynamicSearch
|
2
|
-
DynamicSearch provides support for multi term searches to your rails models.
|
2
|
+
DynamicSearch provides support for multi term searches to your rails models. At the
|
3
|
+
moment, I would recommend using at least rails 5.2 or higher for security reasons.
|
3
4
|
|
4
|
-
|
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.
|
5
|
+
At the moment, the sql only supports PostgreSQL!
|
33
6
|
|
34
7
|
## Installation
|
35
8
|
Add this line to your application's Gemfile:
|
@@ -40,12 +13,45 @@ gem 'dynamic_search'
|
|
40
13
|
|
41
14
|
And then execute:
|
42
15
|
```bash
|
43
|
-
|
16
|
+
bundle
|
44
17
|
```
|
45
18
|
|
46
19
|
Or install it yourself as:
|
47
20
|
```bash
|
48
|
-
|
21
|
+
gem install dynamic_search
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
1. Include the module to any model you want DynamicSearch functionality.
|
26
|
+
```ruby
|
27
|
+
include DynamicSearch
|
28
|
+
```
|
29
|
+
|
30
|
+
2. To use the search you can create a search box form in your view.
|
31
|
+
```ruby
|
32
|
+
<%= form_for :model_name, method: 'get' do |f| %>
|
33
|
+
<%= f.text_field :search, placeholder: "search" %>
|
34
|
+
<% end %>
|
35
|
+
```
|
36
|
+
Then in your controller, you can call the search method on your model. You could also
|
37
|
+
specify the columns you want to search on. If you don't specify columns, DynamicSearch
|
38
|
+
will check against all of the columns on your model except foreign keys, id, and timestamps.
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
columns_array = ["first_name", "last_name", "username", "email"]
|
42
|
+
@results = Model.search(params[:search], columns_array)
|
43
|
+
```
|
44
|
+
From there I normally would paginate the results with Kaminari or WillPaginate.
|
45
|
+
|
46
|
+
*Optional*
|
47
|
+
Customize the search method in your model to specify how your search should work all the
|
48
|
+
time for that model, instead of just one view like above. In this case, you're telling the search method
|
49
|
+
which columns you want included in your search every time for this model.
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
def search(search)
|
53
|
+
super(search, ["first_name", "last_name", "username", "email"])
|
54
|
+
end
|
49
55
|
```
|
50
56
|
|
51
57
|
## Contributing
|
data/lib/dynamic_search.rb
CHANGED
@@ -21,14 +21,12 @@ module DynamicSearch
|
|
21
21
|
params = {}
|
22
22
|
sql = []
|
23
23
|
|
24
|
-
search.split.each_with_index do |s, i|
|
25
|
-
params["
|
26
|
-
params["nth#{i}".to_sym] = "% #{sanitize_sql(s)}%"
|
24
|
+
search.split(' ').each_with_index do |s, i|
|
25
|
+
params["nth#{i}".to_sym] = "%#{sanitize_sql(s)}%"
|
27
26
|
|
28
27
|
statements = []
|
29
28
|
for column in columns
|
30
|
-
statements << "#{column} LIKE :
|
31
|
-
statements << "#{column} LIKE :nth#{i}"
|
29
|
+
statements << "UPPER(CAST(#{column} AS text)) LIKE UPPER(:nth#{i})"
|
32
30
|
end
|
33
31
|
|
34
32
|
sql << "(#{statements.join(" OR ")})"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Winterberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -85,8 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
85
85
|
- !ruby/object:Gem::Version
|
86
86
|
version: '0'
|
87
87
|
requirements: []
|
88
|
-
|
89
|
-
rubygems_version: 2.6.13
|
88
|
+
rubygems_version: 3.0.1
|
90
89
|
signing_key:
|
91
90
|
specification_version: 4
|
92
91
|
summary: DynamicSearch provides support for multi term searches to your rails models.
|