mongoid-fixture_set 1.1.2 → 1.2.1

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: 08d28904367b1439b40eff8c368b9e2fbc0a685a
4
- data.tar.gz: 29794db7719e71e7a33c0daf552c421beb912684
3
+ metadata.gz: 02b82e3dfd784198cbf75724b2478e14f557a4a8
4
+ data.tar.gz: 4410c8f10c4ae2497c3394071be568b2f5a2e9cd
5
5
  SHA512:
6
- metadata.gz: 7836135a9f31c0c09b063e572521be6d8c3d06e828acdeec5a507b6398832daae12c87a5cee77189aafdca3e24b5ff0853845dd8cdd07e446d1880dcddc439d4
7
- data.tar.gz: b4a989ce4acb13cf16f816bc27565b8f68bf06a3b2bc7d8806ab2439ee901e590649a4f19182c0156080c3cb71365bbf1fb80a88c35d4ee8d2a833491c2f0e06
6
+ metadata.gz: 1200a0ae4d699541d3402eb41405b833bfee3e36e27c056d76a823d8d201fed7296cab3eb9bbd30e8757393200fbfd7771f7b38f1aa373e47a1619f692866983
7
+ data.tar.gz: 3062a85ec71403443e653fdd888f2b67878c236934c0f16fef2013c14751869c82237a9fb352f8f1d8f0d4c50ed2970622942b558c4c1c0db4115156ef416b13
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  class FixtureSet
3
- VERSION = '1.1.2'
3
+ VERSION = '1.2.1'
4
4
  end
5
5
  end
6
6
 
@@ -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
 
@@ -7,4 +7,10 @@ geoffroy:
7
7
  user1:
8
8
  firstname: Margot
9
9
  lastname: Last
10
- fuckit: true
10
+ address:
11
+ city: Strasbourg
12
+ homes:
13
+ - name: Home
14
+ address:
15
+ city: Strasbourg
16
+ real: true
@@ -0,0 +1,9 @@
1
+ class Address
2
+ include Mongoid::Document
3
+
4
+ embedded_in :place, polymorphic: true
5
+
6
+ field :city
7
+ field :real, type: Boolean, default: true
8
+ end
9
+
@@ -0,0 +1,8 @@
1
+ class Home
2
+ include Mongoid::Document
3
+
4
+ field :name
5
+
6
+ embeds_one :address, as: :place
7
+ end
8
+
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.2
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