mongoid-fixture_set 1.1.2 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mongoid/fixture_set/version.rb +1 -1
- data/lib/mongoid/fixture_set.rb +33 -0
- data/test/fixtures/users.yml +7 -1
- data/test/models/address.rb +9 -0
- data/test/models/home.rb +8 -0
- data/test/models/user.rb +5 -0
- data/test/mongoid/fixture_set_test.rb +1 -0
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 02b82e3dfd784198cbf75724b2478e14f557a4a8
|
4
|
+
data.tar.gz: 4410c8f10c4ae2497c3394071be568b2f5a2e9cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1200a0ae4d699541d3402eb41405b833bfee3e36e27c056d76a823d8d201fed7296cab3eb9bbd30e8757393200fbfd7771f7b38f1aa373e47a1619f692866983
|
7
|
+
data.tar.gz: 3062a85ec71403443e653fdd888f2b67878c236934c0f16fef2013c14751869c82237a9fb352f8f1d8f0d4c50ed2970622942b558c4c1c0db4115156ef416b13
|
data/lib/mongoid/fixture_set.rb
CHANGED
@@ -104,10 +104,43 @@ module Mongoid
|
|
104
104
|
document[key] = attributes[key] || document[key]
|
105
105
|
end
|
106
106
|
end
|
107
|
+
sanitize_new_embedded_documents(document)
|
107
108
|
document.save(validate: false)
|
108
109
|
return document
|
109
110
|
end
|
110
111
|
|
112
|
+
def sanitize_new_embedded_documents(document, is_new = false)
|
113
|
+
document.relations.each do |name, relation|
|
114
|
+
case relation.macro
|
115
|
+
when :embeds_one
|
116
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
117
|
+
is_new && document[name]
|
118
|
+
|
119
|
+
embedded_document_set_default_values(document.public_send(relation.name), document[name])
|
120
|
+
end
|
121
|
+
when :embeds_many
|
122
|
+
if (document.changes[name] && !document.changes[name][1].nil?) ||
|
123
|
+
is_new && document[name]
|
124
|
+
|
125
|
+
embeddeds = document.public_send(relation.name)
|
126
|
+
embeddeds.each_with_index do |embedded, i|
|
127
|
+
embedded_document_set_default_values(embedded, document[name][i])
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def embedded_document_set_default_values(document, attributes)
|
135
|
+
sanitize_new_embedded_documents(document, true)
|
136
|
+
attributes.delete('_id')
|
137
|
+
document.fields.select do |k, v|
|
138
|
+
k != '_id' && v.default_val != nil && attributes[k] == document[k]
|
139
|
+
end.each do |k, v|
|
140
|
+
attributes.delete(k)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
111
144
|
def find_or_create_document(model, fixture_name)
|
112
145
|
model = model.constantize if model.is_a? String
|
113
146
|
|
data/test/fixtures/users.yml
CHANGED
data/test/models/home.rb
ADDED
data/test/models/user.rb
CHANGED
@@ -2,9 +2,14 @@ class User
|
|
2
2
|
include Mongoid::Document
|
3
3
|
include Mongoid::Timestamps::Updated::Short
|
4
4
|
|
5
|
+
embeds_one :address, as: :place
|
6
|
+
embeds_many :homes
|
7
|
+
|
5
8
|
field :firstname
|
6
9
|
field :lastname
|
7
10
|
|
11
|
+
field :default, type: Boolean, default: true
|
12
|
+
|
8
13
|
belongs_to :main_group, class_name: 'Group', inverse_of: :main_users
|
9
14
|
|
10
15
|
has_and_belongs_to_many :groups
|
@@ -51,6 +51,7 @@ module Mongoid
|
|
51
51
|
orga1 = Organisation.find_by(name: '1 Organisation')
|
52
52
|
school = School.find_by(name: 'School')
|
53
53
|
|
54
|
+
assert_equal 1, user1.homes.count
|
54
55
|
assert_equal geoffroy, f_geoffroy.find
|
55
56
|
assert_equal 2, print.users.count
|
56
57
|
assert print.users.include?(geoffroy)
|
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
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Geoffroy Planquart
|
@@ -75,8 +75,10 @@ files:
|
|
75
75
|
- test/models/group.rb
|
76
76
|
- test/models/organisation.rb
|
77
77
|
- test/models/school.rb
|
78
|
-
- test/models/user.rb
|
79
78
|
- test/models/test.rb
|
79
|
+
- test/models/home.rb
|
80
|
+
- test/models/address.rb
|
81
|
+
- test/models/user.rb
|
80
82
|
- test/mongoid.yml
|
81
83
|
- test/fixtures/groups.yml
|
82
84
|
- test/fixtures/schools.yml
|
@@ -117,8 +119,10 @@ test_files:
|
|
117
119
|
- test/models/group.rb
|
118
120
|
- test/models/organisation.rb
|
119
121
|
- test/models/school.rb
|
120
|
-
- test/models/user.rb
|
121
122
|
- test/models/test.rb
|
123
|
+
- test/models/home.rb
|
124
|
+
- test/models/address.rb
|
125
|
+
- test/models/user.rb
|
122
126
|
- test/mongoid.yml
|
123
127
|
- test/fixtures/groups.yml
|
124
128
|
- test/fixtures/schools.yml
|