portable_model 1.2.2 → 1.3.0

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.
@@ -10,12 +10,18 @@ module PortableModel
10
10
  base.extend(ClassMethods)
11
11
  end
12
12
 
13
- # Introduce a virtual attribute that is used to indicate whether the record
14
- # is currently being imported. It can be read to skip callbacks and
15
- # validations that shouldn't be run when a record is being imported.
13
+ # This virtual attribute is now deprecated in favor of #currently_importing?
16
14
  #
17
15
  attr_accessor :importing_record
18
16
 
17
+ # Check whether the record is currently being imported. Callers can use this
18
+ # for determining when to skip callbacks and validations that shouldn't be
19
+ # run when a record is being imported.
20
+ #
21
+ def currently_importing?
22
+ !!Thread.current[:imported_records]
23
+ end
24
+
19
25
  # Export the record to a hash.
20
26
  #
21
27
  def export_to_hash
@@ -111,7 +117,9 @@ module PortableModel
111
117
  # use the result of that previous import.
112
118
  record = imported_records[record_hash.object_id]
113
119
 
114
- unless record
120
+ if record
121
+ update_record_associations(record, record_hash, options)
122
+ else
115
123
  transaction do
116
124
  # First split out the attributes that correspond to portable
117
125
  # associations.
@@ -276,6 +284,23 @@ module PortableModel
276
284
  end
277
285
  end
278
286
 
287
+ # Update a record's foreign key associations with the respective keys from record_hash.
288
+ #
289
+ def update_record_associations(record, record_hash, options)
290
+ # Determine the foreign keys that the record owns
291
+ association_foreign_key_list = record.class.reflect_on_all_associations(:belongs_to).map(&:association_foreign_key)
292
+
293
+ # Update the foreign keys to any associations that haven't been set yet.
294
+ association_foreign_keys = record_hash.reject do |key, value|
295
+ !association_foreign_key_list.include?(key) || value.nil? || !record[key].nil?
296
+ end
297
+
298
+ unless association_foreign_keys.empty?
299
+ record.attributes = association_foreign_keys
300
+ record.save(!options.fetch(:skip_validations, false))
301
+ end
302
+ end
303
+
279
304
  end
280
305
 
281
306
  end
@@ -1,3 +1,3 @@
1
1
  module PortableModel
2
- VERSION = "1.2.2"
2
+ VERSION = "1.3.0"
3
3
  end
metadata CHANGED
@@ -5,9 +5,9 @@ version: !ruby/object:Gem::Version
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
9
- - 2
10
- version: 1.2.2
8
+ - 3
9
+ - 0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Clyde Law
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2016-04-07 00:00:00 Z
18
+ date: 2016-11-09 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activerecord