jsonapi-utils 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67e5ec59fd75caee715c6e6cac80336259a4cb91
|
4
|
+
data.tar.gz: d138871fc7ea80fb1c4f2277f7716aaf27615760
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bc928e3037b6e4483f54238c081e969bf62a51845e243f039240b449af8b5f1867a2c9889381bc6b7d334e482cd580fd914c77bb40215c1d36d74875af6d358
|
7
|
+
data.tar.gz: c2616c4ee9bb69e292ca6b0625322ba3b1add51ab9a8f7698fe6877e6911efa4c7a4e4c7eb4afce6abe0e876ba2c46da73175d2551abd742fe3f16dacffe70a8
|
@@ -171,7 +171,7 @@ module JSONAPI
|
|
171
171
|
end
|
172
172
|
end
|
173
173
|
|
174
|
-
# Apply some result options like pagination params and count to
|
174
|
+
# Apply some result options like pagination params and record count to collection responses.
|
175
175
|
#
|
176
176
|
# @param records [ActiveRecord::Relation, Hash, Array<Hash>]
|
177
177
|
# Object to be formatted into JSON
|
@@ -192,7 +192,7 @@ module JSONAPI
|
|
192
192
|
end
|
193
193
|
|
194
194
|
if JSONAPI.configuration.top_level_meta_include_record_count
|
195
|
-
data[:record_count] =
|
195
|
+
data[:record_count] = record_count_for(records, options)
|
196
196
|
end
|
197
197
|
end
|
198
198
|
end
|
@@ -2,6 +2,8 @@ module JSONAPI
|
|
2
2
|
module Utils
|
3
3
|
module Support
|
4
4
|
module Pagination
|
5
|
+
RecordCountError = Class.new(ArgumentError)
|
6
|
+
|
5
7
|
# Apply proper pagination to the records.
|
6
8
|
#
|
7
9
|
# @param records [ActiveRecord::Relation, Array] collection of records
|
@@ -33,7 +35,23 @@ module JSONAPI
|
|
33
35
|
# @api public
|
34
36
|
def pagination_params(records, options)
|
35
37
|
return {} unless JSONAPI.configuration.top_level_links_include_pagination
|
36
|
-
paginator.links_page_params(record_count:
|
38
|
+
paginator.links_page_params(record_count: record_count_for(records, options))
|
39
|
+
end
|
40
|
+
|
41
|
+
# Apply memoization to the record count result avoiding duplicate counts.
|
42
|
+
#
|
43
|
+
# @param records [ActiveRecord::Relation, Array] collection of records
|
44
|
+
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
|
45
|
+
#
|
46
|
+
# @param options [Hash] JU's options
|
47
|
+
# e.g.: { resource: V2::UserResource, count: 100 }
|
48
|
+
#
|
49
|
+
# @return [Integer]
|
50
|
+
# e.g.: 42
|
51
|
+
#
|
52
|
+
# @api public
|
53
|
+
def record_count_for(records, options)
|
54
|
+
@record_count ||= count_records(records, options)
|
37
55
|
end
|
38
56
|
|
39
57
|
private
|
@@ -130,14 +148,49 @@ module JSONAPI
|
|
130
148
|
#
|
131
149
|
# @api private
|
132
150
|
def count_records(records, options)
|
133
|
-
if options[:count].
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
151
|
+
return options[:count].to_i if options[:count].is_a?(Numeric)
|
152
|
+
|
153
|
+
case records
|
154
|
+
when ActiveRecord::Relation then count_records_from_database(records, options)
|
155
|
+
when Array then records.length
|
156
|
+
else raise RecordCountError, "Can't count records with the given options"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
# Count records from the datatase applying the given request filters
|
161
|
+
# and skipping things like eager loading, grouping and sorting.
|
162
|
+
#
|
163
|
+
# @param records [ActiveRecord::Relation, Array] collection of records
|
164
|
+
# e.g.: User.all or [{ id: 1, name: 'Tiago' }, { id: 2, name: 'Doug' }]
|
165
|
+
#
|
166
|
+
# @param options [Hash] JU's options
|
167
|
+
# e.g.: { resource: V2::UserResource, count: 100 }
|
168
|
+
#
|
169
|
+
# @return [Integer]
|
170
|
+
# e.g.: 42
|
171
|
+
#
|
172
|
+
# @api private
|
173
|
+
def count_records_from_database(records, options)
|
174
|
+
records = apply_filter(records, options) if params[:filter].present?
|
175
|
+
count = -> (records, except:) do
|
176
|
+
records.except(*except).count(distinct_count_sql(records))
|
140
177
|
end
|
178
|
+
count.(records, except: %i(includes group order))
|
179
|
+
rescue ActiveRecord::StatementInvalid
|
180
|
+
count.(records, except: %i(group order))
|
181
|
+
end
|
182
|
+
|
183
|
+
# Build the SQL distinct count with some reflection on the "records" object.
|
184
|
+
#
|
185
|
+
# @param records [ActiveRecord::Relation] collection of records
|
186
|
+
# e.g.: User.all
|
187
|
+
#
|
188
|
+
# @return [String]
|
189
|
+
# e.g.: "DISTINCT users.id"
|
190
|
+
#
|
191
|
+
# @api private
|
192
|
+
def distinct_count_sql(records)
|
193
|
+
"DISTINCT #{records.table_name}.#{records.primary_key}"
|
141
194
|
end
|
142
195
|
end
|
143
196
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tiago Guedes
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-01-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: jsonapi-resources
|
@@ -151,8 +151,7 @@ dependencies:
|
|
151
151
|
- - ">="
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: '0'
|
154
|
-
description:
|
155
|
-
(http://jsosapi.org)
|
154
|
+
description: Build JSON API-compliant APIs on Rails with no (or less) learning curve.
|
156
155
|
email:
|
157
156
|
- tiagopog@gmail.com
|
158
157
|
- douglas@beautydate.com.br
|
@@ -200,9 +199,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
200
199
|
version: '0'
|
201
200
|
requirements: []
|
202
201
|
rubyforge_project:
|
203
|
-
rubygems_version: 2.
|
202
|
+
rubygems_version: 2.5.2
|
204
203
|
signing_key:
|
205
204
|
specification_version: 4
|
206
|
-
summary: JSON::Utils is a simple way to get a full-featured JSON API
|
207
|
-
|
205
|
+
summary: JSON::Utils is a simple way to get a full-featured JSON API on your Rails
|
206
|
+
application.
|
208
207
|
test_files: []
|