caprese 0.3.3 → 0.3.4

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
  SHA1:
3
- metadata.gz: 1064cb8d4344aa314d5cf4a3a851a4e52fad8ce7
4
- data.tar.gz: 12be18352ab0c879d4c1bb138faa785b0ae5a28a
3
+ metadata.gz: 0ca4c84e5919ce00359d805fbbbfd4da7e26c769
4
+ data.tar.gz: bd3df0c2fc3a9f29f91c9577c18ebf9a48b9a9df
5
5
  SHA512:
6
- metadata.gz: f02cc3064dbc79f29464be3222073bbfd2da45c85c63f3a96ce566ae5a45fd48b835ee54260b942ff70fc25bb9f41a6383302f9e0fd45fd5f86d54fea3cad240
7
- data.tar.gz: 0df51ffba9329b200042af7521230213763ea0ada30799a50786160dcfea0500af1f02926bdf945ba058203e470d6ced4c36e21fba3785a9b6d304a28cf05f08
6
+ metadata.gz: 46b96f45c77864c3ae31ec75291a0ae9e1e4c3ce4480c952896c5a07751cd799678fe9d525237abb16ba8df485d76340232819cb390ab374916352fadbb32471
7
+ data.tar.gz: b8fad22479e137399a200458e706bf632c18dceda7be71b8e61fa3295381a773100fd232852d48639ca695df6449fbcb697c4589d775454adb878c10035ecdf0
data/CHANGELOG.md CHANGED
@@ -18,3 +18,8 @@
18
18
  ## 0.3.3
19
19
 
20
20
  * Depend on all route primary key URL params to be named `:id`, rather than `Caprese.config.resource_primary_key`
21
+
22
+ ## 0.3.4
23
+
24
+ * Fix bug in processing of collection relationship data (5e21582)
25
+ * Fix bug in persisting of collection relationship data on create (1eb4720)
@@ -50,6 +50,8 @@ module Caprese
50
50
 
51
51
  record.save!
52
52
 
53
+ persist_collection_relationships(record)
54
+
53
55
  execute_after_create_callbacks(record)
54
56
  execute_after_save_callbacks(record)
55
57
 
@@ -242,12 +244,12 @@ module Caprese
242
244
  # @param [Hash,Array<Hash>] relationship_data the resource identifier data to use to find/build records
243
245
  # @return [ActiveRecord::Base,Array<ActiveRecord::Base>] the record(s) for the relationship
244
246
  def records_for_relationship(owner, permitted_params, relationship_name, relationship_data)
245
- if relationship_data.is_a?(Array)
246
- relationship_data.map do |relationship_data_item|
247
- ref = record_for_relationship(owner, relationship_name, relationship_data_item[:data])
247
+ if relationship_data[:data].is_a?(Array)
248
+ relationship_data[:data].map do |relationship_data_item|
249
+ ref = record_for_relationship(owner, relationship_name, relationship_data_item)
248
250
 
249
- if ref && contains_constructable_data?(relationship_data_item[:data])
250
- assign_record_attributes(ref, permitted_params, relationship_data_item[:data])
251
+ if ref && contains_constructable_data?(relationship_data_item)
252
+ assign_record_attributes(ref, permitted_params, relationship_data_item)
251
253
  end
252
254
 
253
255
  ref
@@ -316,5 +318,25 @@ module Caprese
316
318
  def contains_constructable_data?(resource_identifier)
317
319
  [:attributes, :relationships].any? { |k| resource_identifier.key?(k) }
318
320
  end
321
+
322
+ # Called in create, after the record is saved. When creating a new record, and assigning to it
323
+ # existing has_many association relation, the records in the relation will be pushed onto the
324
+ # appropriate target, but the relationship will not be persisted in their attributes until their
325
+ # owner is saved.
326
+ #
327
+ # This methods persists the collection relation(s) pushed onto the record's association target(s)
328
+ def persist_collection_relationships(record)
329
+ record.class.reflect_on_all_associations
330
+ .select { |ref| ref.collection? && record.association(ref.name).any? }
331
+ .map do |ref|
332
+ [
333
+ ref.has_inverse? ? ref.inverse_of.name : ref.options[:as],
334
+ record.association(ref.name).target
335
+ ]
336
+ end
337
+ .to_h.each do |name, targets|
338
+ targets.each { |t| t.update name => record }
339
+ end
340
+ end
319
341
  end
320
342
  end
@@ -1,3 +1,3 @@
1
1
  module Caprese
2
- VERSION = '0.3.3'
2
+ VERSION = '0.3.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caprese
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Landgrebe
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: exe
12
12
  cert_chain: []
13
- date: 2017-02-07 00:00:00.000000000 Z
13
+ date: 2017-02-08 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: active_model_serializers