ruby_simple_search 0.0.3 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +38 -0
- data/LICENSE.txt +2 -2
- data/README.md +95 -63
- data/lib/ruby_simple_search.rb +76 -49
- data/lib/ruby_simple_search/errors.rb +6 -7
- data/lib/ruby_simple_search/like_patterns.rb +8 -0
- data/lib/ruby_simple_search/version.rb +1 -1
- metadata +77 -34
- data/.gitignore +0 -19
- data/.rspec +0 -2
- data/Gemfile +0 -4
- data/Rakefile +0 -1
- data/lib/ruby_simple_search/like_pattern.rb +0 -4
- data/ruby_simple_search.gemspec +0 -25
- data/spec/lib/user.rb +0 -8
- data/spec/lib/user2.rb +0 -6
- data/spec/spec_helper.rb +0 -40
- data/spec/user_spec.rb +0 -132
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fff76b38b0d4b750a205788f0213735da4d14e0e
|
4
|
+
data.tar.gz: 91d61ad7a53a00f9b818951aff37c2b88c4c8095
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0dad0091246f35424bb7a529f2ab8259096074ea5de4e2a0c3c200cde61bcd7c4f0868b10ac19453c2b192bcf25a75bf6af69d63493b18e3e4c10f783ad15187
|
7
|
+
data.tar.gz: 3e59a4db93d6c3302a8a89cfbe8758c28fbfaf5f59203663a582e479461b20a24dc34632f087821616874ee2fe676bec79979e42ea33d1e28c921935c0efbfbf
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
|
+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
## [2.0.0]
|
8
|
+
### Added
|
9
|
+
- Supports `attributes` parameter to `simple_search` method. [PR#4](https://github.com/mechanicles/ruby_simple_search/pull/4)
|
10
|
+
- Supports data types other than `string` and `text` to `simple_search_attributes`
|
11
|
+
- Now tests cover **SQLite**, **MySQL**, and **PostgreSQL** databases
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
- Used Minitest over RSpec
|
15
|
+
- Refactored the code
|
16
|
+
- Used Travis CI over CircleCi
|
17
|
+
|
18
|
+
### Removed
|
19
|
+
- Removed `plain` pattern from `LIKE` query
|
20
|
+
|
21
|
+
## [0.0.3]
|
22
|
+
### Fixed
|
23
|
+
- Fixed problem when using simple search with joins. [GI#1](https://github.com/mechanicles/ruby_simple_search/issues/1)
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
- Moved pattern option to `simple_search` method and removed it from `simple_search_attributes` method
|
27
|
+
- Updated specs accordingly
|
28
|
+
|
29
|
+
## [0.0.2]
|
30
|
+
### Added
|
31
|
+
- Added support for `LIKE` patterns e.g. 'beginning', 'ending', 'containing', 'underscore', and 'plain'
|
32
|
+
- Added block support to `simple_search` method so user can extend it based on its need
|
33
|
+
- Added specs
|
34
|
+
- Added some exceptions handling
|
35
|
+
|
36
|
+
## [0.0.1]
|
37
|
+
### Added
|
38
|
+
- First major release
|
data/LICENSE.txt
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright (c) 2013 Santosh Wadghule
|
1
|
+
Copyright (c) 2013-2018 Santosh Wadghule
|
2
2
|
|
3
3
|
MIT License
|
4
4
|
|
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
19
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,50 +1,24 @@
|
|
1
1
|
# RubySimpleSearch
|
2
2
|
|
3
|
-
|
4
|
-
very easily.
|
3
|
+
The simplest way to search the data in ActiveRecord models.
|
5
4
|
|
6
|
-
|
7
|
-
table.
|
5
|
+
It offers simple but useful features:
|
8
6
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
7
|
+
- [Search on the default attributes](#search-on-the-default-attributes)
|
8
|
+
- [Override default search attributes to specific attributes ](#override-default-search-attributes-to-specific-attributes) (Credit goes to [@abdullahtariq1171](https://github.com/abdullahtariq1171))
|
9
|
+
- [Search using patterns](#search-using-patterns)
|
10
|
+
- [Ruby block support to extend the search query](#ruby-block-support-to-extend-the-search-query)
|
11
|
+
- [Simple search returns an `ActiveRecord::Relation` object](#simple-search-returns-an-activerecordrelation-object)
|
13
12
|
|
14
|
-
|
13
|
+
Mostly on the admin side, we do have a standard text field to search the data on the table.
|
14
|
+
Sometimes we want to search through the attributes like title, content and ratings on the
|
15
|
+
post model or email, username and description on the user model. For those searches, we use
|
16
|
+
MySQL's or PostgreSQL's `LIKE` operator to get the results. While doing the same thing again
|
17
|
+
and again on the different models, you add lots of duplication in your code.
|
15
18
|
|
16
|
-
####
|
17
|
-
- 'LIKE' pattern is more flexible now. Now you can pass pattern on ```simple_search```
|
18
|
-
method directly. Pattern support on the ```simple_search_attributes``` method has been removed
|
19
|
-
- Fixed column ambiguous error when used with the joins
|
19
|
+
#### Do not repeat yourself, use RubySimpleSearch.
|
20
20
|
|
21
|
-
|
22
|
-
#### RubySimpleSearch Features:
|
23
|
-
- Added 'LIKE' pattern support ('beginning', 'ending', 'containing', 'underscore', 'plain').
|
24
|
-
By default pattern is 'containing'
|
25
|
-
|
26
|
-
```Ruby
|
27
|
-
Post.simple_search('york', :pattern => :ending)
|
28
|
-
# It will search like '%york'
|
29
|
-
|
30
|
-
Post.simple_search('york', :pattern => :begining)
|
31
|
-
# It will search like 'york%'
|
32
|
-
|
33
|
-
Post.simple_search('york', :pattern => :containing)
|
34
|
-
# It will search like '%york%'
|
35
|
-
|
36
|
-
Post.simple_search('o', :pattern => :underscore)
|
37
|
-
# It will search like '_o_'
|
38
|
-
|
39
|
-
Post.simple_search('yourk', :pattern => :plain)
|
40
|
-
# It will search like 'york'
|
41
|
-
```
|
42
|
-
- Added **block** support to ```simple_search``` method, so user can extend the query as per
|
43
|
-
his/her requirements (Now you can operate on the integer/decimal values also)
|
44
|
-
|
45
|
-
- Added specs
|
46
|
-
|
47
|
-
- Added exception handler
|
21
|
+
[![Build Status](https://travis-ci.org/mechanicles/ruby_simple_search.svg?branch=master)](https://travis-ci.org/mechanicles/ruby_simple_search)
|
48
22
|
|
49
23
|
## Installation
|
50
24
|
|
@@ -54,7 +28,7 @@ Add this line to your application's Gemfile:
|
|
54
28
|
|
55
29
|
And then execute:
|
56
30
|
|
57
|
-
$ bundle
|
31
|
+
$ bundle install
|
58
32
|
|
59
33
|
Or install it yourself as:
|
60
34
|
|
@@ -62,7 +36,7 @@ Or install it yourself as:
|
|
62
36
|
|
63
37
|
## Usage
|
64
38
|
|
65
|
-
Define attributes that you want to search
|
39
|
+
Define attributes that you want to search on it
|
66
40
|
|
67
41
|
```Ruby
|
68
42
|
class Post < ActiveActiveRecord::Base
|
@@ -71,44 +45,102 @@ class Post < ActiveActiveRecord::Base
|
|
71
45
|
simple_search_attributes :title, :description
|
72
46
|
end
|
73
47
|
```
|
48
|
+
|
74
49
|
```Ruby
|
75
50
|
class User < ActiveActiveRecord::Base
|
76
51
|
include RubySimpleSearch
|
77
52
|
|
53
|
+
simple_search_attributes :email, :username, :address, :age
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
## Features
|
58
|
+
|
59
|
+
### Search on the default attributes
|
60
|
+
If you don't provide any attribute at the time of searching, it will use `simple_search_attributes` from the model.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
class User < ActiveActiveRecord::Base
|
64
|
+
include RubySimpleSearch
|
65
|
+
|
78
66
|
simple_search_attributes :email, :username, :address
|
79
67
|
end
|
68
|
+
|
69
|
+
|
70
|
+
Post.simple_search('york')
|
71
|
+
# It will search in :email, :username and :address only
|
80
72
|
```
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
73
|
+
|
74
|
+
### Override default search attributes to specific attributes
|
75
|
+
|
76
|
+
If you want to perform a specific search on particular attributes, you can pass specific attributes with `attributes` option.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
class User < ActiveActiveRecord::Base
|
80
|
+
include RubySimpleSearch
|
81
|
+
|
82
|
+
simple_search_attributes :email, :username, :address
|
83
|
+
end
|
84
|
+
|
85
|
+
Post.simple_search('york')
|
86
|
+
# It will search in :email, :username and :address only
|
87
|
+
|
88
|
+
Post.simple_search('york', attributes: :address)
|
89
|
+
# It will search in :address only
|
90
|
+
|
91
|
+
User.simple_search('york', pattern: :ending, attributes: [:email, :address])
|
92
|
+
# It will search in :email and :address only with 'ending' pattern
|
87
93
|
```
|
88
|
-
|
89
|
-
|
90
|
-
|
94
|
+
|
95
|
+
### Search using patterns
|
96
|
+
You can pass a `LIKE` pattern to the `simple_search` method.
|
97
|
+
|
98
|
+
Patterns:
|
99
|
+
|
100
|
+
- beginning
|
101
|
+
- ending
|
102
|
+
- containing (Default pattern)
|
103
|
+
- plain
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
Post.simple_search('york', pattern: :beginning)
|
107
|
+
# It will search like 'york%' and finds any values that start with "york"
|
108
|
+
|
109
|
+
Post.simple_search('york', pattern: :ending)
|
110
|
+
# It will search like '%york' and finds any values that end with "york"
|
111
|
+
|
112
|
+
Post.simple_search('york', pattern: :containing)
|
113
|
+
# It will search like '%york%' and finds any values that have "york" in any position
|
114
|
+
|
115
|
+
Post.simple_search('york', pattern: :plain)
|
116
|
+
# It will search like 'york' and finds any values that have "york" word
|
91
117
|
```
|
118
|
+
|
119
|
+
### Ruby block support to extend the search query
|
120
|
+
|
92
121
|
```Ruby
|
93
|
-
User.simple_search('
|
94
|
-
["
|
122
|
+
User.simple_search('35') do |search_term|
|
123
|
+
["AND age = ?", search_term]
|
95
124
|
end
|
96
|
-
# => You can pass block to simple_search method so you can extend it as your
|
97
|
-
# wish but you need to return an array of valid parameters like you do in #where
|
98
|
-
# method
|
99
|
-
```
|
100
|
-
```Ruby
|
101
|
-
Model.simple_search('string')
|
102
|
-
# => with and without block will return ActiveRecord::Relation object
|
103
125
|
```
|
126
|
+
Block should return an array of search condition and values.
|
127
|
+
|
128
|
+
### Simple search returns an `ActiveRecord::Relation` object
|
129
|
+
|
104
130
|
```Ruby
|
131
|
+
Model.simple_search('string') # => ActiveRecord::Relation object
|
132
|
+
|
105
133
|
Model.simple_search('string').to_sql
|
106
|
-
|
134
|
+
|
135
|
+
# OR
|
136
|
+
|
107
137
|
User.simple_search('mechanicles') do |search_term|
|
108
|
-
["
|
138
|
+
["AND address != ?", search_term]
|
109
139
|
end.to_sql
|
110
|
-
|
140
|
+
|
141
|
+
# => It will return an SQL query in string format
|
111
142
|
```
|
143
|
+
|
112
144
|
## Contributing
|
113
145
|
|
114
146
|
1. Fork it
|
data/lib/ruby_simple_search.rb
CHANGED
@@ -1,17 +1,18 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
1
|
+
require 'ruby_simple_search/version'
|
2
|
+
require 'ruby_simple_search/like_patterns'
|
3
|
+
require 'ruby_simple_search/errors'
|
4
4
|
require 'active_support/concern'
|
5
5
|
|
6
6
|
module RubySimpleSearch
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
9
|
included do
|
10
|
-
|
11
|
-
def
|
10
|
+
instance_eval do
|
11
|
+
def simple_search_attributes(*args)
|
12
12
|
@simple_search_attributes = []
|
13
13
|
args.each do |arg|
|
14
|
-
raise ArgumentError,
|
14
|
+
raise ArgumentError, Errors::WRONG_ATTRIBUTES unless arg.is_a? Symbol
|
15
|
+
|
15
16
|
@simple_search_attributes << arg
|
16
17
|
end
|
17
18
|
end
|
@@ -19,70 +20,96 @@ module RubySimpleSearch
|
|
19
20
|
end
|
20
21
|
|
21
22
|
module ClassMethods
|
22
|
-
def simple_search(search_term, options={}, &block)
|
23
|
-
raise
|
24
|
-
raise ArgumentError,
|
25
|
-
set_pattern(options[:pattern])
|
26
|
-
|
27
|
-
sql_query = nil
|
28
|
-
extended_query = nil
|
29
|
-
sql_query_condition = ""
|
30
|
-
sql_query_values = []
|
23
|
+
def simple_search(search_term, options = {}, &block)
|
24
|
+
raise Errors::ATTRIBUTES_MISSING if @simple_search_attributes.blank?
|
25
|
+
raise ArgumentError, Errors::SEARCH_ARG_TYPE unless search_term.is_a? String
|
31
26
|
|
32
|
-
|
33
|
-
|
34
|
-
@
|
35
|
-
|
36
|
-
|
37
|
-
end
|
27
|
+
@simple_search_term = search_term
|
28
|
+
@simple_search_pattern = get_pattern(options[:pattern])
|
29
|
+
@simple_search_patterned_text = @simple_search_pattern.gsub('q', @simple_search_term.try(:downcase))
|
30
|
+
@simple_search_query_conditions = ''
|
31
|
+
@simple_search_query_values = []
|
38
32
|
|
39
|
-
|
40
|
-
|
41
|
-
extended_query = block.call(search_term)
|
42
|
-
end
|
43
|
-
|
44
|
-
if !extended_query.nil?
|
45
|
-
sql_query_values, sql_query_condition = extend_simple_search(extended_query,
|
46
|
-
sql_query_condition,
|
47
|
-
sql_query_values)
|
48
|
-
end
|
49
|
-
sql_query = [sql_query_condition, sql_query_values]
|
33
|
+
build_query_conditions_and_values(options)
|
34
|
+
extend_query(block) if block.is_a? Proc
|
50
35
|
|
36
|
+
sql_query = [@simple_search_query_conditions, @simple_search_query_values]
|
51
37
|
where(sql_query.flatten)
|
52
38
|
end
|
53
39
|
|
54
40
|
private
|
55
41
|
|
56
|
-
def
|
42
|
+
def get_pattern(pattern)
|
57
43
|
if pattern.nil?
|
58
44
|
# default pattern is '%q%'
|
59
|
-
|
45
|
+
LIKE_PATTERNS[:containing]
|
60
46
|
else
|
61
|
-
pattern =
|
62
|
-
raise
|
63
|
-
|
47
|
+
pattern = LIKE_PATTERNS[pattern.to_sym]
|
48
|
+
raise Errors::INVALID_PATTERN if pattern.nil?
|
49
|
+
|
50
|
+
pattern
|
64
51
|
end
|
65
52
|
end
|
66
53
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
70
|
-
|
54
|
+
def build_query_conditions_and_values(options)
|
55
|
+
attributes = if options[:attributes].nil?
|
56
|
+
@simple_search_attributes
|
57
|
+
else
|
58
|
+
_attr = *options[:attributes]
|
59
|
+
end
|
60
|
+
|
61
|
+
attributes.each do |attribute|
|
62
|
+
condition, value = build_query_condition_and_value(attribute)
|
71
63
|
|
72
|
-
|
73
|
-
|
64
|
+
@simple_search_query_conditions << condition
|
65
|
+
@simple_search_query_values << value
|
74
66
|
end
|
67
|
+
end
|
75
68
|
|
76
|
-
|
77
|
-
|
69
|
+
def build_query_condition_and_value(attribute)
|
70
|
+
condition = if %i[string text].include?(columns_hash[attribute.to_s].type)
|
71
|
+
build_query_for_string_and_text_types(attribute)
|
72
|
+
else
|
73
|
+
build_query_non_string_and_text_types(attribute)
|
74
|
+
end
|
78
75
|
|
79
|
-
[
|
76
|
+
[condition, @simple_search_patterned_text]
|
80
77
|
end
|
81
78
|
|
82
|
-
def
|
83
|
-
|
84
|
-
|
79
|
+
def build_query_for_string_and_text_types(attribute)
|
80
|
+
if @simple_search_query_conditions.blank?
|
81
|
+
"LOWER(#{table_name}.#{attribute}) LIKE ?"
|
82
|
+
else
|
83
|
+
" OR LOWER(#{table_name}.#{attribute}) LIKE ?"
|
84
|
+
end
|
85
85
|
end
|
86
86
|
|
87
|
+
def build_query_non_string_and_text_types(attribute)
|
88
|
+
if @simple_search_query_conditions.blank?
|
89
|
+
"CAST(#{table_name}.#{attribute} AS CHAR(255)) LIKE ?"
|
90
|
+
else
|
91
|
+
" OR CAST(#{table_name}.#{attribute} AS CHAR(255)) LIKE ?"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def extend_query(block)
|
96
|
+
@simple_search_query_conditions = "(#{@simple_search_query_conditions})"
|
97
|
+
extended_query = block.call @simple_search_term
|
98
|
+
extend_simple_search(extended_query) if extended_query
|
99
|
+
end
|
100
|
+
|
101
|
+
def extend_simple_search(extended_query)
|
102
|
+
raise Errors::INVALID_TYPE unless extended_query.is_a?(Array)
|
103
|
+
|
104
|
+
extended_query_condition = extended_query[0]
|
105
|
+
extended_query_values = extended_query - [extended_query[0]]
|
106
|
+
|
107
|
+
if extended_query_condition.count('?') != extended_query_values.size
|
108
|
+
raise Errors::INVALID_CONDITION
|
109
|
+
end
|
110
|
+
|
111
|
+
@simple_search_query_conditions << " #{extended_query_condition}"
|
112
|
+
@simple_search_query_values += extended_query_values
|
113
|
+
end
|
87
114
|
end
|
88
115
|
end
|
@@ -1,11 +1,10 @@
|
|
1
|
-
require "ruby_simple_search/errors"
|
2
|
-
|
3
1
|
module RubySimpleSearch
|
4
2
|
module Errors
|
5
|
-
ATTRIBUTES_MISSING
|
6
|
-
|
7
|
-
|
8
|
-
INVALID_PATTERN
|
9
|
-
|
3
|
+
ATTRIBUTES_MISSING = 'Simple search attributes are missing'.freeze
|
4
|
+
INVALID_CONDITION = "Extended query's array conditions are wrong".freeze
|
5
|
+
INVALID_TYPE = 'Extended query is not an array type'.freeze
|
6
|
+
INVALID_PATTERN = "Looks like given pattern is wrong, valid pattern list is '#{LIKE_PATTERNS.keys}'".freeze
|
7
|
+
SEARCH_ARG_TYPE = '`search_term` argument is not a string'.freeze
|
8
|
+
WRONG_ATTRIBUTES = "`simple_search_arguments` method's arguments should be in symbol format".freeze
|
10
9
|
end
|
11
10
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_simple_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santosh Wadghule
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -16,36 +16,64 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: '4.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: '4.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
34
|
-
type: :
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
|
-
type: :
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
49
77
|
prerelease: false
|
50
78
|
version_requirements: !ruby/object:Gem::Requirement
|
51
79
|
requirements:
|
@@ -53,7 +81,35 @@ dependencies:
|
|
53
81
|
- !ruby/object:Gem::Version
|
54
82
|
version: '0'
|
55
83
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
84
|
+
name: pg
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "<"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "<"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: mysql2
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "<"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.5'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "<"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.5'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: sqlite3
|
57
113
|
requirement: !ruby/object:Gem::Requirement
|
58
114
|
requirements:
|
59
115
|
- - ">="
|
@@ -66,31 +122,22 @@ dependencies:
|
|
66
122
|
- - ">="
|
67
123
|
- !ruby/object:Gem::Version
|
68
124
|
version: '0'
|
69
|
-
description:
|
70
|
-
|
71
|
-
email:
|
72
|
-
- santosh.wadghule@gmail.com
|
125
|
+
description:
|
126
|
+
email: santosh.wadghule@gmail.com
|
73
127
|
executables: []
|
74
128
|
extensions: []
|
75
129
|
extra_rdoc_files: []
|
76
130
|
files:
|
77
|
-
-
|
78
|
-
- ".rspec"
|
79
|
-
- Gemfile
|
131
|
+
- CHANGELOG.md
|
80
132
|
- LICENSE.txt
|
81
133
|
- README.md
|
82
|
-
- Rakefile
|
83
134
|
- lib/ruby_simple_search.rb
|
84
135
|
- lib/ruby_simple_search/errors.rb
|
85
|
-
- lib/ruby_simple_search/
|
136
|
+
- lib/ruby_simple_search/like_patterns.rb
|
86
137
|
- lib/ruby_simple_search/version.rb
|
87
|
-
- ruby_simple_search.gemspec
|
88
|
-
- spec/lib/user.rb
|
89
|
-
- spec/lib/user2.rb
|
90
|
-
- spec/spec_helper.rb
|
91
|
-
- spec/user_spec.rb
|
92
138
|
homepage: https://github.com/mechanicles/ruby_simple_search
|
93
|
-
licenses:
|
139
|
+
licenses:
|
140
|
+
- MIT
|
94
141
|
metadata: {}
|
95
142
|
post_install_message:
|
96
143
|
rdoc_options: []
|
@@ -100,7 +147,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
100
147
|
requirements:
|
101
148
|
- - ">="
|
102
149
|
- !ruby/object:Gem::Version
|
103
|
-
version:
|
150
|
+
version: '2.2'
|
104
151
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
152
|
requirements:
|
106
153
|
- - ">="
|
@@ -108,12 +155,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
155
|
version: '0'
|
109
156
|
requirements: []
|
110
157
|
rubyforge_project:
|
111
|
-
rubygems_version: 2.
|
158
|
+
rubygems_version: 2.4.5.1
|
112
159
|
signing_key:
|
113
160
|
specification_version: 4
|
114
|
-
summary:
|
115
|
-
test_files:
|
116
|
-
- spec/lib/user.rb
|
117
|
-
- spec/lib/user2.rb
|
118
|
-
- spec/spec_helper.rb
|
119
|
-
- spec/user_spec.rb
|
161
|
+
summary: The simplest way to search the data
|
162
|
+
test_files: []
|
data/.gitignore
DELETED
data/.rspec
DELETED
data/Gemfile
DELETED
data/Rakefile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
data/ruby_simple_search.gemspec
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'ruby_simple_search/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |gem|
|
7
|
-
gem.name = "ruby_simple_search"
|
8
|
-
gem.version = RubySimpleSearch::VERSION
|
9
|
-
gem.authors = ["Santosh Wadghule"]
|
10
|
-
gem.email = ["santosh.wadghule@gmail.com"]
|
11
|
-
gem.description = %q{It will search on the attributes that you provided to simple_search_attributes method}
|
12
|
-
gem.summary = %q{Ruby simple search for ActiveRecord}
|
13
|
-
gem.homepage = "https://github.com/mechanicles/ruby_simple_search"
|
14
|
-
|
15
|
-
gem.add_dependency "activesupport", ">= 3.0.0"
|
16
|
-
gem.add_dependency "activerecord", ">= 3.0.0"
|
17
|
-
gem.add_dependency "sqlite3"
|
18
|
-
gem.add_development_dependency "rspec"
|
19
|
-
gem.required_ruby_version = ">= 1.9.2"
|
20
|
-
|
21
|
-
gem.files = `git ls-files`.split($/)
|
22
|
-
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
23
|
-
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
24
|
-
gem.require_paths = ["lib"]
|
25
|
-
end
|
data/spec/lib/user.rb
DELETED
data/spec/lib/user2.rb
DELETED
data/spec/spec_helper.rb
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
ActiveRecord::Migration.verbose = false
|
5
|
-
|
6
|
-
RSpec.configure do |config|
|
7
|
-
config.before(:all) do
|
8
|
-
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
9
|
-
create_database
|
10
|
-
User.create :email => "alice@example.com",
|
11
|
-
:name => "alice", :address => "usa",
|
12
|
-
:contact => '12345', :age => 60
|
13
|
-
User.create :email => "bob@example.com",
|
14
|
-
:name => "bob", :address => "usa",
|
15
|
-
:contact => "56789", :age => 26
|
16
|
-
end
|
17
|
-
|
18
|
-
config.after(:all) do
|
19
|
-
drop_database
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def create_database
|
24
|
-
ActiveRecord::Schema.define(:version => 1) do
|
25
|
-
create_table :users do |t|
|
26
|
-
t.string :name
|
27
|
-
t.text :address
|
28
|
-
t.text :contact
|
29
|
-
t.string :email
|
30
|
-
t.integer :age
|
31
|
-
t.timestamps
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def drop_database
|
37
|
-
ActiveRecord::Base.connection.tables.each do |table|
|
38
|
-
ActiveRecord::Base.connection.drop_table(table)
|
39
|
-
end
|
40
|
-
end
|
data/spec/user_spec.rb
DELETED
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative "../spec/lib/user.rb"
|
3
|
-
require_relative "../spec/lib/user2.rb"
|
4
|
-
|
5
|
-
describe 'User' do
|
6
|
-
describe ".simple_search_attributes" do
|
7
|
-
it "sets attributes" do
|
8
|
-
expect(User.instance_variable_get("@simple_search_attributes")).to eq([:name, :email, :contact, :address])
|
9
|
-
end
|
10
|
-
|
11
|
-
it "does not return an exception if simple_search_attributes is not called while loading the model" do
|
12
|
-
expect { User.simple_search('usa') }.to_not raise_error(RubySimpleSearch::Errors::ATTRIBUTES_MISSING)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
describe ".simple_search" do
|
17
|
-
context "without block" do
|
18
|
-
it "searches users whose names are 'alice'" do
|
19
|
-
user = User.find_by_name('alice')
|
20
|
-
users = User.simple_search('alice')
|
21
|
-
users.should include(user)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "has default 'LIKE' pattern" do
|
25
|
-
User.find_by_name('alice')
|
26
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q%')
|
27
|
-
end
|
28
|
-
|
29
|
-
it "can have 'LIKE' patterns like plain, beginning, ending, containing and underscore" do
|
30
|
-
User.simple_search('alice', pattern: :plain)
|
31
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('q')
|
32
|
-
User.simple_search('al', pattern: :beginning)
|
33
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('q%')
|
34
|
-
User.simple_search('alice', pattern: :ending)
|
35
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q')
|
36
|
-
User.simple_search('alice', pattern: :containing)
|
37
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q%')
|
38
|
-
User.simple_search('alice', pattern: :underscore)
|
39
|
-
expect(User.instance_variable_get("@simple_search_pattern")).to eq('_q_')
|
40
|
-
end
|
41
|
-
|
42
|
-
it "raises an exception if pattern is wrong" do
|
43
|
-
expect{ User.simple_search('alice', pattern: 'wrong') }.to raise_error(RubySimpleSearch::Errors::INVALID_PATTERN)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "searches users whose names are 'alice' with beginning pattern" do
|
47
|
-
user = User.find_by_name('alice')
|
48
|
-
users = User.simple_search('al', { pattern: :beginning })
|
49
|
-
users.should include(user)
|
50
|
-
end
|
51
|
-
|
52
|
-
it "returns empty records if contact number does not exist" do
|
53
|
-
users = User.simple_search('343434')
|
54
|
-
users.should be_empty
|
55
|
-
end
|
56
|
-
|
57
|
-
it "searches user records if users belong to 'USA'" do
|
58
|
-
users = User.where(:address => 'usa')
|
59
|
-
searched_users = User.simple_search('usa')
|
60
|
-
expect(users.to_a).to eq(searched_users.to_a)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "searches the records with beginning pattern" do
|
64
|
-
users = User.where("name like ?", 'bo%')
|
65
|
-
User.simple_search_attributes :name, :contact, :address
|
66
|
-
searched_users = User.simple_search('bo', pattern: :beginning)
|
67
|
-
expect(users.count).to eq(searched_users.count)
|
68
|
-
end
|
69
|
-
|
70
|
-
it "searches the records with ending pattern" do
|
71
|
-
users = User.where("name like ?", '%ce')
|
72
|
-
User.simple_search_attributes :name, :contact, :address
|
73
|
-
searched_users = User.simple_search('ce', pattern: :ending)
|
74
|
-
expect(users.count).to eq(searched_users.count)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "searches the records with underscore pattern" do
|
78
|
-
users = User.where("name like ?", 'ce')
|
79
|
-
User.simple_search_attributes :name, :contact, :address
|
80
|
-
searched_users = User.simple_search('ce', pattern: :underscore)
|
81
|
-
expect(users.count).to eq(searched_users.count)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "searches the records with plain pattern" do
|
85
|
-
users = User.where("name like ?", 'bob')
|
86
|
-
User.simple_search_attributes :name, :contact, :address
|
87
|
-
searched_users = User.simple_search('bob', pattern: :plain)
|
88
|
-
expect(users.count).to eq(searched_users.count)
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
context "with block" do
|
93
|
-
it "returns users who live in usa and their age should be greater than 50" do
|
94
|
-
User.simple_search_attributes :name, :contact, :address
|
95
|
-
users = User.where(:age => 60)
|
96
|
-
searched_users = User.simple_search('usa', pattern: :plain) do |search_term|
|
97
|
-
['AND age > ?', 50]
|
98
|
-
end
|
99
|
-
expect(users.to_a).to eq(searched_users.to_a)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "returns an exception if array condition is wrong in simple_search block" do
|
103
|
-
expect{ User.simple_search('usa') do |search_term|
|
104
|
-
['AND age > ?', 50, 60]
|
105
|
-
end }.to raise_error(RubySimpleSearch::Errors::INVALID_CONDITION)
|
106
|
-
end
|
107
|
-
|
108
|
-
it "returns an exception if return value is not an array type" do
|
109
|
-
expect{ User.simple_search('usa') do |search_term|
|
110
|
-
"Wrong return"
|
111
|
-
end }.to raise_error(RubySimpleSearch::Errors::INVALID_TYPE)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
describe User2 do
|
118
|
-
describe ".simple_search_attributes" do
|
119
|
-
it "returns an exception if simple_search_attributes method is not called while loading the model" do
|
120
|
-
expect { User2.simple_search('usa') }.to raise_error(RubySimpleSearch::Errors::ATTRIBUTES_MISSING)
|
121
|
-
end
|
122
|
-
|
123
|
-
it "returns an exception if simple_search_attributes method has wrong attribute type" do
|
124
|
-
expect { User2.simple_search_attributes :name, '24' }.to raise_error(RubySimpleSearch::Errors::WROG_ATTRIBUTES)
|
125
|
-
end
|
126
|
-
|
127
|
-
it "sets attributes if simple_search_attributes method is called on the model" do
|
128
|
-
User2.simple_search_attributes :name, :contact
|
129
|
-
expect(User2.instance_variable_get("@simple_search_attributes")).to eq([:name, :contact])
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|