http-api-tools 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +4 -2
- data/lib/hat.rb +1 -0
- data/lib/hat/base_json_serializer.rb +0 -4
- data/lib/hat/expanded_relation_includes.rb +4 -4
- data/lib/hat/relation_includes.rb +6 -0
- data/lib/hat/version.rb +1 -1
- data/spec/hat/expanded_relation_includes_spec.rb +1 -2
- data/spec/hat/relation_includes_spec.rb +11 -0
- data/spec/hat/sideloading/json_serializer_spec.rb +0 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
YTBjYWE0MmIwNjVlOTJlNGJkOWJmZjJlMzViNGI5YjBlMDQ0YWM4Yg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzE2M2U4MGZjMzUzNGViOTE4YjAwYjdmN2FhZDgyZjU3YThkMjBjOQ==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
MWY0NGQ4NDQ5NzdiMTExZmM4NzYwNTgxZWY2ZGFiMDk5MmJhNzZlNGZiNjA2
|
10
|
+
NjA3MGMwY2QyODQ3MTIzNDAwOTQxYjNhMTg3MTNmMDk1MzcwYTk4Njg2ZGE1
|
11
|
+
NGQwY2Y1NmM2ZmQyYjgzNzBlMjdlMjYwZWNiNTA0ZDk3YzJlZmM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MjRmNTgxNmFjYzEyYzcyNDhlOTZkMTg0OTU1MTcyZDE4ZGFlYzE0YzhjYTc3
|
14
|
+
MWUzODkyZjFlNmNmOWEwNmM3MGQ5NzQzZjNlOTlhZWZkODkxZWIxMDBiMTMz
|
15
|
+
NWQ2NmU5ZDM2MjZkZDY4ZTc1NWJkYzU2NDBmMWZhNGI3ODM1YTM=
|
data/README.md
CHANGED
@@ -244,15 +244,17 @@ and splat into the serializer includes:
|
|
244
244
|
|
245
245
|
and/or active record queries:
|
246
246
|
|
247
|
-
`User.find(params[:id]).includes(*
|
247
|
+
`User.find(params[:id]).includes(*relation_includes.for_query(UserSerializer))`
|
248
248
|
|
249
249
|
When providing the includes for an active record query, we actually want a deeper set of includes in order to account for the ids fetched for has_many relationships. If we passed the same set of includes to the query as we pass to the serializer, we'd end up with n+1 queries when fetching the ids for the has_many relationships.
|
250
250
|
|
251
|
-
Calling `
|
251
|
+
Calling `relation_includes.for_query(UserSerializer)` will figure out the minimum set of includes that are required based on the following:
|
252
252
|
|
253
253
|
* The models and their relationships
|
254
254
|
* The relationships actually being serialized
|
255
255
|
|
256
|
+
**** Note that this particular API is pretty rough at the moment and likely to change once we find a nicer way of describing this feature.
|
257
|
+
|
256
258
|
##### Restricting what is included
|
257
259
|
Once you expose what can be included as a query string parameter you risk exposing too much information or poorly considered api calls that fetch too much. This can be countered by defining what is `includable` for each serializer when it's being used as the root serializer for a json response.
|
258
260
|
|
data/lib/hat.rb
CHANGED
@@ -3,22 +3,22 @@
|
|
3
3
|
module Hat
|
4
4
|
class ExpandedRelationIncludes
|
5
5
|
|
6
|
-
def initialize(relation_includes,
|
6
|
+
def initialize(relation_includes, serializer_class)
|
7
7
|
@relation_includes = relation_includes
|
8
|
-
@
|
8
|
+
@serializer_class = serializer_class
|
9
9
|
end
|
10
10
|
|
11
11
|
def to_a
|
12
12
|
@expanded_includes ||= begin
|
13
13
|
expanded_includes = []
|
14
|
-
expand_includes(
|
14
|
+
expand_includes(serializer_class, relation_includes, expanded_includes)
|
15
15
|
expanded_includes
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
19
|
private
|
20
20
|
|
21
|
-
attr_reader :
|
21
|
+
attr_reader :serializer_class, :relation_includes
|
22
22
|
|
23
23
|
def expand_includes(target_serializer_class, base_includes, expanded_includes)
|
24
24
|
|
@@ -77,6 +77,12 @@ module Hat
|
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
80
|
+
#Return an expanded version of the includes for use in a query.
|
81
|
+
#This api is still pretty rough and likely to change
|
82
|
+
def for_query(serializer_class)
|
83
|
+
RelationIncludes.new(*ExpandedRelationIncludes.new(self, serializer_class))
|
84
|
+
end
|
85
|
+
|
80
86
|
private
|
81
87
|
|
82
88
|
attr_accessor :includes
|
data/lib/hat/version.rb
CHANGED
@@ -6,8 +6,7 @@ module Hat
|
|
6
6
|
|
7
7
|
describe "#to_a" do
|
8
8
|
|
9
|
-
let(:
|
10
|
-
let(:expanded_includes) { ExpandedRelationIncludes.new(includes, serializer) }
|
9
|
+
let(:expanded_includes) { ExpandedRelationIncludes.new(includes, Hat::Sideloading::PersonSerializer) }
|
11
10
|
|
12
11
|
context 'with single-level includes' do
|
13
12
|
|
@@ -181,5 +181,16 @@ module Hat
|
|
181
181
|
end
|
182
182
|
end
|
183
183
|
|
184
|
+
describe "#for_query" do
|
185
|
+
|
186
|
+
let(:includes) { RelationIncludes.new(:employer, { skills: [:person] }).for_query(Hat::Sideloading::PersonSerializer) }
|
187
|
+
|
188
|
+
it "creates includes for included relationships and has_many relationships for fetching ids" do
|
189
|
+
expect(includes.find(:employer)).to eq({ employer: [:employees] })
|
190
|
+
expect(includes.find(:skills)).to eq({ skills: [{ person: [:skills] }] })
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
184
195
|
end
|
185
196
|
end
|
@@ -78,17 +78,6 @@ module Hat
|
|
78
78
|
expect(serialized[:linked][:skills].first[:name]).to eql person.skills.first.name
|
79
79
|
end
|
80
80
|
|
81
|
-
describe "#includes_for_query" do
|
82
|
-
|
83
|
-
let(:includes_for_query) { serializer.includes_for_query }
|
84
|
-
|
85
|
-
it "expands includes for included relationships and has_many relationships for fetching ids" do
|
86
|
-
expect(includes_for_query.find(:employer)).to eq({ employer: [:employees] })
|
87
|
-
expect(includes_for_query.find(:skills)).to eq({ skills: [{ person: [:skills] }] })
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
91
|
-
|
92
81
|
end
|
93
82
|
|
94
83
|
end
|