bullet_train-api 1.21.1 → 1.22.0
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 +4 -4
- data/app/controllers/concerns/api/controllers/base.rb +43 -17
- data/lib/bullet_train/api/version.rb +1 -1
- data/lib/bullet_train/api.rb +0 -1
- metadata +1 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3b7a3614e89a9ae0ddffe302d628205c02131faf10a20d98895d58b82001a2d8
|
4
|
+
data.tar.gz: bfc27b83f7888559aad0dd0bf2153bbc402829012e1960d0a5c9d6fcc662900a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1141c3b197a201cbe7fdc5a7a571f5935e4308f23aa46822850a5a93388a7e27cdbec0812753584d5c5188a8e0be96ccb4b9f3f7b5d1eecc784874f3cd6bf779
|
7
|
+
data.tar.gz: 6983da4808837519ac2332fa6ffd2f53755b044b5162de6ee22e664f16f167d2ad4398688a1193e5f0600f8a7c595aec9d193b623a26aae2d53d4481f4f35279
|
@@ -1,6 +1,3 @@
|
|
1
|
-
require "pagy_cursor/pagy/extras/cursor"
|
2
|
-
require "pagy_cursor/pagy/extras/uuid_cursor"
|
3
|
-
|
4
1
|
module Api::Controllers::Base
|
5
2
|
extend ActiveSupport::Concern
|
6
3
|
|
@@ -18,6 +15,9 @@ module Api::Controllers::Base
|
|
18
15
|
before_action :set_default_response_format
|
19
16
|
after_action :set_pagination_headers
|
20
17
|
|
18
|
+
before_action :apply_filters, only: [:index]
|
19
|
+
before_action :apply_pagination, only: [:index]
|
20
|
+
|
21
21
|
def modify_url_params(url, new_params)
|
22
22
|
uri = URI.parse(url)
|
23
23
|
query = Rack::Utils.parse_query(uri.query)
|
@@ -30,18 +30,24 @@ module Api::Controllers::Base
|
|
30
30
|
|
31
31
|
def set_pagination_headers
|
32
32
|
return unless @pagy
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
link_value = "<#{modify_url_params(request.url, after: next_cursor)}>; rel=\"next\""
|
39
|
-
response.headers["Link"] = link_header ? "#{link_header}, #{link_value}" : link_value
|
40
|
-
response.headers["Pagination-Next"] = next_cursor
|
41
|
-
end
|
33
|
+
if collection_has_more?
|
34
|
+
link_header = response.headers["Link"]
|
35
|
+
link_value = "<#{modify_url_params(request.url, after: last_id_in_collection)}>; rel=\"next\""
|
36
|
+
response.headers["Link"] = link_header ? "#{link_header}, #{link_value}" : link_value
|
37
|
+
response.headers["Pagination-Next"] = last_id_in_collection
|
42
38
|
end
|
43
39
|
end
|
44
40
|
|
41
|
+
def collection_has_more?
|
42
|
+
return false unless last_id_in_collection
|
43
|
+
remaining_collection = collection.limit(nil).order(id: :asc).where("id > ?", last_id_in_collection)
|
44
|
+
remaining_collection.any?
|
45
|
+
end
|
46
|
+
|
47
|
+
def last_id_in_collection
|
48
|
+
@last_id_in_collection ||= collection&.last&.id
|
49
|
+
end
|
50
|
+
|
45
51
|
rescue_from CanCan::AccessDenied, ActiveRecord::RecordNotFound do |exception|
|
46
52
|
render json: {error: "Not found"}, status: :not_found
|
47
53
|
end
|
@@ -49,8 +55,6 @@ module Api::Controllers::Base
|
|
49
55
|
rescue_from NotAuthenticatedError do |exception|
|
50
56
|
render json: {error: "Invalid token or no user signed in"}, status: :unauthorized
|
51
57
|
end
|
52
|
-
|
53
|
-
before_action :apply_pagination, only: [:index]
|
54
58
|
end
|
55
59
|
|
56
60
|
def permitted_fields
|
@@ -97,10 +101,32 @@ module Api::Controllers::Base
|
|
97
101
|
@collection_variable ||= "@#{self.class.name.split("::").last.gsub("Controller", "").underscore}"
|
98
102
|
end
|
99
103
|
|
104
|
+
def collection
|
105
|
+
@collection ||= instance_variable_get(collection_variable)
|
106
|
+
end
|
107
|
+
|
108
|
+
def collection=(new_collection)
|
109
|
+
@collection = new_collection
|
110
|
+
instance_variable_set collection_variable, new_collection
|
111
|
+
end
|
112
|
+
|
113
|
+
def apply_filters
|
114
|
+
# An empty method that descendant controllers can override
|
115
|
+
# A possible implementation might look like:
|
116
|
+
#
|
117
|
+
# self.collection = collection.where(status: params[:filter_status]) if params[:filter_status]
|
118
|
+
#
|
119
|
+
# Keep in mind that for adding filters to auto-generated documentation, you
|
120
|
+
# will need to override index.yaml.erb or _paths.yaml.erbs.
|
121
|
+
end
|
122
|
+
|
100
123
|
def apply_pagination
|
101
|
-
|
102
|
-
|
103
|
-
|
124
|
+
pagination_collection = collection.order(id: :asc)
|
125
|
+
if params[:after]
|
126
|
+
pagination_collection = pagination_collection.where("id > ?", params[:after])
|
127
|
+
end
|
128
|
+
@pagy, pagination_collection = pagy(pagination_collection)
|
129
|
+
self.collection = pagination_collection
|
104
130
|
end
|
105
131
|
|
106
132
|
def set_default_response_format
|
data/lib/bullet_train/api.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bullet_train-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.22.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Culver
|
@@ -93,20 +93,6 @@ dependencies:
|
|
93
93
|
- - "~>"
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '8'
|
96
|
-
- !ruby/object:Gem::Dependency
|
97
|
-
name: pagy_cursor
|
98
|
-
requirement: !ruby/object:Gem::Requirement
|
99
|
-
requirements:
|
100
|
-
- - ">="
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: '0'
|
103
|
-
type: :runtime
|
104
|
-
prerelease: false
|
105
|
-
version_requirements: !ruby/object:Gem::Requirement
|
106
|
-
requirements:
|
107
|
-
- - ">="
|
108
|
-
- !ruby/object:Gem::Version
|
109
|
-
version: '0'
|
110
96
|
- !ruby/object:Gem::Dependency
|
111
97
|
name: doorkeeper
|
112
98
|
requirement: !ruby/object:Gem::Requirement
|