spyke 1.3.0 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/spyke/associations/has_many.rb +9 -0
- data/lib/spyke/version.rb +1 -1
- data/test/associations_test.rb +17 -8
- 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: 18786d5a16e1af110485c81e9c7174507bad9ffc
|
4
|
+
data.tar.gz: a12316a477c6c9baf8dc95985ac0c378279cc88f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: caa7363096f246e4a8547499545b288262c6cae186f6130071284619d7f7ccd1f3ddddaa48f840134d7748e88b2c670a532759dcc161e6d21658809bd538529d
|
7
|
+
data.tar.gz: 02a3ece5fbcba5c35723b40459cb85593c5e72be3a3ada1d1840a1b628ec08c9aca6a2bfeb20086aa6c55526cfa673dec54adfa88c375b9765cdcd398d7fae96
|
@@ -13,6 +13,7 @@ module Spyke
|
|
13
13
|
|
14
14
|
def assign_nested_attributes(collection)
|
15
15
|
collection = collection.values if collection.is_a?(Hash)
|
16
|
+
replace_existing! unless primary_keys_present?
|
16
17
|
|
17
18
|
collection.each do |attributes|
|
18
19
|
if existing = find_existing_attributes(attributes.with_indifferent_access[:id])
|
@@ -29,6 +30,14 @@ module Spyke
|
|
29
30
|
embedded_attributes.to_a.find { |attr| attr[:id] && attr[:id].to_s == id.to_s }
|
30
31
|
end
|
31
32
|
|
33
|
+
def primary_keys_present?
|
34
|
+
embedded_attributes && embedded_attributes.any? { |attr| attr.has_key?(:id) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def replace_existing!
|
38
|
+
parent.attributes[name] = []
|
39
|
+
end
|
40
|
+
|
32
41
|
def add_to_parent(record)
|
33
42
|
parent.attributes[name] ||= []
|
34
43
|
parent.attributes[name] << record.attributes
|
data/lib/spyke/version.rb
CHANGED
data/test/associations_test.rb
CHANGED
@@ -242,21 +242,30 @@ module Spyke
|
|
242
242
|
assert_equal %w{ starter sauce }, recipe.groups.map(&:title)
|
243
243
|
end
|
244
244
|
|
245
|
-
def
|
245
|
+
def test_nested_attributes_replacing_existing_when_no_ids_present
|
246
246
|
recipe = Recipe.new(groups_attributes: [{ title: 'starter' }, { title: 'sauce' }])
|
247
247
|
recipe.attributes = { groups_attributes: [{ title: 'flavor' }] }
|
248
|
-
assert_equal %w{
|
248
|
+
assert_equal %w{ flavor }, recipe.groups.map(&:title)
|
249
249
|
end
|
250
250
|
|
251
|
-
def
|
251
|
+
def test_nested_attributes_merging_with_existing_when_ids_present?
|
252
252
|
recipe = Recipe.new(groups_attributes: [{ id: 1, title: 'starter', description: 'nice' }, { id: 2, title: 'sauce', description: 'spicy' }])
|
253
253
|
recipe.attributes = { groups_attributes: [{ 'id' => '2', 'title' => 'flavor' }] }
|
254
254
|
assert_equal %w{ starter flavor }, recipe.groups.map(&:title)
|
255
255
|
assert_equal %w{ nice spicy }, recipe.groups.map(&:description)
|
256
256
|
end
|
257
257
|
|
258
|
-
def
|
259
|
-
params = { groups_attributes: [{ id: 1, ingredient_attributes: [{
|
258
|
+
def test_deeply_nested_attributes_using_array_style
|
259
|
+
params = { groups_attributes: [{ id: 1, ingredient_attributes: [{ title: 'fish' }, { title: 'sauce' } ]}] }
|
260
|
+
recipe = Recipe.new(params)
|
261
|
+
recipe.attributes = params
|
262
|
+
assert_equal 1, recipe.groups.size
|
263
|
+
skip "ingredients don't get built properly it seems?"
|
264
|
+
assert_equal 'fish', recipe.groups.first.ingredients.first.title
|
265
|
+
end
|
266
|
+
|
267
|
+
def test_deeply_nested_attributes_with_no_ids
|
268
|
+
params = { groups_attributes: [{ ingredient_attributes: [{ title: 'fish' }, { title: 'sauce' }]}] }
|
260
269
|
recipe = Recipe.new(params)
|
261
270
|
recipe.attributes = params
|
262
271
|
assert_equal 1, recipe.groups.size
|
@@ -267,9 +276,9 @@ module Spyke
|
|
267
276
|
assert_equal %w{ starter sauce }, recipe.groups.map(&:title)
|
268
277
|
end
|
269
278
|
|
270
|
-
def
|
271
|
-
recipe = Recipe.new(groups_attributes: { '0' => { ingredients_attributes: { '0' => { name: 'Salt' } } } })
|
272
|
-
assert_equal %w{ Salt }, recipe.ingredients.map(&:name)
|
279
|
+
def test_deeply_nested_attributes_with_blank_ids
|
280
|
+
recipe = Recipe.new(groups_attributes: { '0' => { ingredients_attributes: { '0' => { id: '', name: 'Salt' }, '1' => { id: '', name: 'Pepper' } } } })
|
281
|
+
assert_equal %w{ Salt Pepper }, recipe.ingredients.map(&:name)
|
273
282
|
end
|
274
283
|
|
275
284
|
def test_reflect_on_association
|
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.
|
4
|
+
version: 1.4.0
|
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-01-
|
11
|
+
date: 2015-01-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|