spyke 1.8.8 → 1.8.9
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/lib/spyke/associations/association.rb +4 -15
- data/lib/spyke/associations/has_many.rb +6 -2
- data/lib/spyke/http.rb +11 -3
- data/lib/spyke/version.rb +1 -1
- data/test/associations_test.rb +13 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0b73653b9952f7257a838b16b11abc70770ce471
|
4
|
+
data.tar.gz: 3f753f7c24bcaad6bc5365a2721a28e5b1df99fc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40e9090eed648f8a51d6af63ad3e1476b9d64e5e054cd90c42810952670c747e3cc1af14af3ba0b31dedf981257b097b368c6225ca9f00d136ab1fd3b197e815
|
7
|
+
data.tar.gz: 97b830a87cbec15d5f4c74226b0a60841b81b9b9e46a6e60a6b3c4ca0d27f19331845c58ccf3a2d9bbc7103d588b9fbbc48d613c95415e2be0dc8a55dd6a51b9
|
@@ -15,17 +15,6 @@ module Spyke
|
|
15
15
|
find_one # Override for plural associations that return an association object
|
16
16
|
end
|
17
17
|
|
18
|
-
def find_one
|
19
|
-
result = super
|
20
|
-
update_parent(result) if result
|
21
|
-
end
|
22
|
-
|
23
|
-
def find_some
|
24
|
-
result = super
|
25
|
-
update_parent(result) if result.any?
|
26
|
-
result
|
27
|
-
end
|
28
|
-
|
29
18
|
def assign_nested_attributes(attributes)
|
30
19
|
update_parent new(attributes)
|
31
20
|
end
|
@@ -55,15 +44,15 @@ module Spyke
|
|
55
44
|
end
|
56
45
|
|
57
46
|
def fetch_embedded
|
58
|
-
if
|
59
|
-
Result.new(data:
|
47
|
+
if embedded
|
48
|
+
Result.new(data: embedded)
|
60
49
|
elsif !uri
|
61
50
|
Result.new(data: nil)
|
62
51
|
end
|
63
52
|
end
|
64
53
|
|
65
|
-
def
|
66
|
-
|
54
|
+
def embedded
|
55
|
+
parent.attributes[name]
|
67
56
|
end
|
68
57
|
|
69
58
|
def update_parent(value)
|
@@ -24,7 +24,7 @@ module Spyke
|
|
24
24
|
|
25
25
|
def combine_with_existing(incoming)
|
26
26
|
return incoming unless primary_keys_present_in_existing?
|
27
|
-
combined =
|
27
|
+
combined = embedded_attributes + incoming
|
28
28
|
group_by_primary_key(combined).flat_map do |primary_key, hashes|
|
29
29
|
if primary_key.present?
|
30
30
|
hashes.reduce(:merge)
|
@@ -39,13 +39,17 @@ module Spyke
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def primary_keys_present_in_existing?
|
42
|
-
|
42
|
+
embedded_attributes && embedded_attributes.any? { |attr| attr.has_key?('id') }
|
43
43
|
end
|
44
44
|
|
45
45
|
def clear_existing!
|
46
46
|
update_parent []
|
47
47
|
end
|
48
48
|
|
49
|
+
def embedded_attributes
|
50
|
+
@embedded_attributes ||= parent.attributes.to_params[name]
|
51
|
+
end
|
52
|
+
|
49
53
|
def add_to_parent(record)
|
50
54
|
parent.attributes[name] ||= []
|
51
55
|
parent.attributes[name] << record
|
data/lib/spyke/http.rb
CHANGED
@@ -20,11 +20,11 @@ module Spyke
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def new_instance_from_result(result)
|
23
|
-
|
23
|
+
new_or_return result.data if result.data
|
24
24
|
end
|
25
25
|
|
26
26
|
def new_collection_from_result(result)
|
27
|
-
Collection.new Array(result.data).map { |record|
|
27
|
+
Collection.new Array(result.data).map { |record| new_or_return(record) }, result.metadata
|
28
28
|
end
|
29
29
|
|
30
30
|
def uri(uri_template = nil)
|
@@ -60,6 +60,14 @@ module Spyke
|
|
60
60
|
end
|
61
61
|
end
|
62
62
|
|
63
|
+
def new_or_return(attributes_or_object)
|
64
|
+
if attributes_or_object.is_a?(Spyke::Base)
|
65
|
+
attributes_or_object
|
66
|
+
else
|
67
|
+
new attributes_or_object
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
63
71
|
def superclass_uri
|
64
72
|
superclass.uri.dup.freeze if superclass != Base
|
65
73
|
end
|
@@ -82,7 +90,7 @@ module Spyke
|
|
82
90
|
end
|
83
91
|
|
84
92
|
def uri
|
85
|
-
Path.new(@uri_template, attributes)
|
93
|
+
Path.new(@uri_template, attributes) if @uri_template
|
86
94
|
end
|
87
95
|
|
88
96
|
private
|
data/lib/spyke/version.rb
CHANGED
data/test/associations_test.rb
CHANGED
@@ -184,7 +184,7 @@ module Spyke
|
|
184
184
|
|
185
185
|
assert_equal %w{ Salt }, recipe.ingredients.map(&:name)
|
186
186
|
assert_equal({ 'recipe' => { 'groups' => [{ 'recipe_id' => 1, 'ingredients' => [{ 'group_id' => nil, 'name' => 'Salt' }] }] } }, recipe.to_params)
|
187
|
-
assert_equal({ 'group' => { '
|
187
|
+
assert_equal({ 'group' => { 'ingredients' => [{ 'group_id' => nil, 'name' => 'Salt' }] } }, recipe.groups.first.to_params)
|
188
188
|
end
|
189
189
|
|
190
190
|
def test_deep_build_has_many_association_with_scope
|
@@ -377,6 +377,18 @@ module Spyke
|
|
377
377
|
assert_equal %w{ Salt Spoon }, recipe.ingredients.map(&:name)
|
378
378
|
end
|
379
379
|
|
380
|
+
def test_not_caching_result_with_different_params
|
381
|
+
endpoint_1 = stub_request(:get, 'http://sushi.com/recipes/1/groups/1').to_return_json(result: { id: 1 })
|
382
|
+
endpoint_2 = stub_request(:get, 'http://sushi.com/recipes/1/groups/2').to_return_json(result: { id: 2 })
|
383
|
+
|
384
|
+
recipe = Recipe.new(id: 1)
|
385
|
+
recipe.groups.find(1)
|
386
|
+
recipe.groups.find(2)
|
387
|
+
|
388
|
+
assert_requested endpoint_1, times: 1
|
389
|
+
assert_requested endpoint_2, times: 1
|
390
|
+
end
|
391
|
+
|
380
392
|
def test_namespaced_model
|
381
393
|
tip_endpoint = stub_request(:get, 'http://sushi.com/tips/1').to_return_json(result: { id: 1 })
|
382
394
|
likes_endpoint = stub_request(:get, 'http://sushi.com/tips/1/likes')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spyke
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.8.
|
4
|
+
version: 1.8.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jens Balvig
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|