mongoid-fixture_set 1.3.2 → 1.3.3

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: c7e6e3f628466c6643665b562f50ae06e4b2068e
4
- data.tar.gz: c0978d43e8aa95bc8622a7f22261146b939320bc
3
+ metadata.gz: 954c1649aade85a048af51c936b5243633c71bb1
4
+ data.tar.gz: a91c7e1f646481b7da07d0e11dcced34e1ab9bad
5
5
  SHA512:
6
- metadata.gz: 051d3f4ee6018932783edba6a288d1197eb2e469d0f7b44bd92d9a03d7a161ad6e4718184a13bc35032a5c66cd6ed7437d3ce3bb0b66aa251683193d4c760f01
7
- data.tar.gz: c1c8260fed59e349f53167aab4e20d97de28feb46450001ae0de7980a6db10c474dd740ba60eb871cc9dac9317721d9c9e54d0f4fec77a994e6983c6c5a2becb
6
+ metadata.gz: 025d7524855339e96cdf1228fbc0e2019c5ca38c992c346a746944715c1f9b90c063e476a9439636bd0f3045afe74cce8aa96d8680e06ba98ce7294c9ea35b09
7
+ data.tar.gz: 4ab38d9670943e6b4bd08c5ce6200a225a0b375122e171e2890dafb0ab9c771007a6654a14fb99845b31ca8f0eae7973060188c86f5ba4eea237af37b156a94d
@@ -1,3 +1,4 @@
1
+ require 'mongoid/fixture_set/errors'
1
2
  require 'mongoid/fixture_set/fixture'
2
3
  require 'mongoid/fixture_set/file'
3
4
  require 'mongoid/fixture_set/class_cache'
@@ -114,13 +115,13 @@ module Mongoid
114
115
  case relation.macro
115
116
  when :embeds_one
116
117
  if (document.changes[name] && !document.changes[name][1].nil?) ||
117
- is_new && document[name]
118
+ (is_new && document[name])
118
119
 
119
120
  embedded_document_set_default_values(document.public_send(relation.name), document[name])
120
121
  end
121
122
  when :embeds_many
122
123
  if (document.changes[name] && !document.changes[name][1].nil?) ||
123
- is_new && document[name]
124
+ (is_new && document[name])
124
125
 
125
126
  embeddeds = document.public_send(relation.name)
126
127
  embeddeds.each_with_index do |embedded, i|
@@ -178,8 +179,6 @@ module Mongoid
178
179
  fixtures[x]
179
180
  end
180
181
 
181
- # Returns a hash of documents to be inserted. The key is the model class, the value is
182
- # a list of documents to insert in th relative collection.
183
182
  def collection_documents
184
183
  # allow a standard key to be used for doing defaults in YAML
185
184
  fixtures.delete('DEFAULTS')
@@ -188,99 +187,137 @@ module Mongoid
188
187
  documents = Hash.new
189
188
 
190
189
  documents[class_name] = fixtures.map do |label, fixture|
191
- attributes = fixture.to_hash
190
+ unmarshall_fixture(label, fixture, model_class)
191
+ end
192
192
 
193
- attributes['__fixture_name'] = label
193
+ return documents
194
+ end
194
195
 
195
- next attributes if model_class.nil?
196
+ private
197
+ def unmarshall_fixture(label, attributes, model_class)
198
+ model_class = model_class.constantize if model_class.is_a? String
199
+ attributes = attributes.to_hash
196
200
 
197
- if !attributes.has_key?('_id')
198
- document = self.class.find_or_create_document(model_class, label)
199
- attributes['_id'] = document.id
200
- end
201
+ if label
202
+ attributes['__fixture_name'] = label
201
203
 
202
- set_attributes_timestamps(attributes, model_class)
203
-
204
204
  # interpolate the fixture label
205
205
  attributes.each do |key, value|
206
206
  attributes[key] = value.gsub("$LABEL", label) if value.is_a?(String)
207
207
  end
208
+ end
208
209
 
209
- model_class.relations.each do |name, relation|
210
- case relation.macro
211
- when :belongs_to
212
- if value = attributes.delete(relation.name.to_s)
213
- if value.is_a? Hash
214
- if relation.polymorphic?
215
- raise Mongoid::FixtureSet::FixtureError.new "Unable to create document from nested attributes in a polymorphic relation"
216
- end
217
- document = relation.class_name.constantize.new
218
- document = self.class.update_document(document, value)
219
- attributes[relation.foreign_key] = document.id
220
- next
221
- end
210
+ return attributes if model_class.nil?
222
211
 
223
- if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
224
- type = $1
225
- attributes[relation.foreign_key.sub(/_id$/, '_type')] = type
226
- attributes[relation.foreign_key] = self.class.find_or_create_document(type, value).id
227
- else
228
- attributes[relation.foreign_key] = self.class.find_or_create_document(relation.class_name, value).id
229
- end
230
- end
231
- when :has_many
232
- if values = attributes.delete(relation.name.to_s)
233
- values.each do |value|
234
- if value.is_a? Hash
235
- document = relation.class_name.constantize.new
236
- if relation.polymorphic?
237
- value["#{relation.as}_id"] = attributes['_id']
238
- value["#{relation.as}_type"] = model_class.name
239
- else
240
- value[relation.foreign_key] = attributes['_id']
241
- end
242
- self.class.update_document(document, value)
243
- next
244
- end
245
-
246
- document = self.class.find_or_create_document(relation.class_name, value)
247
- if relation.polymorphic?
248
- self.class.update_document(document, {
249
- "#{relation.as}_id" => attributes['_id'],
250
- "#{relation.as}_type" => model_class.name,
251
- })
252
- else
253
- self.class.update_document(document, {
254
- relation.foreign_key => attributes['_id']
255
- })
256
- end
257
- end
258
- end
259
- when :has_and_belongs_to_many
260
- if values = attributes.delete(relation.name.to_s)
261
- key = "#{relation.name.to_s.singularize}_ids"
262
- attributes[key] = []
263
-
264
- values.each do |value|
265
- document = self.class.find_or_create_document(relation.class_name, value)
266
- attributes[key] << document.id
267
-
268
- self.class.update_document(document, {
269
- relation.inverse_foreign_key => Array(attributes['_id'])
270
- })
271
- end
272
- end
212
+ if !attributes.has_key?('_id')
213
+ if label
214
+ document = self.class.find_or_create_document(model_class, label)
215
+ else
216
+ document = model_class.new
217
+ end
218
+ attributes['_id'] = document.id
219
+ end
220
+
221
+ set_attributes_timestamps(model_class, attributes)
222
+
223
+ model_class.relations.each_value do |relation|
224
+ case relation.macro
225
+ when :belongs_to
226
+ unmarshall_belongs_to(model_class, attributes, relation)
227
+ when :has_many
228
+ unmarshall_has_many(model_class, attributes, relation)
229
+ when :has_and_belongs_to_many
230
+ unmarshall_has_and_belongs_to_many(model_class, attributes, relation)
231
+ end
232
+ end
233
+
234
+ return attributes
235
+ end
236
+
237
+ def unmarshall_belongs_to(model_class, attributes, relation)
238
+ value = attributes.delete(relation.name.to_s)
239
+ return if value.nil?
240
+
241
+ if value.is_a? Hash
242
+ if relation.polymorphic?
243
+ raise Mongoid::FixtureSet::FixtureError.new "Unable to create document from nested attributes in a polymorphic relation"
244
+ end
245
+ document = relation.class_name.constantize.new
246
+ value = unmarshall_fixture(nil, value, relation.class_name)
247
+ document = self.class.update_document(document, value)
248
+ attributes[relation.foreign_key] = document.id
249
+ return
250
+ end
251
+
252
+ if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
253
+ type = $1
254
+ attributes[relation.inverse_type] = type
255
+ attributes[relation.foreign_key] = self.class.find_or_create_document(type, value).id
256
+ else
257
+ attributes[relation.foreign_key] = self.class.find_or_create_document(relation.class_name, value).id
258
+ end
259
+ end
260
+
261
+ def unmarshall_has_many(model_class, attributes, relation)
262
+ values = attributes.delete(relation.name.to_s)
263
+ return if values.nil?
264
+
265
+ values.each do |value|
266
+ if value.is_a? Hash
267
+ document = relation.class_name.constantize.new
268
+ if relation.polymorphic?
269
+ value[relation.foreign_key] = attributes['_id']
270
+ value[relation.type] = model_class.name
271
+ else
272
+ value[relation.foreign_key] = attributes['_id']
273
273
  end
274
+ value = unmarshall_fixture(nil, value, relation.class_name)
275
+ self.class.update_document(document, value)
276
+ next
274
277
  end
275
278
 
276
- attributes
279
+ document = self.class.find_or_create_document(relation.class_name, value)
280
+ if relation.polymorphic?
281
+ self.class.update_document(document, {
282
+ relation.foreign_key => attributes['_id'],
283
+ relation.type => model_class.name,
284
+ })
285
+ else
286
+ self.class.update_document(document, {
287
+ relation.foreign_key => attributes['_id']
288
+ })
289
+ end
277
290
  end
291
+ end
278
292
 
279
- return documents
293
+ def unmarshall_has_and_belongs_to_many(model_class, attributes, relation)
294
+ values = attributes.delete(relation.name.to_s)
295
+ return if values.nil?
296
+
297
+ key = relation.foreign_key
298
+ attributes[key] = []
299
+
300
+ values.each do |value|
301
+ if value.is_a? Hash
302
+ document = relation.class_name.constantize.new
303
+ value[relation.inverse_foreign_key] = Array(attributes['_id'])
304
+ value = unmarshall_fixture(nil, value, relation.class_name)
305
+ self.class.update_document(document, value)
306
+ attributes[key] << document.id
307
+
308
+ next
309
+ end
310
+
311
+ document = self.class.find_or_create_document(relation.class_name, value)
312
+ attributes[key] << document.id
313
+
314
+ self.class.update_document(document, {
315
+ relation.inverse_foreign_key => Array(attributes['_id'])
316
+ })
317
+ end
280
318
  end
281
319
 
282
- private
283
- def set_attributes_timestamps(attributes, model_class)
320
+ def set_attributes_timestamps(model_class, attributes)
284
321
  now = Time.now.utc
285
322
 
286
323
  if model_class < Mongoid::Timestamps::Created::Short
@@ -3,6 +3,9 @@ module Mongoid
3
3
  class FixtureError < Mongoid::Errors::MongoidError
4
4
  end
5
5
 
6
+ class FixtureNotFound < FixtureError
7
+ end
8
+
6
9
  class FormatError < FixtureError
7
10
  end
8
11
 
@@ -53,7 +53,7 @@ module Mongoid
53
53
  if @loaded_fixtures[fs_name] && @loaded_fixtures[fs_name][f_name]
54
54
  @fixture_cache[fs_name][f_name] ||= @loaded_fixtures[fs_name][f_name].find
55
55
  else
56
- raise StandardError, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'"
56
+ raise FixtureNotFound, "No fixture named '#{f_name}' found for fixture set '#{fs_name}'"
57
57
  end
58
58
  end
59
59
  instances.size == 1 ? instances.first : instances
@@ -68,7 +68,7 @@ module Mongoid
68
68
  @fixture_cache = {}
69
69
 
70
70
  if self.class.cached_fixtures && self.class.load_fixtures_once
71
- self.class.fixtures(:all)
71
+ self.class.fixtures(self.class.fixture_set_names)
72
72
  @loaded_fixtures = self.class.cached_fixtures
73
73
  else
74
74
  Mongoid::FixtureSet.reset_cache
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  class FixtureSet
3
- VERSION = '1.3.2'
3
+ VERSION = '1.3.3'
4
4
  end
5
5
  end
6
6
 
@@ -4,6 +4,15 @@ geoffroy:
4
4
  main_group: sudoers
5
5
  groups:
6
6
  - print
7
+ - !ruby/hash
8
+ name: Test nested polymorphic belongs_to
9
+ something: orga1 (Organisation)
10
+ - !ruby/hash
11
+ name: Test nested has_many creation
12
+ main_users:
13
+ - {
14
+ firstname: 'Created in nested group'
15
+ }
7
16
  items:
8
17
  - {
9
18
  name: Test
@@ -52,7 +52,7 @@ module Mongoid
52
52
  f_geoffroy = users['geoffroy']
53
53
 
54
54
  assert_equal 6, School.count
55
- assert_equal 5, User.count
55
+ assert_equal 6, User.count
56
56
 
57
57
  geoffroy = User.find_by(firstname: 'Geoffroy')
58
58
  user1 = User.find_by(firstname: 'Margot')
@@ -64,6 +64,9 @@ module Mongoid
64
64
  test_item = Item.find_by(name: 'Test')
65
65
  user2 = User.find_by(firstname: 'user2')
66
66
  win_group = Group.find_by(name: 'Win?')
67
+ test_nested_polymorphic_belongs_to = Group.find_by(name: 'Test nested polymorphic belongs_to')
68
+ test_nested_has_many_creation = Group.find_by(name: 'Test nested has_many creation')
69
+ User.find_by(firstname: 'Created in nested group')
67
70
 
68
71
  assert_equal 1, user1.homes.count
69
72
  assert_equal geoffroy, f_geoffroy.find
@@ -80,7 +83,7 @@ module Mongoid
80
83
  assert_equal group1, school.groups.first
81
84
  assert_equal school, group1.something
82
85
 
83
- assert_equal 2, orga1.groups.count
86
+ assert_equal 3, orga1.groups.count
84
87
  assert orga1.groups.include?(sudoers)
85
88
  assert_equal orga1, sudoers.something
86
89
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-fixture_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
4
+ version: 1.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffroy Planquart
@@ -59,10 +59,10 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - lib/mongoid/fixture_set/errors.rb
63
62
  - lib/mongoid/fixture_set/file.rb
64
63
  - lib/mongoid/fixture_set/fixture.rb
65
64
  - lib/mongoid/fixture_set/class_cache.rb
65
+ - lib/mongoid/fixture_set/errors.rb
66
66
  - lib/mongoid/fixture_set/test_helper.rb
67
67
  - lib/mongoid/fixture_set/version.rb
68
68
  - lib/mongoid/fixture_set.rb
@@ -85,8 +85,8 @@ files:
85
85
  - test/fixtures/organisations.yml
86
86
  - test/fixtures/users/family.yml
87
87
  - test/fixtures/not_models.yml
88
- - test/fixtures/users.yml
89
88
  - test/fixtures/groups.yml
89
+ - test/fixtures/users.yml
90
90
  - test/test_helper.rb
91
91
  - test/load_once_fixtures/tests.yml
92
92
  - test/nested_polymorphic_relation_fixtures/groups.yml
@@ -131,8 +131,8 @@ test_files:
131
131
  - test/fixtures/organisations.yml
132
132
  - test/fixtures/users/family.yml
133
133
  - test/fixtures/not_models.yml
134
- - test/fixtures/users.yml
135
134
  - test/fixtures/groups.yml
135
+ - test/fixtures/users.yml
136
136
  - test/test_helper.rb
137
137
  - test/load_once_fixtures/tests.yml
138
138
  - test/nested_polymorphic_relation_fixtures/groups.yml