api_only_pagination 0.1.0 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +123 -0
- data/README.md +37 -18
- data/api_only_pagination.gemspec +6 -5
- data/lib/api_only_pagination/active_record_paginate.rb +24 -0
- data/lib/api_only_pagination/configuration.rb +13 -0
- data/lib/api_only_pagination/paginate.rb +55 -0
- data/lib/api_only_pagination/paginated_data.rb +21 -0
- data/lib/api_only_pagination/pagination.rb +22 -0
- data/lib/api_only_pagination/version.rb +1 -1
- data/lib/api_only_pagination.rb +8 -8
- metadata +55 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 826fde0fb392a394358a65a5759171b45cc47f449785bf2877d6728f7dfb4f0e
|
4
|
+
data.tar.gz: 1b13d3e52a7cdac0de53a8a54c5751283fb5e6419fdf0ac85457b74124b0e600
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14eb5e9588e159cc148149135bdc6ef7b2b6879394300d4aa71d6da36fc7fe8049357aab703c9f2e3488fc7290c9671bede4eda9c9b2562fbb875d962b25935a
|
7
|
+
data.tar.gz: 47d8acd2be2b1f6ea72a689124156e9728404be2214232782fa733b01912e8a6875f0c62bd67eecba7c4b784d21d92ad0d5697b4f1cde2975d47e95eb3872bd5
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
api_only_pagination (0.1.4)
|
5
|
+
activerecord (>= 5.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actionpack (6.1.7.3)
|
11
|
+
actionview (= 6.1.7.3)
|
12
|
+
activesupport (= 6.1.7.3)
|
13
|
+
rack (~> 2.0, >= 2.0.9)
|
14
|
+
rack-test (>= 0.6.3)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
17
|
+
actionview (6.1.7.3)
|
18
|
+
activesupport (= 6.1.7.3)
|
19
|
+
builder (~> 3.1)
|
20
|
+
erubi (~> 1.4)
|
21
|
+
rails-dom-testing (~> 2.0)
|
22
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
23
|
+
activemodel (6.1.7.3)
|
24
|
+
activesupport (= 6.1.7.3)
|
25
|
+
activerecord (6.1.7.3)
|
26
|
+
activemodel (= 6.1.7.3)
|
27
|
+
activesupport (= 6.1.7.3)
|
28
|
+
activesupport (6.1.7.3)
|
29
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
30
|
+
i18n (>= 1.6, < 2)
|
31
|
+
minitest (>= 5.1)
|
32
|
+
tzinfo (~> 2.0)
|
33
|
+
zeitwerk (~> 2.3)
|
34
|
+
ast (2.4.2)
|
35
|
+
builder (3.2.4)
|
36
|
+
concurrent-ruby (1.2.2)
|
37
|
+
crass (1.0.6)
|
38
|
+
diff-lcs (1.5.0)
|
39
|
+
erubi (1.12.0)
|
40
|
+
i18n (1.13.0)
|
41
|
+
concurrent-ruby (~> 1.0)
|
42
|
+
json (2.6.1)
|
43
|
+
loofah (2.20.0)
|
44
|
+
crass (~> 1.0.2)
|
45
|
+
nokogiri (>= 1.5.9)
|
46
|
+
method_source (1.0.0)
|
47
|
+
minitest (5.18.0)
|
48
|
+
nokogiri (1.13.10-x86_64-darwin)
|
49
|
+
racc (~> 1.4)
|
50
|
+
parallel (1.21.0)
|
51
|
+
parser (3.2.1.1)
|
52
|
+
ast (~> 2.4.1)
|
53
|
+
racc (1.6.2)
|
54
|
+
rack (2.2.7)
|
55
|
+
rack-test (2.1.0)
|
56
|
+
rack (>= 1.3)
|
57
|
+
rails-dom-testing (2.0.3)
|
58
|
+
activesupport (>= 4.2.0)
|
59
|
+
nokogiri (>= 1.6)
|
60
|
+
rails-html-sanitizer (1.5.0)
|
61
|
+
loofah (~> 2.19, >= 2.19.1)
|
62
|
+
railties (6.1.7.3)
|
63
|
+
actionpack (= 6.1.7.3)
|
64
|
+
activesupport (= 6.1.7.3)
|
65
|
+
method_source
|
66
|
+
rake (>= 12.2)
|
67
|
+
thor (~> 1.0)
|
68
|
+
rainbow (3.1.1)
|
69
|
+
rake (13.0.6)
|
70
|
+
regexp_parser (2.3.0)
|
71
|
+
rexml (3.2.5)
|
72
|
+
rspec (3.10.0)
|
73
|
+
rspec-core (~> 3.10.0)
|
74
|
+
rspec-expectations (~> 3.10.0)
|
75
|
+
rspec-mocks (~> 3.10.0)
|
76
|
+
rspec-core (3.10.1)
|
77
|
+
rspec-support (~> 3.10.0)
|
78
|
+
rspec-expectations (3.10.1)
|
79
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
80
|
+
rspec-support (~> 3.10.0)
|
81
|
+
rspec-mocks (3.10.2)
|
82
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
83
|
+
rspec-support (~> 3.10.0)
|
84
|
+
rspec-rails (4.0.2)
|
85
|
+
actionpack (>= 4.2)
|
86
|
+
activesupport (>= 4.2)
|
87
|
+
railties (>= 4.2)
|
88
|
+
rspec-core (~> 3.10)
|
89
|
+
rspec-expectations (~> 3.10)
|
90
|
+
rspec-mocks (~> 3.10)
|
91
|
+
rspec-support (~> 3.10)
|
92
|
+
rspec-support (3.10.2)
|
93
|
+
rubocop (1.48.1)
|
94
|
+
json (~> 2.3)
|
95
|
+
parallel (~> 1.10)
|
96
|
+
parser (>= 3.2.0.0)
|
97
|
+
rainbow (>= 2.2.2, < 4.0)
|
98
|
+
regexp_parser (>= 1.8, < 3.0)
|
99
|
+
rexml (>= 3.2.5, < 4.0)
|
100
|
+
rubocop-ast (>= 1.26.0, < 2.0)
|
101
|
+
ruby-progressbar (~> 1.7)
|
102
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
103
|
+
rubocop-ast (1.27.0)
|
104
|
+
parser (>= 3.2.1.0)
|
105
|
+
ruby-progressbar (1.11.0)
|
106
|
+
thor (1.2.1)
|
107
|
+
tzinfo (2.0.6)
|
108
|
+
concurrent-ruby (~> 1.0)
|
109
|
+
unicode-display_width (2.4.2)
|
110
|
+
zeitwerk (2.6.8)
|
111
|
+
|
112
|
+
PLATFORMS
|
113
|
+
x86_64-darwin-20
|
114
|
+
|
115
|
+
DEPENDENCIES
|
116
|
+
api_only_pagination!
|
117
|
+
rake (~> 13.0)
|
118
|
+
rspec (~> 3.6)
|
119
|
+
rspec-rails (~> 4.0)
|
120
|
+
rubocop (~> 1.21)
|
121
|
+
|
122
|
+
BUNDLED WITH
|
123
|
+
2.4.9
|
data/README.md
CHANGED
@@ -1,24 +1,43 @@
|
|
1
1
|
# ApiOnlyPagination
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
$ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
14
|
-
|
15
|
-
If bundler is not being used to manage dependencies, install the gem by executing:
|
16
|
-
|
17
|
-
$ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
|
18
|
-
|
2
|
+
Lightweight pagination gem that works only with Activerecord for Api only applications
|
3
|
+
## Installation
|
4
|
+
To install this gem,
|
5
|
+
```
|
6
|
+
$ gem install api_only_pagination
|
7
|
+
```
|
8
|
+
or add it to your Gemfile
|
9
|
+
```
|
10
|
+
gem 'api_only_pagination'
|
11
|
+
```
|
19
12
|
## Usage
|
20
13
|
|
21
|
-
|
14
|
+
Configure your pagination adding api_only_pagination.rb file in config/initializers unless it will take default configurations.
|
15
|
+
|
16
|
+
```
|
17
|
+
ApiOnlyPagination.configure do |config|
|
18
|
+
config.default_page_size = 3
|
19
|
+
config.max_per_page = 100
|
20
|
+
config.default_page = 1
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Now you can call chain .do_paginate(page, per_page) to your activerecord query.
|
25
|
+
```
|
26
|
+
response = User.do_paginate(1,20)
|
27
|
+
```
|
28
|
+
|
29
|
+
The response will be
|
30
|
+
|
31
|
+
|
32
|
+
{
|
33
|
+
"data" => [],
|
34
|
+
"per_page" => 3,
|
35
|
+
"current_page" => 1,
|
36
|
+
"total_pages" => 2,
|
37
|
+
"total_records" => 5
|
38
|
+
}
|
39
|
+
|
40
|
+
|
22
41
|
|
23
42
|
## Development
|
24
43
|
|
data/api_only_pagination.gemspec
CHANGED
@@ -10,15 +10,14 @@ Gem::Specification.new do |spec|
|
|
10
10
|
|
11
11
|
spec.summary = "pagination with api response only"
|
12
12
|
spec.description = "pagination with api response only"
|
13
|
-
|
13
|
+
spec.homepage = "https://github.com/ashok1/api_only_pagination"
|
14
14
|
spec.license = "MIT"
|
15
15
|
spec.required_ruby_version = ">= 2.6.0"
|
16
16
|
|
17
17
|
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
# spec.metadata["changelog_uri"] = ""
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
22
21
|
|
23
22
|
# Specify which files should be added to the gem when it is released.
|
24
23
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
@@ -30,7 +29,9 @@ Gem::Specification.new do |spec|
|
|
30
29
|
spec.bindir = "exe"
|
31
30
|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
31
|
spec.require_paths = ["lib"]
|
33
|
-
|
32
|
+
spec.add_dependency "activerecord", ">= 5.0"
|
33
|
+
spec.add_development_dependency "rspec", "~> 3.6"
|
34
|
+
spec.add_development_dependency "rspec-rails", "~> 4.0"
|
34
35
|
# Uncomment to register a new dependency of your gem
|
35
36
|
# spec.add_dependency "example-gem", "~> 1.0"
|
36
37
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
4
|
+
|
5
|
+
module ApiOnlyPagination
|
6
|
+
module ActiveRecordPaginate
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
module ClassMethods
|
9
|
+
%i[do_paginate paginate paginated_response].each do |access_method|
|
10
|
+
define_method(access_method) do |page = ApiOnlyPagination.configuration.default_page, per_page = ApiOnlyPagination.configuration.default_page_size| # or even |*args|
|
11
|
+
pagination = ApiOnlyPagination::Pagination.new(all, { page: page, per_page: per_page })
|
12
|
+
meta_data = pagination.metadata
|
13
|
+
|
14
|
+
ApiOnlyPagination::PaginatedData
|
15
|
+
.new(data: pagination.results,
|
16
|
+
per_page: meta_data.per_page,
|
17
|
+
current_page: meta_data.page,
|
18
|
+
total_pages: meta_data.total_pages,
|
19
|
+
total_records: meta_data.count)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiOnlyPagination
|
4
|
+
class Paginate
|
5
|
+
attr_reader :page, :count, :per_page
|
6
|
+
|
7
|
+
def initialize(page, per_page, count)
|
8
|
+
@page = (page || ApiOnlyPagination.configuration.default_page).to_i
|
9
|
+
@count = count
|
10
|
+
@per_page = [(per_page.to_i || ApiOnlyPagination.configuration.default_page_size),
|
11
|
+
ApiOnlyPagination.configuration.max_per_page].min
|
12
|
+
|
13
|
+
validate_params
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_params
|
17
|
+
@page = @page.zero? ? ApiOnlyPagination.configuration.default_page : @page
|
18
|
+
@per_page = @per_page.zero? ? ApiOnlyPagination.configuration.default_page_size : @per_page
|
19
|
+
end
|
20
|
+
|
21
|
+
def offset
|
22
|
+
return 0 if page == 1
|
23
|
+
|
24
|
+
per_page * (page.to_i - 1)
|
25
|
+
end
|
26
|
+
|
27
|
+
def next_page
|
28
|
+
page + 1 unless last_page?
|
29
|
+
end
|
30
|
+
|
31
|
+
def next_page?
|
32
|
+
page < total_pages
|
33
|
+
end
|
34
|
+
|
35
|
+
def previous_page
|
36
|
+
page - 1 unless first_page?
|
37
|
+
end
|
38
|
+
|
39
|
+
def previous_page?
|
40
|
+
page > 1
|
41
|
+
end
|
42
|
+
|
43
|
+
def last_page?
|
44
|
+
page == total_pages
|
45
|
+
end
|
46
|
+
|
47
|
+
def first_page?
|
48
|
+
page == 1
|
49
|
+
end
|
50
|
+
|
51
|
+
def total_pages
|
52
|
+
(count / per_page.to_f).ceil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Main module
|
4
|
+
module ApiOnlyPagination
|
5
|
+
# Response object class
|
6
|
+
class PaginatedData
|
7
|
+
attr_accessor :data,
|
8
|
+
:per_page,
|
9
|
+
:current_page,
|
10
|
+
:total_pages,
|
11
|
+
:total_records
|
12
|
+
|
13
|
+
def initialize(data:, per_page:, current_page:, total_pages:, total_records:)
|
14
|
+
@data = data
|
15
|
+
@per_page = per_page
|
16
|
+
@current_page = current_page
|
17
|
+
@total_pages = total_pages
|
18
|
+
@total_records = total_records
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ApiOnlyPagination
|
4
|
+
class Pagination
|
5
|
+
attr_reader :collection, :params
|
6
|
+
|
7
|
+
def initialize(collection, params = {})
|
8
|
+
@collection = collection
|
9
|
+
@params = params.merge(count: collection.count)
|
10
|
+
end
|
11
|
+
|
12
|
+
def metadata
|
13
|
+
@metadata ||= ApiOnlyPagination::Paginate.new(params[:page], params[:per_page], params[:count])
|
14
|
+
end
|
15
|
+
|
16
|
+
def results
|
17
|
+
collection
|
18
|
+
.limit(metadata.per_page)
|
19
|
+
.offset(metadata.offset)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/api_only_pagination.rb
CHANGED
@@ -1,19 +1,14 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require "active_record"
|
3
4
|
require_relative "api_only_pagination/version"
|
4
5
|
require_relative "api_only_pagination/configuration"
|
5
6
|
require_relative "api_only_pagination/paginate"
|
6
7
|
require_relative "api_only_pagination/pagination"
|
8
|
+
require_relative "api_only_pagination/paginated_data"
|
9
|
+
require_relative "api_only_pagination/active_record_paginate"
|
7
10
|
|
8
11
|
module ApiOnlyPagination
|
9
|
-
def paginated_response(collection ,
|
10
|
-
page = ApiOnlyPagination.configuration.default_page,
|
11
|
-
per_page = ApiOnlyPagination.configuration.default_page_size )
|
12
|
-
pagination = ApiOnlyPagination::Pagination.new(collection, {page: page, per_page: per_page})
|
13
|
-
meta_data = pagination.metadata
|
14
|
-
meta_data.as_json.merge({ data: pagination.results })
|
15
|
-
end
|
16
|
-
|
17
12
|
class << self
|
18
13
|
def configuration
|
19
14
|
@configuration ||= Configuration.new
|
@@ -28,3 +23,8 @@ module ApiOnlyPagination
|
|
28
23
|
end
|
29
24
|
end
|
30
25
|
end
|
26
|
+
|
27
|
+
ActiveSupport.on_load(:active_record) do
|
28
|
+
include ApiOnlyPagination::ActiveRecordPaginate
|
29
|
+
ActiveRecord::Relation.include(ApiOnlyPagination::ActiveRecordPaginate)
|
30
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_only_pagination
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashok
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
12
|
-
dependencies:
|
11
|
+
date: 2023-05-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec-rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4.0'
|
13
55
|
description: pagination with api response only
|
14
56
|
email:
|
15
57
|
- kingkoirala1@gmail.com
|
@@ -21,17 +63,25 @@ files:
|
|
21
63
|
- CHANGELOG.md
|
22
64
|
- CODE_OF_CONDUCT.md
|
23
65
|
- Gemfile
|
66
|
+
- Gemfile.lock
|
24
67
|
- LICENSE.txt
|
25
68
|
- README.md
|
26
69
|
- Rakefile
|
27
70
|
- api_only_pagination.gemspec
|
28
71
|
- lib/api_only_pagination.rb
|
72
|
+
- lib/api_only_pagination/active_record_paginate.rb
|
73
|
+
- lib/api_only_pagination/configuration.rb
|
74
|
+
- lib/api_only_pagination/paginate.rb
|
75
|
+
- lib/api_only_pagination/paginated_data.rb
|
76
|
+
- lib/api_only_pagination/pagination.rb
|
29
77
|
- lib/api_only_pagination/version.rb
|
30
78
|
- sig/api_only_pagination.rbs
|
31
|
-
homepage:
|
79
|
+
homepage: https://github.com/ashok1/api_only_pagination
|
32
80
|
licenses:
|
33
81
|
- MIT
|
34
|
-
metadata:
|
82
|
+
metadata:
|
83
|
+
homepage_uri: https://github.com/ashok1/api_only_pagination
|
84
|
+
source_code_uri: https://github.com/ashok1/api_only_pagination
|
35
85
|
post_install_message:
|
36
86
|
rdoc_options: []
|
37
87
|
require_paths:
|