portable_model 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZGMxOWY3ZjVjMmRlY2NiOWY3ZTBkOTA2Nzg0M2JlZTIzMjgxYjIxZQ==
5
- data.tar.gz: !binary |-
6
- YTk4MTcxMzEwYzNlZGM0ZWZkMjdkZmRkNDA2MmExMzMzZWNkN2IxNQ==
2
+ SHA1:
3
+ metadata.gz: d6897d9fb7774a53644006262851eef7a35821c8
4
+ data.tar.gz: 3c0f72fa86f1d8076bd3b4d9ec0e882a710443df
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NTYwYjFkMjYxYThjNTMyYjE1OTUyZmVmN2EwOGNjYmI1ZTBlZDc2ZGM5NjRm
10
- MzQyODk0MjhhNGYyNDljOWEwYmVjNDUyYTMwMjY3MTQ2NGYyZDc4YmI3Nzcz
11
- NTg5N2VhOGM4NzRjMjc1ZTc1ODQyZmRlZWViNWI0OTQxNGU0MGU=
12
- data.tar.gz: !binary |-
13
- Mjc5MzI3M2I1YjY3ZGQwZTkwYmE2NDk2MTk1MTkzNjI4NTgyZTc1OWFlMzFm
14
- OWUzZjVkZDJmM2M3NzVkZWU5MTA0ZTUwZTIzNzg0NzE3ODk0YzZmNGE1ODQx
15
- YzNhMTQ5ZWM4MmY0YjUxZDJjNWIzNDI4ZmM2NTQ1NWI5M2Q3YWU=
6
+ metadata.gz: 0702055e33fbb5b65364eeb2c427036ee826ed6a67d102e0a5d4269cd268b60e649cfdb1f976c551533d2cdb8d1856d836e38e46dc1fe2ca8fa65dc5d765feee
7
+ data.tar.gz: 186005d91cca52fe8ef36e585381df1d9ea1fa94836d3dca5129bdf58607e0f27c4c9b0be464622e5a15ef08e8242d76756e16dfcc0c4ea303323eb64e39c55a
@@ -40,7 +40,13 @@ module PortableModel
40
40
  # Include the exported attributes of portable associations.
41
41
  self.class.portable_associations.inject(record_hash) do |hash, assoc_name|
42
42
  assoc = self.__send__(assoc_name)
43
- hash[assoc_name] = assoc.export_portable_association if assoc
43
+ if assoc
44
+ if assoc.respond_to?(:export_portable_association)
45
+ hash[assoc_name] = assoc.export_portable_association
46
+ elsif !assoc.new_record?
47
+ hash[assoc_name] = assoc.export_to_hash
48
+ end
49
+ end
44
50
  hash
45
51
  end
46
52
 
@@ -67,15 +73,15 @@ module PortableModel
67
73
 
68
74
  # Import values into the record's association.
69
75
  #
70
- def import_into_association(assoc_name, assoc_value)
76
+ def import_into_association(assoc_name, assoc_value, options = {})
71
77
  assoc = self.__send__(assoc_name)
72
78
  if assoc
73
- assoc.import_portable_association(assoc_value)
79
+ assoc.import_portable_association(assoc_value, options)
74
80
  else
75
81
  assoc_reflection = self.class.reflect_on_association(assoc_name.to_sym)
76
82
  raise 'nil can only be handled for direct has_one associations' unless assoc_reflection.macro == :has_one && !assoc_reflection.is_a?(ActiveRecord::Reflection::ThroughReflection)
77
83
  assoc = ActiveRecord::Associations::HasOneAssociation.new(self, assoc_reflection)
78
- assoc.import_portable_association(assoc_value)
84
+ assoc.import_portable_association(assoc_value, options)
79
85
  association_instance_set(assoc_reflection.name, assoc.target.nil? ? nil : assoc)
80
86
  end
81
87
  end
@@ -84,7 +90,7 @@ module PortableModel
84
90
 
85
91
  # Import a record from a hash.
86
92
  #
87
- def import_from_hash(record_hash)
93
+ def import_from_hash(record_hash, options = {})
88
94
  raise ArgumentError.new('specified argument is not a hash') unless record_hash.is_a?(Hash)
89
95
 
90
96
  # Override any necessary attributes before importing.
@@ -96,7 +102,7 @@ module PortableModel
96
102
  record_type_name != sti_name)
97
103
  # The model implements STI and the record type points to a different
98
104
  # class; call the method in that class instead.
99
- compute_type(record_type_name).import_from_hash(record_hash)
105
+ compute_type(record_type_name).import_from_hash(record_hash, options)
100
106
  else
101
107
  start_importing do |imported_records|
102
108
  # If the hash had already been imported during the current session,
@@ -112,13 +118,20 @@ module PortableModel
112
118
  hash
113
119
  end
114
120
 
115
- # Create a new record.
116
- record = create!(record_hash.merge(:importing_record => true))
121
+ if options.fetch(:skip_validations, false)
122
+ # Create a new record and save, skipping validations.
123
+ record = new(record_hash.merge(:importing_record => true))
124
+ record.save(false)
125
+ else
126
+ record = create!(record_hash.merge(:importing_record => true))
127
+ end
117
128
 
118
129
  # Import each of the record's associations into the record.
130
+ assoc_attrs = assoc_attrs.sort_by { |assoc_name, assoc_value| order_associations.index(assoc_name) ? order_associations.index(assoc_name) : order_associations.size } unless order_associations.empty?
119
131
  assoc_attrs.each do |assoc_name, assoc_value|
120
- record.import_into_association(assoc_name, assoc_value)
132
+ record.import_into_association(assoc_name, assoc_value, options)
121
133
  end
134
+
122
135
  end
123
136
 
124
137
  imported_records[record_hash.object_id] = record
@@ -131,9 +144,9 @@ module PortableModel
131
144
 
132
145
  # Export a record from a YAML file.
133
146
  #
134
- def import_from_yml(filename, additional_attrs = {})
147
+ def import_from_yml(filename, additional_attrs = {}, options = {})
135
148
  record_hash = YAML::load_file(filename)
136
- import_from_hash(record_hash.merge(additional_attrs))
149
+ import_from_hash(record_hash.merge(additional_attrs), options)
137
150
  end
138
151
 
139
152
  # Starts an export session and yields a hash of currently exported records
@@ -198,6 +211,10 @@ module PortableModel
198
211
  @overridden_import_attrs ||= {}
199
212
  end
200
213
 
214
+ def order_associations
215
+ @order_associations ||= []
216
+ end
217
+
201
218
  protected
202
219
 
203
220
  # Includes the specified associations' foreign keys (which are normally
@@ -236,6 +253,10 @@ module PortableModel
236
253
  end
237
254
  end
238
255
 
256
+ def order_associations_on_import(assocs)
257
+ order_associations.concat assocs
258
+ end
259
+
239
260
  private
240
261
 
241
262
  def start_porting(storage_identifier)
@@ -60,7 +60,7 @@ module ActiveRecord::Associations
60
60
 
61
61
  # Import the association from a hash.
62
62
  #
63
- def import_portable_association(record_hash)
63
+ def import_portable_association(record_hash, options = {})
64
64
  NotPortableError.raise_on_not_portable(self)
65
65
  raise ArgumentError.new('specified argument is not a hash') unless record_hash.is_a?(Hash)
66
66
  raise 'cannot replace existing association record' unless target.nil?
@@ -68,7 +68,7 @@ module ActiveRecord::Associations
68
68
  proxy_reflection.klass.start_importing do
69
69
  proxy_owner.transaction do
70
70
  record_hash.merge!(primary_key_hash)
71
- assoc_record = proxy_reflection.klass.import_from_hash(record_hash)
71
+ assoc_record = proxy_reflection.klass.import_from_hash(record_hash, options)
72
72
  replace(assoc_record)
73
73
  end
74
74
  end
@@ -87,7 +87,7 @@ module ActiveRecord::Associations
87
87
 
88
88
  # Import the association from an array of hashes.
89
89
  #
90
- def import_portable_association(record_hashes)
90
+ def import_portable_association(record_hashes, options = {})
91
91
  NotPortableError.raise_on_not_portable(self)
92
92
  raise ArgumentError.new('specified argument is not an array of hashes') unless record_hashes.is_a?(Array) && record_hashes.all? { |record_hash| record_hash.is_a?(Hash) }
93
93
 
@@ -96,7 +96,7 @@ module ActiveRecord::Associations
96
96
  delete_all
97
97
  assoc_records = record_hashes.map do |record_hash|
98
98
  record_hash.merge!(primary_key_hash)
99
- proxy_reflection.klass.import_from_hash(record_hash)
99
+ proxy_reflection.klass.import_from_hash(record_hash, options)
100
100
  end
101
101
  replace(assoc_records)
102
102
  end
@@ -1,3 +1,3 @@
1
1
  module PortableModel
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
3
3
  end
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: portable_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Clyde Law
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-09 00:00:00.000000000 Z
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 2.3.8
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.3.8
27
27
  description: Enables exporting and importing an ActiveRecord model's records.
@@ -31,7 +31,7 @@ executables: []
31
31
  extensions: []
32
32
  extra_rdoc_files: []
33
33
  files:
34
- - .gitignore
34
+ - ".gitignore"
35
35
  - Gemfile
36
36
  - README.rdoc
37
37
  - Rakefile
@@ -48,17 +48,17 @@ require_paths:
48
48
  - lib
49
49
  required_ruby_version: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ! '>='
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
53
  version: '0'
54
54
  required_rubygems_version: !ruby/object:Gem::Requirement
55
55
  requirements:
56
- - - ! '>='
56
+ - - ">="
57
57
  - !ruby/object:Gem::Version
58
58
  version: '0'
59
59
  requirements: []
60
60
  rubyforge_project: portable_model
61
- rubygems_version: 2.1.11
61
+ rubygems_version: 2.2.2
62
62
  signing_key:
63
63
  specification_version: 4
64
64
  summary: Enables exporting and importing an ActiveRecord model's records.