jsonapi-materializer 1.2.0 → 2.0.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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9556e194a2e69b081bad43a8ddfbab23c55b9707a2250598222854943118b50c
|
4
|
+
data.tar.gz: 14653444ae546e3f3ecb4a4611b9739e247538b82d032a475b7931fe8a97f2ef
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a0985c0cfcbdab3e5c6d6115838a3f30699e40a819cd4c00c2a58b9bf27181e5d5bce12d4da230e6d2c3ba9564df9e04a777a5086e5074c1bf6b757ff7b8d456
|
7
|
+
data.tar.gz: 82e6d60d6806c0d37c043bbe16783dacb9601eb9651c200decf078efc012292d87d36a605b55697e8c6ef8afeb0bc03bf300eca8104b1e707aadc4f18438e0d9
|
@@ -13,13 +13,6 @@ module JSONAPI
|
|
13
13
|
attr_writer(:includes)
|
14
14
|
attr_writer(:pagination)
|
15
15
|
|
16
|
-
delegate(:first_page?, to: :object)
|
17
|
-
delegate(:prev_page, to: :object)
|
18
|
-
delegate(:total_pages, to: :object)
|
19
|
-
delegate(:next_page, to: :object)
|
20
|
-
delegate(:last_page?, to: :object)
|
21
|
-
delegate(:limit_value, to: :object)
|
22
|
-
|
23
16
|
def as_json(*)
|
24
17
|
{
|
25
18
|
links: pagination,
|
@@ -55,11 +48,11 @@ module JSONAPI
|
|
55
48
|
private def pagination
|
56
49
|
if @pagination
|
57
50
|
{
|
58
|
-
first: (pagination_link_template.expand(offset: 1, limit:
|
59
|
-
prev: (pagination_link_template.expand(offset:
|
60
|
-
self: (
|
61
|
-
next: (pagination_link_template.expand(offset:
|
62
|
-
last: (pagination_link_template.expand(offset:
|
51
|
+
first: (pagination_link_template.expand(offset: 1, limit: @pagination.in).to_s unless @pagination.pages.zero? || @pagination.prev.nil?),
|
52
|
+
prev: (pagination_link_template.expand(offset: @pagination.prev, limit: @pagination.in).to_s unless @pagination.pages.zero? || @pagination.prev.nil?),
|
53
|
+
self: (pagination_link_template.expand(offset: @pagination.page, limit: @pagination.in).to_s unless @pagination.pages.zero?),
|
54
|
+
next: (pagination_link_template.expand(offset: @pagination.next, limit: @pagination.in).to_s unless @pagination.pages.zero? || @pagination.next.nil?),
|
55
|
+
last: (pagination_link_template.expand(offset: @pagination.pages, limit: @pagination.in).to_s unless @pagination.pages.zero? || @pagination.next.nil?)
|
63
56
|
}.compact
|
64
57
|
else
|
65
58
|
{}
|
@@ -93,9 +86,8 @@ module JSONAPI
|
|
93
86
|
end
|
94
87
|
end
|
95
88
|
end
|
96
|
-
end.uniq.map(&:as_data)
|
89
|
+
end.uniq.compact.map(&:as_data)
|
97
90
|
end
|
98
|
-
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
|
99
91
|
end
|
100
92
|
end
|
101
93
|
end
|
@@ -2,9 +2,26 @@
|
|
2
2
|
|
3
3
|
require("spec_helper")
|
4
4
|
|
5
|
+
PaginationAdapter = Struct.new(:in, :page, :pages, :prev, :next)
|
6
|
+
|
5
7
|
RSpec.describe(JSONAPI::Materializer::Collection) do
|
6
8
|
let(:described_class) { ArticleMaterializer::Collection }
|
7
|
-
let(:collection)
|
9
|
+
let(:collection) do
|
10
|
+
described_class.new(
|
11
|
+
object:,
|
12
|
+
includes: [["comments"], ["author"]],
|
13
|
+
pagination:
|
14
|
+
)
|
15
|
+
end
|
16
|
+
let(:pagination) do
|
17
|
+
PaginationAdapter.new(
|
18
|
+
10, # items in page
|
19
|
+
9, # current page
|
20
|
+
10, # total pages
|
21
|
+
100, # previous page
|
22
|
+
80 # next page
|
23
|
+
)
|
24
|
+
end
|
8
25
|
|
9
26
|
describe("#as_json") do
|
10
27
|
subject { collection.as_json.deep_stringify_keys }
|
@@ -54,12 +71,84 @@ RSpec.describe(JSONAPI::Materializer::Collection) do
|
|
54
71
|
}]))
|
55
72
|
end
|
56
73
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
74
|
+
context("when on the first page") do
|
75
|
+
let(:pagination) do
|
76
|
+
PaginationAdapter.new(
|
77
|
+
10, # items in page
|
78
|
+
1, # current page
|
79
|
+
10, # total pages
|
80
|
+
nil, # previous page
|
81
|
+
2 # next page
|
82
|
+
)
|
83
|
+
end
|
84
|
+
|
85
|
+
it("has a links key at root with pagination") do
|
86
|
+
expect(subject.fetch("links")).to(eq(
|
87
|
+
"last" => "http://example.com/articles?page[offset]=10&page[limit]=10",
|
88
|
+
"next" => "http://example.com/articles?page[offset]=2&page[limit]=10",
|
89
|
+
"self" => "http://example.com/articles?page[offset]=1&page[limit]=10"
|
90
|
+
))
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context("when on the last page") do
|
95
|
+
let(:pagination) do
|
96
|
+
PaginationAdapter.new(
|
97
|
+
10, # items in page
|
98
|
+
10, # current page
|
99
|
+
10, # total pages
|
100
|
+
9, # previous page
|
101
|
+
nil # next page
|
102
|
+
)
|
103
|
+
end
|
104
|
+
|
105
|
+
it("has a links key at root with pagination") do
|
106
|
+
expect(subject.fetch("links")).to(eq(
|
107
|
+
"first" => "http://example.com/articles?page[offset]=1&page[limit]=10",
|
108
|
+
"prev" => "http://example.com/articles?page[offset]=9&page[limit]=10",
|
109
|
+
"self" => "http://example.com/articles?page[offset]=10&page[limit]=10"
|
110
|
+
))
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
context("when on the middle page") do
|
115
|
+
let(:pagination) do
|
116
|
+
PaginationAdapter.new(
|
117
|
+
10, # items in page
|
118
|
+
5, # current page
|
119
|
+
10, # total pages
|
120
|
+
4, # previous page
|
121
|
+
6 # next page
|
122
|
+
)
|
123
|
+
end
|
124
|
+
|
125
|
+
it("has a links key at root with pagination") do
|
126
|
+
expect(subject.fetch("links")).to(eq(
|
127
|
+
"first" => "http://example.com/articles?page[offset]=1&page[limit]=10",
|
128
|
+
"last" => "http://example.com/articles?page[offset]=10&page[limit]=10",
|
129
|
+
"next" => "http://example.com/articles?page[offset]=6&page[limit]=10",
|
130
|
+
"prev" => "http://example.com/articles?page[offset]=4&page[limit]=10",
|
131
|
+
"self" => "http://example.com/articles?page[offset]=5&page[limit]=10"
|
132
|
+
))
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context("when on the only page") do
|
137
|
+
let(:pagination) do
|
138
|
+
PaginationAdapter.new(
|
139
|
+
10, # items in page
|
140
|
+
1, # current page
|
141
|
+
1, # total pages
|
142
|
+
nil, # previous page
|
143
|
+
nil # next page
|
144
|
+
)
|
145
|
+
end
|
146
|
+
|
147
|
+
it("has a links key at root with pagination") do
|
148
|
+
expect(subject.fetch("links")).to(eq(
|
149
|
+
"self" => "http://example.com/articles?page[offset]=1&page[limit]=10"
|
150
|
+
))
|
151
|
+
end
|
63
152
|
end
|
64
153
|
|
65
154
|
it("has a included key at root with included models") do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-materializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kurtis Rainbolt-Greene
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|