abstract_finder 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +0 -0
- data/lib/abstract_finder/base_finder.rb +66 -0
- data/lib/abstract_finder.rb +54 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 77a7e235ebd8fd73ee68168acc1a73d22209d32b7279d1b1b2301f60ad95bdcf
|
4
|
+
data.tar.gz: effe24917541a0e2f74ef5f8ba5f3033796e4fda372ca7def0508e5851e4035f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0f2e8cf4e7a50bf30abfc088d8c8f748451db1722ab62a3c15fa32c02a430e2b61282d5f24ebdbd0f16cc8f8e49b2807a2191f10b12c66cf7085e93c6bbfb8c
|
7
|
+
data.tar.gz: 0b765daf8f62b5364ace03cfac75dd4554388cfb29bf31365a65c7593103da718b2a0e042a0d6d6ada3346d19c227a39e1e1a7292bdc02e837c2577f430a4d82
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2021 Mr Ivanov
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
File without changes
|
@@ -0,0 +1,66 @@
|
|
1
|
+
# Class Base finder
|
2
|
+
class BaseFinder
|
3
|
+
DEFAULT_PER_PAGE = 10
|
4
|
+
|
5
|
+
attr_reader :collection
|
6
|
+
|
7
|
+
def self.call(*args)
|
8
|
+
instance = new(*args)
|
9
|
+
instance.call
|
10
|
+
instance
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(filters)
|
14
|
+
@filters = filters
|
15
|
+
end
|
16
|
+
|
17
|
+
def call
|
18
|
+
load_collection
|
19
|
+
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def page
|
24
|
+
filter_by(:page)&.to_i || 1
|
25
|
+
end
|
26
|
+
|
27
|
+
def per_page
|
28
|
+
filter_by(:per_page)&.to_i || DEFAULT_PER_PAGE
|
29
|
+
end
|
30
|
+
|
31
|
+
def total
|
32
|
+
return 0 if @collection.blank?
|
33
|
+
|
34
|
+
@total ||= @collection.count
|
35
|
+
end
|
36
|
+
|
37
|
+
def meta
|
38
|
+
{
|
39
|
+
page: page,
|
40
|
+
per_page: per_page,
|
41
|
+
total: @total
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
protected
|
46
|
+
|
47
|
+
def load_collection
|
48
|
+
@collection = nil
|
49
|
+
end
|
50
|
+
|
51
|
+
# Internal: check if filter by param specified
|
52
|
+
# param_name - name of filters param
|
53
|
+
#
|
54
|
+
# Returns Boolean
|
55
|
+
def filtered_by?(param_name)
|
56
|
+
filter_by(param_name).present?
|
57
|
+
end
|
58
|
+
|
59
|
+
# Internal: filter value for parameter
|
60
|
+
# param_name - name of filters param
|
61
|
+
#
|
62
|
+
# Returns String
|
63
|
+
def filter_by(param_name)
|
64
|
+
@filters[param_name]
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative 'abstract_finder/base_finder'
|
2
|
+
|
3
|
+
# AbstractFinder
|
4
|
+
class AbstractFinder < BaseFinder
|
5
|
+
attr_reader :model, :associations
|
6
|
+
|
7
|
+
SORT = %w[asc desc].freeze
|
8
|
+
|
9
|
+
def self.call(model, associations = [], args = [])
|
10
|
+
new(model, associations, args).call
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(model, associations, args) # rubocop:disable Lint/MissingSuper
|
14
|
+
@model = model
|
15
|
+
@associations = associations
|
16
|
+
@filters = args
|
17
|
+
end
|
18
|
+
|
19
|
+
def call
|
20
|
+
load_collection
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
protected
|
26
|
+
|
27
|
+
def load_collection
|
28
|
+
base_collection
|
29
|
+
query if @filters[:q].present? && @collection.respond_to?(:search_by)
|
30
|
+
order if @filters[:order].present?
|
31
|
+
filter if @filters[:filter_by].present? && @collection.respond_to?(:filter_by)
|
32
|
+
total
|
33
|
+
finalize_collection
|
34
|
+
end
|
35
|
+
|
36
|
+
def base_collection
|
37
|
+
@collection = model.all.includes(associations)
|
38
|
+
end
|
39
|
+
|
40
|
+
def finalize_collection
|
41
|
+
@collection = @collection.page(page).per(per_page)
|
42
|
+
end
|
43
|
+
|
44
|
+
# define #search_by method in query model and use this finder
|
45
|
+
def query
|
46
|
+
@collection = model.includes(associations).search_by(@filters[:q]) if model.respond_to? :search_by
|
47
|
+
end
|
48
|
+
|
49
|
+
def filter; end
|
50
|
+
|
51
|
+
def order
|
52
|
+
@collection = @collection.order(created_at: @filters[:order])
|
53
|
+
end
|
54
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: abstract_finder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aleksei Ivanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: kaminari
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: Gem to simplify AR queries, it executes query and includes relations.
|
28
|
+
email: alx.ekb@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- lib/abstract_finder.rb
|
36
|
+
- lib/abstract_finder/base_finder.rb
|
37
|
+
homepage: https://github.com/alxekb/abstract_finder
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata: {}
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.1.2
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: Abstract finder for rails api with gueries, sort, filter and pagination
|
60
|
+
test_files: []
|