mongoid-fixture_set 1.1.0 → 1.1.2

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: deee45d8bd6cce4b9e484a8adde2467f0d258a28
4
- data.tar.gz: d0e05c7fa2ce09d4685998e213ab8de144c8a20e
3
+ metadata.gz: 08d28904367b1439b40eff8c368b9e2fbc0a685a
4
+ data.tar.gz: 29794db7719e71e7a33c0daf552c421beb912684
5
5
  SHA512:
6
- metadata.gz: 95d1a77135689a9bbb3c8e142e43d0e2fd3df43b4f2c15d9b325ab94b8ccff6aacd72039ea867d090cafcd0de29fc2dffa0078a33738581a629120adb369f193
7
- data.tar.gz: 8cd816d775042c2bda59152ad2fa5cbe475c907e118a3db5fa8510a526cbb5b8c49776640c61309e27582162977321cd027b4282aff5765e72a43a91f9a53749
6
+ metadata.gz: 7836135a9f31c0c09b063e572521be6d8c3d06e828acdeec5a507b6398832daae12c87a5cee77189aafdca3e24b5ff0853845dd8cdd07e446d1880dcddc439d4
7
+ data.tar.gz: b4a989ce4acb13cf16f816bc27565b8f68bf06a3b2bc7d8806ab2439ee901e590649a4f19182c0156080c3cb71365bbf1fb80a88c35d4ee8d2a833491c2f0e06
@@ -29,7 +29,7 @@ module Mongoid
29
29
  def find
30
30
  if model_class
31
31
  model_class.unscoped do
32
- model_class.find(Mongoid::FixtureSet.identify(model_class, name))
32
+ model_class.find_by('__fixture_name' => name)
33
33
  end
34
34
  else
35
35
  raise FixtureClassNotFound, "No class attached to find."
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  class FixtureSet
3
- VERSION = '1.1.0'
3
+ VERSION = '1.1.2'
4
4
  end
5
5
  end
6
6
 
@@ -3,8 +3,6 @@ require 'mongoid/fixture_set/file'
3
3
  require 'mongoid/fixture_set/class_cache'
4
4
  require 'mongoid/fixture_set/test_helper'
5
5
 
6
- require 'active_support/core_ext/digest/uuid'
7
-
8
6
  module Mongoid
9
7
  class FixtureSet
10
8
  @@cached_fixtures = Hash.new
@@ -89,24 +87,37 @@ module Mongoid
89
87
  end
90
88
 
91
89
  def create_or_update_document(model, attributes)
92
- document = model.find_or_initialize_by('_id' => attributes['_id'])
90
+ model = model.constantize if model.is_a? String
91
+
92
+ document = find_or_create_document(model, attributes['__fixture_name'])
93
+ update_document(document, attributes)
94
+ end
95
+
96
+ def update_document(document, attributes)
97
+ attributes.delete('_id') if document.attributes.has_key?('_id')
98
+
93
99
  keys = (attributes.keys + document.attributes.keys).uniq
94
- attrs = Hash[keys.collect do |key|
95
- if attributes[key].is_a?(Array) || document.attributes[key].is_a?(Array)
96
- value = [attributes[key], document.attributes[key]].flatten(1).compact
100
+ keys.each do |key|
101
+ if attributes[key].is_a?(Array) || document[key].is_a?(Array)
102
+ document[key] = Array(attributes[key]) + Array(document[key])
97
103
  else
98
- value = attributes[key] || document.attributes[key]
104
+ document[key] = attributes[key] || document[key]
99
105
  end
100
- [key, value]
101
- end]
102
- document.assign_attributes(attrs)
106
+ end
103
107
  document.save(validate: false)
108
+ return document
104
109
  end
105
110
 
106
- # Returns a consistent, platform-independent identifier for +label+.
107
- # UUIDs are RFC 4122 version 5 SHA-1 hashes.
108
- def identify(model, label)
109
- Digest::UUID.uuid_v5(Digest::UUID::OID_NAMESPACE, "#{model}##{label}")
111
+ def find_or_create_document(model, fixture_name)
112
+ model = model.constantize if model.is_a? String
113
+
114
+ document = model.where('__fixture_name' => fixture_name).first
115
+ if document.nil?
116
+ document = model.new
117
+ document['__fixture_name'] = fixture_name
118
+ document.save(validate: false)
119
+ end
120
+ return document
110
121
  end
111
122
  end
112
123
 
@@ -146,8 +157,15 @@ module Mongoid
146
157
  documents[class_name] = fixtures.map do |label, fixture|
147
158
  attributes = fixture.to_hash
148
159
 
160
+ attributes['__fixture_name'] = label
161
+
149
162
  next attributes if model_class.nil?
150
163
 
164
+ if !attributes.has_key?('_id')
165
+ document = self.class.find_or_create_document(model_class, label)
166
+ attributes['_id'] = document.id
167
+ end
168
+
151
169
  set_attributes_timestamps(attributes, model_class)
152
170
 
153
171
  # interpolate the fixture label
@@ -155,34 +173,30 @@ module Mongoid
155
173
  attributes[key] = value.gsub("$LABEL", label) if value.is_a?(String)
156
174
  end
157
175
 
158
- attributes['_id'] = self.class.identify(class_name, label)
159
-
160
176
  model_class.relations.each do |name, relation|
161
177
  case relation.macro
162
178
  when :belongs_to
163
179
  if value = attributes.delete(relation.name.to_s)
164
- id = self.class.identify(relation.class_name, value)
165
180
  if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
166
181
  type = $1
167
182
  attributes[relation.foreign_key.sub(/_id$/, '_type')] = type
168
- id = self.class.identify(type, value)
183
+ attributes[relation.foreign_key] = self.class.find_or_create_document(type, value).id
184
+ else
185
+ attributes[relation.foreign_key] = self.class.find_or_create_document(relation.class_name, value).id
169
186
  end
170
- attributes[relation.foreign_key] = id
171
187
  end
172
188
  when :has_many
173
189
  if values = attributes.delete(relation.name.to_s)
174
190
  values.each do |value|
175
- id = self.class.identify(relation.class_name, value)
191
+ document = self.class.find_or_create_document(relation.class_name, value)
176
192
  if relation.polymorphic?
177
- self.class.create_or_update_document(relation.class_name.constantize, {
178
- '_id' => id,
193
+ self.class.update_document(document, {
179
194
  "#{relation.as}_id" => attributes['_id'],
180
195
  "#{relation.as}_type" => model_class.name,
181
196
  })
182
197
  else
183
- self.class.create_or_update_document(relation.class_name.constantize, {
184
- '_id' => id,
185
- relation.foreign_key => attributes['_id'],
198
+ self.class.update_document(document, {
199
+ relation.foreign_key => attributes['_id']
186
200
  })
187
201
  end
188
202
  end
@@ -193,12 +207,11 @@ module Mongoid
193
207
  attributes[key] = []
194
208
 
195
209
  values.each do |value|
196
- id = self.class.identify(relation.class_name, value)
197
- attributes[key] << id
210
+ document = self.class.find_or_create_document(relation.class_name, value)
211
+ attributes[key] << document.id
198
212
 
199
- self.class.create_or_update_document(relation.class_name.constantize, {
200
- '_id' => id,
201
- relation.inverse_foreign_key => [attributes['_id']]
213
+ self.class.update_document(document, {
214
+ relation.inverse_foreign_key => Array(attributes['_id'])
202
215
  })
203
216
  end
204
217
  end
@@ -7,3 +7,4 @@ geoffroy:
7
7
  user1:
8
8
  firstname: Margot
9
9
  lastname: Last
10
+ fuckit: true
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.1.0
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffroy Planquart
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ~>
32
32
  - !ruby/object:Gem::Version
33
- version: '4.2'
33
+ version: '4.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ~>
39
39
  - !ruby/object:Gem::Version
40
- version: '4.2'
40
+ version: '4.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -62,28 +62,28 @@ files:
62
62
  - lib/mongoid/fixture_set/errors.rb
63
63
  - lib/mongoid/fixture_set/file.rb
64
64
  - lib/mongoid/fixture_set/class_cache.rb
65
+ - lib/mongoid/fixture_set/test_helper.rb
65
66
  - lib/mongoid/fixture_set/fixture.rb
66
67
  - lib/mongoid/fixture_set/version.rb
67
- - lib/mongoid/fixture_set/test_helper.rb
68
68
  - lib/mongoid/fixture_set.rb
69
69
  - lib/mongoid-fixture_set.rb
70
70
  - MIT-LICENSE
71
71
  - Rakefile
72
72
  - test/mongoid/fixtures_test.rb
73
- - test/mongoid/fixture_set_test.rb
74
73
  - test/mongoid/load_once_test.rb
74
+ - test/mongoid/fixture_set_test.rb
75
75
  - test/models/group.rb
76
76
  - test/models/organisation.rb
77
77
  - test/models/school.rb
78
78
  - test/models/user.rb
79
79
  - test/models/test.rb
80
80
  - test/mongoid.yml
81
- - test/fixtures/users.yml
82
81
  - test/fixtures/groups.yml
83
82
  - test/fixtures/schools.yml
84
83
  - test/fixtures/organisations.yml
85
84
  - test/fixtures/users/family.yml
86
85
  - test/fixtures/not_models.yml
86
+ - test/fixtures/users.yml
87
87
  - test/test_helper.rb
88
88
  - test/load_once_fixtures/tests.yml
89
89
  homepage: https://github.com/Aethelflaed/mongoid-fixture_set
@@ -112,20 +112,20 @@ specification_version: 4
112
112
  summary: Fixtures for Mongoid
113
113
  test_files:
114
114
  - test/mongoid/fixtures_test.rb
115
- - test/mongoid/fixture_set_test.rb
116
115
  - test/mongoid/load_once_test.rb
116
+ - test/mongoid/fixture_set_test.rb
117
117
  - test/models/group.rb
118
118
  - test/models/organisation.rb
119
119
  - test/models/school.rb
120
120
  - test/models/user.rb
121
121
  - test/models/test.rb
122
122
  - test/mongoid.yml
123
- - test/fixtures/users.yml
124
123
  - test/fixtures/groups.yml
125
124
  - test/fixtures/schools.yml
126
125
  - test/fixtures/organisations.yml
127
126
  - test/fixtures/users/family.yml
128
127
  - test/fixtures/not_models.yml
128
+ - test/fixtures/users.yml
129
129
  - test/test_helper.rb
130
130
  - test/load_once_fixtures/tests.yml
131
131
  has_rdoc: