segugio 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +10 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +52 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +12 -0
- data/lib/segugio/search.rb +167 -0
- data/lib/segugio/searchable.rb +72 -0
- data/lib/segugio/version.rb +5 -0
- data/lib/segugio/wrappers/active_record/base.rb +9 -0
- data/lib/segugio.rb +10 -0
- data/sig/segugio.rbs +4 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 21f1f902e957723cc82d4f131a7b2bd8ba251afbd5a13f5d44b0fc8dbd09af84
|
4
|
+
data.tar.gz: 05a0dc7ec8ddd435fa6852f33d577709586eca6858e9047228d00d695526110f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c552b10a4a4ea1f8f4db063a42fb3e373e2c44dded30ee14c5566de63a3173d1dfc33bbcdf1a5f34788f7b6380bde6810e20a8ea9bc9f88b64838504f37b8e6b
|
7
|
+
data.tar.gz: 5670ace531c9bcb397bffe27fa4fadb72ca52725cffa36b7f13a69a468659148ca1c2284f02ba60f45284aae9f04e636415da05e1079159798c2e1e4140d0e9b
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at groza@opinioni.net. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
segugio (0.1.0)
|
5
|
+
activerecord (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (7.0.4)
|
11
|
+
activesupport (= 7.0.4)
|
12
|
+
activerecord (7.0.4)
|
13
|
+
activemodel (= 7.0.4)
|
14
|
+
activesupport (= 7.0.4)
|
15
|
+
activesupport (7.0.4)
|
16
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
17
|
+
i18n (>= 1.6, < 2)
|
18
|
+
minitest (>= 5.1)
|
19
|
+
tzinfo (~> 2.0)
|
20
|
+
concurrent-ruby (1.1.10)
|
21
|
+
diff-lcs (1.5.0)
|
22
|
+
i18n (1.12.0)
|
23
|
+
concurrent-ruby (~> 1.0)
|
24
|
+
minitest (5.16.3)
|
25
|
+
rake (13.0.6)
|
26
|
+
rspec (3.12.0)
|
27
|
+
rspec-core (~> 3.12.0)
|
28
|
+
rspec-expectations (~> 3.12.0)
|
29
|
+
rspec-mocks (~> 3.12.0)
|
30
|
+
rspec-core (3.12.0)
|
31
|
+
rspec-support (~> 3.12.0)
|
32
|
+
rspec-expectations (3.12.1)
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
34
|
+
rspec-support (~> 3.12.0)
|
35
|
+
rspec-mocks (3.12.1)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-support (3.12.0)
|
39
|
+
tzinfo (2.0.5)
|
40
|
+
concurrent-ruby (~> 1.0)
|
41
|
+
|
42
|
+
PLATFORMS
|
43
|
+
x86_64-linux
|
44
|
+
|
45
|
+
DEPENDENCIES
|
46
|
+
bundler (~> 2.0)
|
47
|
+
rake (~> 13.0)
|
48
|
+
rspec (~> 3.0)
|
49
|
+
segugio!
|
50
|
+
|
51
|
+
BUNDLED WITH
|
52
|
+
2.3.24
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Groza Sergiu
|
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,115 @@
|
|
1
|
+
# Segugio
|
2
|
+
|
3
|
+
Segugio is a Ruby gem that simplifies the implementation of search functionality in Rails using ActiveRecord.
|
4
|
+
With this gem, you can easily search, filter and sort results in a simple and intuitive way.
|
5
|
+
|
6
|
+
It is built on ActiveRecord and provides a clean and easy-to-use interface for creating advanced search queries.
|
7
|
+
It also provides a variety of utility methods for handling results, allowing you to efficiently sort and filter data.
|
8
|
+
|
9
|
+
In addition, Segugio is fully customizable, supporting the creation of custom queries to meet the specific needs of your applications.
|
10
|
+
|
11
|
+
In summary, Segugio is an essential tool for anyone looking to implement advanced search functionality in Rails using ActiveRecord,
|
12
|
+
providing a simple and efficient solution for managing the data of your applications.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Install the gem and add to the application's Gemfile by executing:
|
17
|
+
|
18
|
+
$ bundle add segugio
|
19
|
+
|
20
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
21
|
+
|
22
|
+
$ gem install segugio
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# app/models/user.rb
|
28
|
+
|
29
|
+
class User < ApplicationRecord
|
30
|
+
# Fields to be used for records search by string (text search)
|
31
|
+
query_fields: :id, :email, :username, :first_name, :last_name
|
32
|
+
|
33
|
+
# Fields that can be used to filter records by exact value
|
34
|
+
filter_fields: :id, :email, :username, :role
|
35
|
+
|
36
|
+
# Fields that can be used to order returned results
|
37
|
+
order_fields: :id, :email, :username, :first_name, :last_name, :created_at, :updated_at
|
38
|
+
# ... other code ...
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Search for users containing `John Doe` in `query_fields`:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
User.search(query: 'John Doe')
|
46
|
+
```
|
47
|
+
|
48
|
+
Filter users by role and return results ordered by `email` and `first_name`:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
User.search(filters: {role: %w[admin moderator]}, order: %i[email first_name])
|
52
|
+
```
|
53
|
+
|
54
|
+
## Custom `search` method name
|
55
|
+
|
56
|
+
Sometimes you might want to call `search` method with a different name (e.g. `my_search`), in order to avoid collisions with already existing `search` method. For doing so you can specify a custom search method name for Segugio:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# config/initializers/segugio.rb
|
60
|
+
require 'segugio'
|
61
|
+
|
62
|
+
Segugio.search_method_name = :my_search
|
63
|
+
```
|
64
|
+
|
65
|
+
Then to search for users do:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
User.my_search(query: 'John Doe')
|
69
|
+
```
|
70
|
+
|
71
|
+
## Manually activate Segugio
|
72
|
+
|
73
|
+
By default Segugio will wrap `ActiveRecord::Base` in order to automatically add `search`, `query_fields`, `filter_fields` and `order_fields` methods. If you don't wont this behaviour (i.e. already defined a method called `search` in your model), you can load Segugio manually and add its methods only to desired models.
|
74
|
+
|
75
|
+
For doing so, you need to add `require: false` to your Gemfile:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# Gemfile
|
79
|
+
|
80
|
+
gem 'segugio', require: false
|
81
|
+
```
|
82
|
+
|
83
|
+
This will disable autoload of Segugio gem in yout application.
|
84
|
+
|
85
|
+
Then, to add Segugio methods to a specific model, you can do:
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
require 'segugio/searchable'
|
89
|
+
|
90
|
+
class User < ApplicationRecord
|
91
|
+
extend Segugio::Searchable
|
92
|
+
|
93
|
+
query_fields :id, :email, :username, :first_name, :last_name
|
94
|
+
|
95
|
+
# ... other code ...
|
96
|
+
end
|
97
|
+
```
|
98
|
+
|
99
|
+
Now you'll be able to user `search` method on `User` model:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
User.search(query: 'john')
|
103
|
+
```
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/demetra-it/segugio. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/demetra-it/segugio/blob/master/CODE_OF_CONDUCT.md).
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
112
|
+
|
113
|
+
## Code of Conduct
|
114
|
+
|
115
|
+
Everyone interacting in the Segugio project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/demetra-it/segugio/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,167 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Segugio
|
4
|
+
# Implements a feature for generic search, that allows to search using ILIKE operator, filter records by exact value
|
5
|
+
# and specify result order.
|
6
|
+
class Search
|
7
|
+
attr_reader :result
|
8
|
+
|
9
|
+
def initialize(relation, options = {})
|
10
|
+
@relation = relation
|
11
|
+
options = options.to_h.with_indifferent_access
|
12
|
+
@configs = options[:configs] || {}
|
13
|
+
@options = options
|
14
|
+
|
15
|
+
parse_options
|
16
|
+
|
17
|
+
@result = relation.all
|
18
|
+
|
19
|
+
configure!
|
20
|
+
generate_search_result!
|
21
|
+
end
|
22
|
+
|
23
|
+
def collection_name
|
24
|
+
@collection_name ||= @relation.model_name.collection.split('/').last
|
25
|
+
end
|
26
|
+
|
27
|
+
def logger
|
28
|
+
@logger ||= Rails.logger
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def parse_options
|
34
|
+
parse_string_options
|
35
|
+
|
36
|
+
@configs = (@options[:configs] || {})
|
37
|
+
|
38
|
+
@query = @options[:query].to_s.strip
|
39
|
+
@filters = (@options[:filters] || {})
|
40
|
+
@exclude = (@options[:exclude] || {})
|
41
|
+
@order = @options[:order] || []
|
42
|
+
end
|
43
|
+
|
44
|
+
# In this way you can pass variables from client side with `JSON.stringify`
|
45
|
+
def parse_string_options
|
46
|
+
%i[configs filters order exclude].each do |option|
|
47
|
+
@options[option] = JSON.parse(@options[option]) if @options[option].instance_of?(String)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
# Configure search with relation settings
|
52
|
+
def configure!
|
53
|
+
@columns = @relation.columns.map(&:name)
|
54
|
+
@query_fields = @configs[:query_fields] || []
|
55
|
+
@filter_fields = @configs[:filter_fields] || []
|
56
|
+
@order_fields = (@configs[:order_fields] || []).map(&:to_s)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Use search parameters to generate an ActiveRecord Relation which represents
|
60
|
+
# the search
|
61
|
+
def generate_search_result!
|
62
|
+
filter_by_query!
|
63
|
+
apply_filters!
|
64
|
+
apply_exclude!
|
65
|
+
apply_order!
|
66
|
+
end
|
67
|
+
|
68
|
+
# Generate the part of the query which filters record by a text search
|
69
|
+
def filter_by_query!
|
70
|
+
return if @query.blank? || @query_fields.blank?
|
71
|
+
|
72
|
+
# Generate an array with conditions (ex. ['name ILIKE ? OR title ILIKE ?', '%xyz%', '%xyz%'])
|
73
|
+
conditions = []
|
74
|
+
conditions << @query_fields.map { |field| "#{@relation.table_name}.#{field}::text ILIKE ?" }.join(' OR ')
|
75
|
+
@query_fields.each do
|
76
|
+
conditions << "%#{@query}%"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Use conditions as filter
|
80
|
+
@result = @result.where(conditions)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Filter records by a set of conditions
|
84
|
+
def apply_filters!
|
85
|
+
# Iterate over @filter_fields and set filter conditions on result
|
86
|
+
@filter_fields.each do |column|
|
87
|
+
# TODO: handle filters on array columns
|
88
|
+
@result = @result.where(@collection_name => { column => @filters[column] }) if @filters[column]
|
89
|
+
end
|
90
|
+
|
91
|
+
# Warn about invalid filter fields
|
92
|
+
invalid = @filters.keys.map(&:to_s) - @filter_fields.map(&:to_s)
|
93
|
+
logger.warn "Invalid filter fields: #{invalid.inspect}" unless invalid.empty?
|
94
|
+
end
|
95
|
+
|
96
|
+
# Exclude from results records by a set of conditions
|
97
|
+
def apply_exclude!
|
98
|
+
# Iterate over @filter_fields and set exclude conditions on result
|
99
|
+
@filter_fields.each do |column|
|
100
|
+
@result = @result.where.not(@collection_name => { column => @exclude[column] }) if @exclude[column]
|
101
|
+
end
|
102
|
+
|
103
|
+
# Warn about invalid exclude fields
|
104
|
+
invalid = @exclude.keys.map(&:to_s) - @filter_fields.map(&:to_s)
|
105
|
+
logger.warn "Invalid exclude fields: #{invalid.inspect}" unless invalid.empty?
|
106
|
+
end
|
107
|
+
|
108
|
+
# Apply the specified order
|
109
|
+
def apply_order!
|
110
|
+
return if @order.blank? || @order_fields.blank?
|
111
|
+
|
112
|
+
@result = @result.order(generate_order_fields.join(', '))
|
113
|
+
end
|
114
|
+
|
115
|
+
# Generate order fields from search params
|
116
|
+
def generate_order_fields
|
117
|
+
@order.each_with_object([]) do |item, result|
|
118
|
+
case item
|
119
|
+
when String, Symbol
|
120
|
+
# When order item is a String or a Symbol, just check
|
121
|
+
# if it's valid and use that value as order field
|
122
|
+
if @order_fields.include?(item.to_s)
|
123
|
+
result << "#{@relation.table_name}.#{item}"
|
124
|
+
else
|
125
|
+
# Report invalid order item
|
126
|
+
logger.warn "Invalid order item: #{item.inspect}"
|
127
|
+
end
|
128
|
+
when Hash
|
129
|
+
# When order item is a Hash, we have to build order field from
|
130
|
+
# the values specified within item data.
|
131
|
+
order_item = item.with_indifferent_access
|
132
|
+
if @order_fields.include?(order_item[:field].to_s)
|
133
|
+
# If item field is correct, build order field from its data
|
134
|
+
result << build_order_field_from_hash(order_item)
|
135
|
+
else
|
136
|
+
# Report invalid order item
|
137
|
+
logger.warn "Invalid order field: #{order_item.inspect} (valid fields are: #{@order_fields.inspect})"
|
138
|
+
end
|
139
|
+
else
|
140
|
+
# Order item has unsupported type
|
141
|
+
logger.warn "Invalid order item type: #{item.class} (String, Symbol or Hash expected)"
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# Build order field from a hash like { field: 'name', asc: true|false|nil, nulls_first: true|false|nil }
|
147
|
+
def build_order_field_from_hash(data)
|
148
|
+
# Use field name as base for order field
|
149
|
+
field = "#{@relation.table_name}.#{data[:field]}"
|
150
|
+
|
151
|
+
if data.key?(:asc)
|
152
|
+
# When :asc is specified, add "ASC" or "DESC" when its value is "true" or "false" respectively
|
153
|
+
field += ' ASC' if Service::Utils.true?(data[:asc])
|
154
|
+
field += ' DESC' if Service::Utils.false?(data[:asc])
|
155
|
+
end
|
156
|
+
|
157
|
+
if data.key?(:nulls_first)
|
158
|
+
# When :nulls_first is specified, add "NULLS FIRST" or "NULLS LAST" when its value
|
159
|
+
# is "true" or "false" respectively
|
160
|
+
field += ' NULLS FIRST' if Service::Utils.true?(data[:nulls_first])
|
161
|
+
field += ' NULLS LAST' if Service::Utils.false?(data[:nulls_first])
|
162
|
+
end
|
163
|
+
|
164
|
+
field
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'search'
|
4
|
+
|
5
|
+
module Segugio
|
6
|
+
class << self
|
7
|
+
# Define an accessor where to store current search method name.
|
8
|
+
attr_accessor :__search_method_name
|
9
|
+
|
10
|
+
# Returns current search method name of Segugio.
|
11
|
+
def search_method_name
|
12
|
+
__sarch_method_name
|
13
|
+
end
|
14
|
+
|
15
|
+
# Updates current search method name of Segugio.
|
16
|
+
# This method will remove the old search method and define a new one with
|
17
|
+
# the given name. e.g.: `Segugio.search_method_name = :my_search` will define
|
18
|
+
# a new `my_search` method on all models that extend `Segugio::Searchable`.
|
19
|
+
def search_method_name=(name)
|
20
|
+
return if name.to_s.strip == '' || name.to_s.strip == __search_method_name
|
21
|
+
|
22
|
+
current_method_name = __search_method_name || 'search'
|
23
|
+
|
24
|
+
Segugio::Searchable.remove_method(current_method_name) if Segugio::Searchable.method_defined?(current_method_name)
|
25
|
+
self.__search_method_name = name
|
26
|
+
|
27
|
+
Segugio::Searchable.define_method(name) do |query: '', filters: {}, exclude: {}, order: [], configs: {}|
|
28
|
+
segugio_perform_search(query: query, filters: filters, exclude: exclude, order: order, configs: configs)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module Searchable
|
34
|
+
# Defines the fields that will be used to perform the search.
|
35
|
+
def query_fields(*args)
|
36
|
+
@query_fields = args.flatten unless args.blank?
|
37
|
+
@query_fields || []
|
38
|
+
end
|
39
|
+
|
40
|
+
# Defines the fields that can be used for filters.
|
41
|
+
def filter_fields(*args)
|
42
|
+
@filter_fields = args.flatten unless args.blank?
|
43
|
+
@filter_fields || []
|
44
|
+
end
|
45
|
+
|
46
|
+
# Defines the fields that can be used for ordering.
|
47
|
+
def order_fields(*args)
|
48
|
+
@order_fields = args.flatten unless args.blank?
|
49
|
+
@order_fields || columns.map(&:name)
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
# Implements the `search` method for the model.
|
55
|
+
def segugio_perform_search(query: '', filters: {}, exclude: {}, order: [], configs: {})
|
56
|
+
Segugio::Search.new(
|
57
|
+
self,
|
58
|
+
query: query,
|
59
|
+
filters: filters,
|
60
|
+
exclude: exclude,
|
61
|
+
order: order,
|
62
|
+
configs: {
|
63
|
+
query_fields: configs[:query_fields] || query_fields,
|
64
|
+
filter_fields: configs[:filter_fields] || filter_fields,
|
65
|
+
order_fields: configs[:order_fields] || order_fields
|
66
|
+
}
|
67
|
+
).result
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
Segugio.search_method_name = :search
|
data/lib/segugio.rb
ADDED
data/sig/segugio.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: segugio
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Groza Sergiu
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-01-18 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: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.2'
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: 5.2.0
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - "~>"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '5.2'
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 5.2.0
|
47
|
+
description: |
|
48
|
+
Segugio is a Ruby gem that simplifies the implementation of search functionality in Rails using ActiveRecord.
|
49
|
+
With this gem, you can easily search, filter and sort results in a simple and intuitive way.
|
50
|
+
|
51
|
+
It is built on ActiveRecord and provides a clean and easy-to-use interface for creating advanced search queries.
|
52
|
+
It also provides a variety of utility methods for handling results, allowing you to efficiently sort and filter data.
|
53
|
+
|
54
|
+
In addition, Segugio is fully customizable, supporting the creation of custom queries to meet the specific needs of your applications.
|
55
|
+
|
56
|
+
In summary, Segugio is an essential tool for anyone looking to implement advanced search functionality in Rails using ActiveRecord,
|
57
|
+
providing a simple and efficient solution for managing the data of your applications.
|
58
|
+
email:
|
59
|
+
- groza@opinioni.net
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".rspec"
|
65
|
+
- ".rubocop.yml"
|
66
|
+
- CHANGELOG.md
|
67
|
+
- CODE_OF_CONDUCT.md
|
68
|
+
- Gemfile
|
69
|
+
- Gemfile.lock
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/segugio.rb
|
74
|
+
- lib/segugio/search.rb
|
75
|
+
- lib/segugio/searchable.rb
|
76
|
+
- lib/segugio/version.rb
|
77
|
+
- lib/segugio/wrappers/active_record/base.rb
|
78
|
+
- sig/segugio.rbs
|
79
|
+
homepage: https://github.com/demetra-it/segugio
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata:
|
83
|
+
homepage_uri: https://github.com/demetra-it/segugio
|
84
|
+
source_code_uri: https://github.com/demetra-it/segugio
|
85
|
+
changelog_uri: https://github.com/demetra-it/segugio/blob/master/CHANGELOG.md
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 2.6.0
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - ">="
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubygems_version: 3.3.7
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: A Ruby gem for fast and easy implementation of search functionality in Rails
|
105
|
+
with ActiveRecord.
|
106
|
+
test_files: []
|