mongoid-fixture_set 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02b82e3dfd784198cbf75724b2478e14f557a4a8
4
- data.tar.gz: 4410c8f10c4ae2497c3394071be568b2f5a2e9cd
3
+ metadata.gz: 3f8d0e4ca3845297cbc4d603eb34e7498f6472c5
4
+ data.tar.gz: 2cbc44d1a2aac525c1d6741f03edcece4f9dceca
5
5
  SHA512:
6
- metadata.gz: 1200a0ae4d699541d3402eb41405b833bfee3e36e27c056d76a823d8d201fed7296cab3eb9bbd30e8757393200fbfd7771f7b38f1aa373e47a1619f692866983
7
- data.tar.gz: 3062a85ec71403443e653fdd888f2b67878c236934c0f16fef2013c14751869c82237a9fb352f8f1d8f0d4c50ed2970622942b558c4c1c0db4115156ef416b13
6
+ metadata.gz: c8faf0c64f4475fcabb706e78b45dbafd36c933281ea8b693e6b853fbbc0a92d04958d09f4b5e00fdc5884fb03f68e69b2052350e63ab47b95f0c4345272ffcd
7
+ data.tar.gz: 4536e3e4d4597db95ab88e3c77dad7cebdcb82d6d2c8be16ed49458b073e5866f1984b6240f769f9fa1e80bfb5d589eeeb12bd407286d76c9357920f52f3bdeb
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  class FixtureSet
3
- VERSION = '1.2.1'
3
+ VERSION = '1.3.0'
4
4
  end
5
5
  end
6
6
 
@@ -210,6 +210,16 @@ module Mongoid
210
210
  case relation.macro
211
211
  when :belongs_to
212
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
222
+
213
223
  if relation.polymorphic? && value.sub!(/\s*\(([^)]*)\)\s*/, '')
214
224
  type = $1
215
225
  attributes[relation.foreign_key.sub(/_id$/, '_type')] = type
@@ -221,6 +231,18 @@ module Mongoid
221
231
  when :has_many
222
232
  if values = attributes.delete(relation.name.to_s)
223
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
+
224
246
  document = self.class.find_or_create_document(relation.class_name, value)
225
247
  if relation.polymorphic?
226
248
  self.class.update_document(document, {
@@ -4,6 +4,10 @@ geoffroy:
4
4
  main_group: sudoers
5
5
  groups:
6
6
  - print
7
+ items:
8
+ - {
9
+ name: Test
10
+ }
7
11
  user1:
8
12
  firstname: Margot
9
13
  lastname: Last
@@ -14,3 +18,7 @@ user1:
14
18
  address:
15
19
  city: Strasbourg
16
20
  real: true
21
+ user2:
22
+ firstname: user2
23
+ main_group:
24
+ name: Win?
@@ -0,0 +1,8 @@
1
+ class Item
2
+ include Mongoid::Document
3
+
4
+ belongs_to :user
5
+
6
+ field :name
7
+ end
8
+
data/test/models/user.rb CHANGED
@@ -13,5 +13,8 @@ class User
13
13
  belongs_to :main_group, class_name: 'Group', inverse_of: :main_users
14
14
 
15
15
  has_and_belongs_to_many :groups
16
+
17
+ has_many :items
18
+ accepts_nested_attributes_for :items
16
19
  end
17
20
 
@@ -33,6 +33,17 @@ module Mongoid
33
33
  end
34
34
  end
35
35
 
36
+ test 'should raised if nested polymorphic relation' do
37
+ Mongoid::FixtureSet.reset_cache
38
+
39
+ begin
40
+ fs = Mongoid::FixtureSet.create_fixtures('test/nested_polymorphic_relation_fixtures', %w(groups))
41
+ assert false
42
+ rescue Mongoid::FixtureSet::FixtureError
43
+ assert true
44
+ end
45
+ end
46
+
36
47
  def test_should_create_fixtures
37
48
  Mongoid::FixtureSet.reset_cache
38
49
  fs = Mongoid::FixtureSet.create_fixtures('test/fixtures/', %w(users groups schools organisations))
@@ -41,7 +52,7 @@ module Mongoid
41
52
  f_geoffroy = users['geoffroy']
42
53
 
43
54
  assert_equal 6, School.count
44
- assert_equal 4, User.count
55
+ assert_equal 5, User.count
45
56
 
46
57
  geoffroy = User.find_by(firstname: 'Geoffroy')
47
58
  user1 = User.find_by(firstname: 'Margot')
@@ -50,6 +61,9 @@ module Mongoid
50
61
  group1 = Group.find_by(name: 'Margot')
51
62
  orga1 = Organisation.find_by(name: '1 Organisation')
52
63
  school = School.find_by(name: 'School')
64
+ test_item = Item.find_by(name: 'Test')
65
+ user2 = User.find_by(firstname: 'user2')
66
+ win_group = Group.find_by(name: 'Win?')
53
67
 
54
68
  assert_equal 1, user1.homes.count
55
69
  assert_equal geoffroy, f_geoffroy.find
@@ -59,6 +73,8 @@ module Mongoid
59
73
  assert sudoers.main_users.include?(geoffroy)
60
74
  assert_equal group1, user1.main_group
61
75
  assert_equal print, user1.groups.first
76
+ assert_equal geoffroy, test_item.user
77
+ assert win_group.main_users.include?(user2)
62
78
 
63
79
  assert_equal 1, school.groups.count
64
80
  assert_equal group1, school.groups.first
@@ -0,0 +1,4 @@
1
+ will_fail:
2
+ name: will_fail
3
+ something:
4
+ name: NOK
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-fixture_set
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoffroy Planquart
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-05 00:00:00.000000000 Z
11
+ date: 2015-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongoid
@@ -79,6 +79,7 @@ files:
79
79
  - test/models/home.rb
80
80
  - test/models/address.rb
81
81
  - test/models/user.rb
82
+ - test/models/item.rb
82
83
  - test/mongoid.yml
83
84
  - test/fixtures/groups.yml
84
85
  - test/fixtures/schools.yml
@@ -88,6 +89,7 @@ files:
88
89
  - test/fixtures/users.yml
89
90
  - test/test_helper.rb
90
91
  - test/load_once_fixtures/tests.yml
92
+ - test/nested_polymorphic_relation_fixtures/groups.yml
91
93
  homepage: https://github.com/Aethelflaed/mongoid-fixture_set
92
94
  licenses:
93
95
  - MIT
@@ -123,6 +125,7 @@ test_files:
123
125
  - test/models/home.rb
124
126
  - test/models/address.rb
125
127
  - test/models/user.rb
128
+ - test/models/item.rb
126
129
  - test/mongoid.yml
127
130
  - test/fixtures/groups.yml
128
131
  - test/fixtures/schools.yml
@@ -132,4 +135,5 @@ test_files:
132
135
  - test/fixtures/users.yml
133
136
  - test/test_helper.rb
134
137
  - test/load_once_fixtures/tests.yml
138
+ - test/nested_polymorphic_relation_fixtures/groups.yml
135
139
  has_rdoc: