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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 954c1649aade85a048af51c936b5243633c71bb1
|
4
|
+
data.tar.gz: a91c7e1f646481b7da07d0e11dcced34e1ab9bad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 025d7524855339e96cdf1228fbc0e2019c5ca38c992c346a746944715c1f9b90c063e476a9439636bd0f3045afe74cce8aa96d8680e06ba98ce7294c9ea35b09
|
7
|
+
data.tar.gz: 4ab38d9670943e6b4bd08c5ce6200a225a0b375122e171e2890dafb0ab9c771007a6654a14fb99845b31ca8f0eae7973060188c86f5ba4eea237af37b156a94d
|
data/lib/mongoid/fixture_set.rb
CHANGED
@@ -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
|
-
|
190
|
+
unmarshall_fixture(label, fixture, model_class)
|
191
|
+
end
|
192
192
|
|
193
|
-
|
193
|
+
return documents
|
194
|
+
end
|
194
195
|
|
195
|
-
|
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
|
-
|
198
|
-
|
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
|
-
|
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
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
@@ -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
|
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(
|
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
|
data/test/fixtures/users.yml
CHANGED
@@ -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
|
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
|
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.
|
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
|