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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 54db6401a61650bf3c2a4eb03c31c7bad268e08f
4
- data.tar.gz: 32f5992d2ad3dc9c412d254e276374e38b862e9c
3
+ metadata.gz: 18786d5a16e1af110485c81e9c7174507bad9ffc
4
+ data.tar.gz: a12316a477c6c9baf8dc95985ac0c378279cc88f
5
5
  SHA512:
6
- metadata.gz: 464225c61b899a3fa8c0b67c50e89a6b7899d77ab16d2fdecc965305d010047cbaa2a91ff6afe981352093b97529f43204bc829fc539b7e650d00747c6db7a44
7
- data.tar.gz: 720b15b5ee804462d4120e271fc03d3a1bc54fb15054fc1d8f370c15a0c35d5914d7533b5d601f554cadb3beec3f90ebcde55e1e1a82559d60d89d7d4cacc036
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
@@ -1,3 +1,3 @@
1
1
  module Spyke
2
- VERSION = '1.3.0'
2
+ VERSION = '1.4.0'
3
3
  end
@@ -242,21 +242,30 @@ module Spyke
242
242
  assert_equal %w{ starter sauce }, recipe.groups.map(&:title)
243
243
  end
244
244
 
245
- def test_nested_attributes_overwriting_existing
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{ starter sauce flavor }, recipe.groups.map(&:title)
248
+ assert_equal %w{ flavor }, recipe.groups.map(&:title)
249
249
  end
250
250
 
251
- def test_nested_attributes_merging_with_existing
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 test_deeply_nested_attributes
259
- params = { groups_attributes: [{ id: 1, ingredient_attributes: [{ id: 1, title: 'fish' }]}] }
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 test_nested_nested_attributes
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.3.0
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-05 00:00:00.000000000 Z
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport