wildsearcher 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/lib/wildsearcher.rb +44 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7e4cc4203569b654f25a929843566c92f77f4f0d
|
4
|
+
data.tar.gz: 171f199fddb87ce52adb931de3c69e622fc7b771
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f44bc5e1bbeccc8d922ce1e38f94b9f27d2796cd38d2e528adb54982b19c74506b589d1020d44011db5388e1250405a7149882be19b4db8fb4c17d8e4ab727c7
|
7
|
+
data.tar.gz: aa0310db8067cf79f6100fec5940f7ccfd3cbd689bf7a313b4e0491d999a9e976f924988df602089feb0461e105ad17e6ea866c112481c0e572fc6fb9095b2be
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Vivek Bisen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# Wildsearcher
|
2
|
+
|
3
|
+
Search for records in rails controller or model by specifying fields and a search term.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'wildsearcher'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install wildsearcher
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
##### `wildsearcher(params)`
|
24
|
+
|
25
|
+
For your rails application, make your url look like `http://some.car_website.com?search_fields=year,make,vin&search_term=fancy ford`.
|
26
|
+
|
27
|
+
This will populate `search_term` and `search_fields` in your controller params. Now, filter your records in `index` action or wherever by calling `Car.wildsearcher(params)`. That's it!
|
28
|
+
|
29
|
+
|
30
|
+
##### `filter_records`
|
31
|
+
|
32
|
+
If you are looking for filtering records for a model `Car(id: integer, year: integer, make: string, vin: string)`, you can call `Car.filter_records(search_fields: [year, vin], search_term: "20")` and it will return expected records.
|
33
|
+
|
34
|
+
## Development
|
35
|
+
|
36
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
37
|
+
|
38
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/vivekbisen/wildsearcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
43
|
+
|
44
|
+
|
45
|
+
## License
|
46
|
+
|
47
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
48
|
+
|
data/lib/wildsearcher.rb
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
require "wildsearcher/version"
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module Wildsearcher
|
5
|
+
def wildsearcher(params = {})
|
6
|
+
field_array = params[:search_fields].to_s.split(",")
|
7
|
+
term = params[:search_term].to_s.strip
|
8
|
+
filter_records(search_fields: field_array, search_term: term)
|
9
|
+
end
|
10
|
+
|
11
|
+
def filter_records(search_fields: [], search_term: "")
|
12
|
+
return default_scope if search_fields.empty? || search_term.blank?
|
13
|
+
where(conditions(filter_fields(search_fields), search_term))
|
14
|
+
end
|
15
|
+
|
16
|
+
private def filter_fields(fields)
|
17
|
+
fields.keep_if do |f|
|
18
|
+
f.to_s.downcase.in? column_names
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private def conditions(fields, term)
|
23
|
+
term = "%" + term.strip + "%" if db_like == "ILIKE"
|
24
|
+
[fields.map { |f| "#{f} #{db_like} ?" }.join(" OR ")] + ([term] * fields.count)
|
25
|
+
end
|
26
|
+
|
27
|
+
private def default_scope
|
28
|
+
::ActiveRecord::VERSION::MAJOR >= 4 ? all : scoped
|
29
|
+
end
|
30
|
+
|
31
|
+
private def db_like
|
32
|
+
case ::ActiveRecord::Base.connection.class.name.demodulize
|
33
|
+
when "PostgreSQLAdapter"
|
34
|
+
"ILIKE"
|
35
|
+
when "MysqlAdapter"
|
36
|
+
"LIKE"
|
37
|
+
else
|
38
|
+
raise NotImplementedError.new("Wildsearcher currently supports only Mysql and Postgresql.")
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
::ActiveRecord::Base.extend(Wildsearcher)
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wildsearcher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vivek Bisen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: activerecord
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '4.0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '4.0'
|
69
|
+
description: Filter your records for your rails app with one line change.
|
70
|
+
email:
|
71
|
+
- vivek@luther.edu
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- LICENSE.txt
|
77
|
+
- README.md
|
78
|
+
- lib/wildsearcher.rb
|
79
|
+
homepage: https://github.com/vivekbisen/wildsearcher
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
allowed_push_host: https://rubygems.org
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.4.8
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Provide a way to search a term for specified fields
|
104
|
+
test_files: []
|