ruby_simple_search 2.0.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +9 -4
- data/README.md +5 -2
- data/lib/ruby_simple_search.rb +65 -64
- data/lib/ruby_simple_search/errors.rb +8 -6
- data/lib/ruby_simple_search/like_patterns.rb +7 -5
- data/lib/ruby_simple_search/version.rb +3 -1
- metadata +20 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 33cda6d4c33aefea4fe48b0270b97ca8ec2f8a2cd2ab5f5e1888b36e5803e46f
|
4
|
+
data.tar.gz: c7cdc38d3e5b0ca43732e5197e800628e2bbb3b00274587a628b0c2bfb8d21d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6ddba0207e0e1a533a6764e6573e6b205a9b6b4df5279fd9c17af24aabf5a0ca1da0784d701c33ddb93505f3badf6ec58ddb8d05afc6b1e2dd8c8e7d24ad2d99
|
7
|
+
data.tar.gz: '08018957ab245bd9a03c08857ed4ab95be8b92ae81e8d5310eff8191cb4576772e018eafe329f4cc0dbfe511d7e7adf44e2e1a912c10455a2b644f6b5344ef0f'
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
-
|
7
|
+
|
8
|
+
## 2.0.1
|
9
|
+
- Supported Rails 6
|
10
|
+
- Dropped support Rails < 5
|
11
|
+
|
12
|
+
## 2.0.0
|
8
13
|
### Added
|
9
14
|
- Supports `attributes` parameter to `simple_search` method. [PR#4](https://github.com/mechanicles/ruby_simple_search/pull/4)
|
10
15
|
- Supports data types other than `string` and `text` to `simple_search_attributes`
|
@@ -18,7 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
18
23
|
### Removed
|
19
24
|
- Removed `plain` pattern from `LIKE` query
|
20
25
|
|
21
|
-
##
|
26
|
+
## 0.0.3
|
22
27
|
### Fixed
|
23
28
|
- Fixed problem when using simple search with joins. [GI#1](https://github.com/mechanicles/ruby_simple_search/issues/1)
|
24
29
|
|
@@ -26,13 +31,13 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
|
26
31
|
- Moved pattern option to `simple_search` method and removed it from `simple_search_attributes` method
|
27
32
|
- Updated specs accordingly
|
28
33
|
|
29
|
-
##
|
34
|
+
## 0.0.2
|
30
35
|
### Added
|
31
36
|
- Added support for `LIKE` patterns e.g. 'beginning', 'ending', 'containing', 'underscore', and 'plain'
|
32
37
|
- Added block support to `simple_search` method so user can extend it based on its need
|
33
38
|
- Added specs
|
34
39
|
- Added some exceptions handling
|
35
40
|
|
36
|
-
##
|
41
|
+
## 0.0.1
|
37
42
|
### Added
|
38
43
|
- First major release
|
data/README.md
CHANGED
@@ -1,5 +1,10 @@
|
|
1
1
|
# RubySimpleSearch
|
2
2
|
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/mechanicles/ruby_simple_search.svg?branch=master)](https://travis-ci.org/mechanicles/ruby_simple_search)
|
5
|
+
[![Maintainability](https://api.codeclimate.com/v1/badges/20e84a4c3be302b07653/maintainability)](https://codeclimate.com/github/mechanicles/ruby_simple_search/maintainability)
|
6
|
+
[![Test Coverage](https://api.codeclimate.com/v1/badges/20e84a4c3be302b07653/test_coverage)](https://codeclimate.com/github/mechanicles/ruby_simple_search/test_coverage)
|
7
|
+
|
3
8
|
The simplest way to search the data in ActiveRecord models.
|
4
9
|
|
5
10
|
It offers simple but useful features:
|
@@ -18,8 +23,6 @@ and again on the different models, you add lots of duplication in your code.
|
|
18
23
|
|
19
24
|
#### Do not repeat yourself, use RubySimpleSearch.
|
20
25
|
|
21
|
-
[![Build Status](https://travis-ci.org/mechanicles/ruby_simple_search.svg?branch=master)](https://travis-ci.org/mechanicles/ruby_simple_search)
|
22
|
-
|
23
26
|
## Installation
|
24
27
|
|
25
28
|
Add this line to your application's Gemfile:
|
data/lib/ruby_simple_search.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require
|
4
|
-
require
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ruby_simple_search/version"
|
4
|
+
require "ruby_simple_search/like_patterns"
|
5
|
+
require "ruby_simple_search/errors"
|
6
|
+
require "active_support/concern"
|
5
7
|
|
6
8
|
module RubySimpleSearch
|
7
9
|
extend ActiveSupport::Concern
|
@@ -26,90 +28,89 @@ module RubySimpleSearch
|
|
26
28
|
|
27
29
|
@simple_search_term = search_term
|
28
30
|
@simple_search_pattern = get_pattern(options[:pattern])
|
29
|
-
@simple_search_patterned_text = @simple_search_pattern.gsub(
|
30
|
-
@simple_search_query_conditions =
|
31
|
+
@simple_search_patterned_text = @simple_search_pattern.gsub("q", @simple_search_term.try(:downcase))
|
32
|
+
@simple_search_query_conditions = []
|
31
33
|
@simple_search_query_values = []
|
32
34
|
|
33
35
|
build_query_conditions_and_values(options)
|
34
36
|
extend_query(block) if block.is_a? Proc
|
35
37
|
|
36
|
-
sql_query = [@simple_search_query_conditions, @simple_search_query_values]
|
38
|
+
sql_query = [@simple_search_query_conditions.join, @simple_search_query_values]
|
37
39
|
where(sql_query.flatten)
|
38
40
|
end
|
39
41
|
|
40
42
|
private
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
43
|
+
def get_pattern(pattern)
|
44
|
+
if pattern.nil?
|
45
|
+
# default pattern is '%q%'
|
46
|
+
LIKE_PATTERNS[:containing]
|
47
|
+
else
|
48
|
+
pattern = LIKE_PATTERNS[pattern.to_sym]
|
49
|
+
raise Errors::INVALID_PATTERN if pattern.nil?
|
50
|
+
|
51
|
+
pattern
|
52
|
+
end
|
51
53
|
end
|
52
|
-
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
def build_query_conditions_and_values(options)
|
56
|
+
attributes = if options[:attributes].nil?
|
57
|
+
@simple_search_attributes
|
58
|
+
else
|
59
|
+
_attr = *options[:attributes]
|
60
|
+
end
|
60
61
|
|
61
|
-
|
62
|
-
|
62
|
+
attributes.each do |attribute|
|
63
|
+
condition, value = build_query_condition_and_value(attribute)
|
63
64
|
|
64
|
-
|
65
|
-
|
65
|
+
@simple_search_query_conditions << condition
|
66
|
+
@simple_search_query_values << value
|
67
|
+
end
|
66
68
|
end
|
67
|
-
end
|
68
69
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
70
|
+
def build_query_condition_and_value(attribute)
|
71
|
+
condition = if %i[string text].include?(columns_hash[attribute.to_s].type)
|
72
|
+
build_query_for_string_and_text_types(attribute)
|
73
|
+
else
|
74
|
+
build_query_non_string_and_text_types(attribute)
|
75
|
+
end
|
75
76
|
|
76
|
-
|
77
|
-
|
77
|
+
[condition, @simple_search_patterned_text]
|
78
|
+
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
80
|
+
def build_query_for_string_and_text_types(attribute)
|
81
|
+
if @simple_search_query_conditions.blank?
|
82
|
+
"LOWER(#{table_name}.#{attribute}) LIKE ?"
|
83
|
+
else
|
84
|
+
" OR LOWER(#{table_name}.#{attribute}) LIKE ?"
|
85
|
+
end
|
84
86
|
end
|
85
|
-
end
|
86
87
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
88
|
+
def build_query_non_string_and_text_types(attribute)
|
89
|
+
if @simple_search_query_conditions.blank?
|
90
|
+
"CAST(#{table_name}.#{attribute} AS CHAR(255)) LIKE ?"
|
91
|
+
else
|
92
|
+
" OR CAST(#{table_name}.#{attribute} AS CHAR(255)) LIKE ?"
|
93
|
+
end
|
92
94
|
end
|
93
|
-
end
|
94
95
|
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
96
|
+
def extend_query(block)
|
97
|
+
@simple_search_query_conditions = ["(#{@simple_search_query_conditions.join})"]
|
98
|
+
extended_query = block.call @simple_search_term
|
99
|
+
extend_simple_search(extended_query) if extended_query
|
100
|
+
end
|
100
101
|
|
101
|
-
|
102
|
-
|
102
|
+
def extend_simple_search(extended_query)
|
103
|
+
raise Errors::INVALID_TYPE unless extended_query.is_a?(Array)
|
103
104
|
|
104
|
-
|
105
|
-
|
105
|
+
extended_query_condition = extended_query[0]
|
106
|
+
extended_query_values = extended_query - [extended_query[0]]
|
106
107
|
|
107
|
-
|
108
|
-
|
109
|
-
|
108
|
+
if extended_query_condition.count("?") != extended_query_values.size
|
109
|
+
raise Errors::INVALID_CONDITION
|
110
|
+
end
|
110
111
|
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
@simple_search_query_conditions << " #{extended_query_condition}"
|
113
|
+
@simple_search_query_values += extended_query_values
|
114
|
+
end
|
114
115
|
end
|
115
116
|
end
|
@@ -1,10 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module RubySimpleSearch
|
2
4
|
module Errors
|
3
|
-
ATTRIBUTES_MISSING =
|
4
|
-
INVALID_CONDITION = "Extended query's array conditions are wrong"
|
5
|
-
INVALID_TYPE =
|
6
|
-
INVALID_PATTERN = "Looks like given pattern is wrong, valid pattern list is '#{LIKE_PATTERNS.keys}'"
|
7
|
-
SEARCH_ARG_TYPE =
|
8
|
-
WRONG_ATTRIBUTES = "`simple_search_arguments` method's arguments should be in symbol format"
|
5
|
+
ATTRIBUTES_MISSING = "Simple search attributes are missing"
|
6
|
+
INVALID_CONDITION = "Extended query's array conditions are wrong"
|
7
|
+
INVALID_TYPE = "Extended query is not an array type"
|
8
|
+
INVALID_PATTERN = "Looks like given pattern is wrong, valid pattern list is '#{LIKE_PATTERNS.keys}'"
|
9
|
+
SEARCH_ARG_TYPE = "`search_term` argument is not a string"
|
10
|
+
WRONG_ATTRIBUTES = "`simple_search_arguments` method's arguments should be in symbol format"
|
9
11
|
end
|
10
12
|
end
|
metadata
CHANGED
@@ -1,29 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby_simple_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.
|
4
|
+
version: 2.0.1
|
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: 2020-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '5'
|
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'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: simplecov
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: activerecord
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,8 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
154
168
|
- !ruby/object:Gem::Version
|
155
169
|
version: '0'
|
156
170
|
requirements: []
|
157
|
-
|
158
|
-
rubygems_version: 2.4.5.1
|
171
|
+
rubygems_version: 3.1.2
|
159
172
|
signing_key:
|
160
173
|
specification_version: 4
|
161
174
|
summary: The simplest way to search the data
|