active_mongoid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.coveralls.yml +1 -0
  4. data/.gitignore +22 -0
  5. data/.ruby_gemset +1 -0
  6. data/.ruby_version +1 -0
  7. data/.travis.yml +11 -0
  8. data/Appraisals +7 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +198 -0
  12. data/Rakefile +7 -0
  13. data/active_mongoid.gemspec +36 -0
  14. data/gemfiles/mongoid_2.8.gemfile +7 -0
  15. data/gemfiles/mongoid_2.8.gemfile.lock +105 -0
  16. data/gemfiles/mongoid_3.1.gemfile +7 -0
  17. data/gemfiles/mongoid_3.1.gemfile.lock +108 -0
  18. data/lib/active_mongoid.rb +8 -0
  19. data/lib/active_mongoid/associations.rb +82 -0
  20. data/lib/active_mongoid/associations/binding.rb +77 -0
  21. data/lib/active_mongoid/associations/builder.rb +26 -0
  22. data/lib/active_mongoid/associations/builders/in.rb +17 -0
  23. data/lib/active_mongoid/associations/builders/many.rb +15 -0
  24. data/lib/active_mongoid/associations/builders/one.rb +15 -0
  25. data/lib/active_mongoid/associations/document_relation/accessors.rb +100 -0
  26. data/lib/active_mongoid/associations/document_relation/associations.rb +33 -0
  27. data/lib/active_mongoid/associations/document_relation/auto_save.rb +31 -0
  28. data/lib/active_mongoid/associations/document_relation/bindings/in.rb +48 -0
  29. data/lib/active_mongoid/associations/document_relation/bindings/many.rb +19 -0
  30. data/lib/active_mongoid/associations/document_relation/bindings/one.rb +19 -0
  31. data/lib/active_mongoid/associations/document_relation/builders.rb +31 -0
  32. data/lib/active_mongoid/associations/document_relation/dependent.rb +26 -0
  33. data/lib/active_mongoid/associations/document_relation/macros.rb +51 -0
  34. data/lib/active_mongoid/associations/document_relation/referenced/in.rb +72 -0
  35. data/lib/active_mongoid/associations/document_relation/referenced/many.rb +125 -0
  36. data/lib/active_mongoid/associations/document_relation/referenced/one.rb +75 -0
  37. data/lib/active_mongoid/associations/many.rb +211 -0
  38. data/lib/active_mongoid/associations/metadata.rb +229 -0
  39. data/lib/active_mongoid/associations/one.rb +21 -0
  40. data/lib/active_mongoid/associations/proxy.rb +38 -0
  41. data/lib/active_mongoid/associations/record_relation/accessors.rb +80 -0
  42. data/lib/active_mongoid/associations/record_relation/associations.rb +33 -0
  43. data/lib/active_mongoid/associations/record_relation/auto_save.rb +43 -0
  44. data/lib/active_mongoid/associations/record_relation/bindings/in.rb +48 -0
  45. data/lib/active_mongoid/associations/record_relation/bindings/many.rb +19 -0
  46. data/lib/active_mongoid/associations/record_relation/bindings/one.rb +19 -0
  47. data/lib/active_mongoid/associations/record_relation/builders.rb +31 -0
  48. data/lib/active_mongoid/associations/record_relation/dependent.rb +26 -0
  49. data/lib/active_mongoid/associations/record_relation/macros.rb +65 -0
  50. data/lib/active_mongoid/associations/record_relation/referenced/in.rb +72 -0
  51. data/lib/active_mongoid/associations/record_relation/referenced/many.rb +128 -0
  52. data/lib/active_mongoid/associations/record_relation/referenced/one.rb +75 -0
  53. data/lib/active_mongoid/associations/targets/enumerable.rb +161 -0
  54. data/lib/active_mongoid/bson_id.rb +44 -0
  55. data/lib/active_mongoid/finder_proxy.rb +55 -0
  56. data/lib/active_mongoid/finders.rb +60 -0
  57. data/lib/active_mongoid/version.rb +3 -0
  58. data/spec/lib/associations/document_relation/accessors_spec.rb +330 -0
  59. data/spec/lib/associations/document_relation/auto_save_spec.rb +157 -0
  60. data/spec/lib/associations/document_relation/bindings/in_spec.rb +39 -0
  61. data/spec/lib/associations/document_relation/bindings/many_spec.rb +36 -0
  62. data/spec/lib/associations/document_relation/bindings/one_spec.rb +39 -0
  63. data/spec/lib/associations/document_relation/builders_spec.rb +117 -0
  64. data/spec/lib/associations/document_relation/dependent_spec.rb +87 -0
  65. data/spec/lib/associations/document_relation/macros_spec.rb +68 -0
  66. data/spec/lib/associations/document_relation/referenced/in_spec.rb +27 -0
  67. data/spec/lib/associations/document_relation/referenced/many_spec.rb +32 -0
  68. data/spec/lib/associations/document_relation/referenced/one_spec.rb +28 -0
  69. data/spec/lib/associations/metadata_spec.rb +157 -0
  70. data/spec/lib/associations/record_relation/accessors_spec.rb +328 -0
  71. data/spec/lib/associations/record_relation/auto_save_spec.rb +157 -0
  72. data/spec/lib/associations/record_relation/bindings/in_spec.rb +39 -0
  73. data/spec/lib/associations/record_relation/bindings/many_spec.rb +39 -0
  74. data/spec/lib/associations/record_relation/bindings/one_spec.rb +57 -0
  75. data/spec/lib/associations/record_relation/builders_spec.rb +118 -0
  76. data/spec/lib/associations/record_relation/dependent_spec.rb +87 -0
  77. data/spec/lib/associations/record_relation/macros_spec.rb +73 -0
  78. data/spec/lib/associations/record_relation/referenced/in_spec.rb +27 -0
  79. data/spec/lib/associations/record_relation/referenced/many_spec.rb +32 -0
  80. data/spec/lib/associations/record_relation/referenced/one_spec.rb +27 -0
  81. data/spec/lib/bson_id_spec.rb +48 -0
  82. data/spec/lib/finders_spec.rb +105 -0
  83. data/spec/spec_helper.rb +89 -0
  84. data/spec/support/models/active_record/address.rb +6 -0
  85. data/spec/support/models/active_record/division.rb +16 -0
  86. data/spec/support/models/active_record/division_setting.rb +9 -0
  87. data/spec/support/models/active_record/player.rb +12 -0
  88. data/spec/support/models/mongoid/league.rb +10 -0
  89. data/spec/support/models/mongoid/person.rb +9 -0
  90. data/spec/support/models/mongoid/post.rb +9 -0
  91. data/spec/support/models/mongoid/stat.rb +8 -0
  92. data/spec/support/models/mongoid/team.rb +9 -0
  93. data/spec/support/shared_examples/shared_many_spec.rb +411 -0
  94. metadata +370 -0
@@ -0,0 +1,28 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::DocumentRelation::Referenced::One do
4
+
5
+ describe ".criteria" do
6
+
7
+
8
+ context "" do
9
+
10
+ let(:id) do
11
+ 1
12
+ end
13
+
14
+ let(:metadata) do
15
+ Player.am_relations["person"]
16
+ end
17
+
18
+ let(:criteria) do
19
+ described_class.criteria(metadata, id, Person)
20
+ end
21
+
22
+ it "does not include the type in the criteria" do
23
+ expect(criteria.selector).to eq({"player_id" => id})
24
+ expect(criteria.klass).to eq(Person)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,157 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::Metadata do
4
+
5
+ let(:has_one_document_relation) { ActiveMongoid::Associations::DocumentRelation::Referenced::One }
6
+ let(:has_many_documents_relation) { ActiveMongoid::Associations::DocumentRelation::Referenced::Many }
7
+ let(:belongs_to_document_relation) { ActiveMongoid::Associations::DocumentRelation::Referenced::In }
8
+ let(:has_one_record_relation) { ActiveMongoid::Associations::RecordRelation::Referenced::One }
9
+ let(:has_many_records_relation) { ActiveMongoid::Associations::RecordRelation::Referenced::Many }
10
+ let(:belong_to_record_relation) { ActiveMongoid::Associations::RecordRelation::Referenced::In }
11
+
12
+ describe "#builder" do
13
+
14
+ let(:metadata) do
15
+ described_class.new(relation: has_one_document_relation)
16
+ end
17
+
18
+ let(:object) do
19
+ double
20
+ end
21
+
22
+ let(:base) do
23
+ double
24
+ end
25
+
26
+ it "returns the builder from the relation" do
27
+ expect(
28
+ metadata.builder(base, object)
29
+ ).to be_a_kind_of(ActiveMongoid::Associations::Builders::One)
30
+ end
31
+ end
32
+
33
+ describe "#class_name" do
34
+ context "when class_name provided" do
35
+ context "when the class name contains leading ::" do
36
+ let(:metadata) do
37
+ described_class.new(
38
+ relation: has_one_document_relation,
39
+ class_name: "::Person"
40
+ )
41
+ end
42
+
43
+ it "returns the stripped class name" do
44
+ expect(metadata.class_name).to eq("Person")
45
+ end
46
+ end
47
+
48
+ context "when the class name has no prefix" do
49
+ let(:metadata) do
50
+ described_class.new(
51
+ relation: Mongoid::Relations::Referenced::Many,
52
+ class_name: "Person"
53
+ )
54
+ end
55
+
56
+ it "constantizes the class name" do
57
+ expect(metadata.class_name).to eq("Person")
58
+ end
59
+ end
60
+ end
61
+
62
+ context "when no class_name provided" do
63
+ context "when inverse_class_name is provided" do
64
+ context "when inverse_class_name is a simple class name" do
65
+ context "when association name is singular" do
66
+ let(:relation) do
67
+ has_one_document_relation
68
+ end
69
+
70
+ let(:metadata) do
71
+ described_class.new(name: :name, relation: relation, inverse_class_name: "Person")
72
+ end
73
+
74
+ it "classifies and constantizes the association name and adds the module" do
75
+ expect(metadata.class_name).to eq("Name")
76
+ end
77
+ end
78
+
79
+ context "when association name is plural" do
80
+ let(:relation) do
81
+ has_many_documents_relation
82
+ end
83
+
84
+ let(:metadata) do
85
+ described_class.new(name: :addresses, relation: relation, inverse_class_name: "Person")
86
+ end
87
+
88
+ it "classifies and constantizes the association name and adds the module" do
89
+ expect(metadata.class_name).to eq("Address")
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ context "when no inverse_class_name is provided" do
96
+ context "when association name is singular" do
97
+
98
+ let(:relation) do
99
+ has_one_document_relation
100
+ end
101
+
102
+ let(:metadata) do
103
+ described_class.new(name: :name, relation: relation)
104
+ end
105
+
106
+ it "classifies and constantizes the association name" do
107
+ expect(metadata.class_name).to eq("Name")
108
+ end
109
+ end
110
+
111
+ context "when association name is plural" do
112
+
113
+ let(:relation) do
114
+ has_many_documents_relation
115
+ end
116
+
117
+ let(:metadata) do
118
+ described_class.new(name: :addresses, relation: relation)
119
+ end
120
+
121
+ it "classifies and constantizes the association name" do
122
+ expect(metadata.class_name).to eq("Address")
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+
129
+ describe "#destructive?" do
130
+ context "when the relation has a destructive dependent option" do
131
+ let(:metadata) do
132
+ described_class.new(
133
+ relation: has_many_documents_relation,
134
+ dependent: :destroy
135
+ )
136
+ end
137
+
138
+ it "returns true" do
139
+ expect(metadata).to be_destructive
140
+ end
141
+ end
142
+
143
+ context "when no dependent option" do
144
+ let(:metadata) do
145
+ described_class.new(
146
+ relation: has_many_documents_relation
147
+ )
148
+ end
149
+
150
+ it "returns false" do
151
+ expect(metadata).to_not be_destructive
152
+ end
153
+ end
154
+ end
155
+
156
+
157
+ end
@@ -0,0 +1,328 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveMongoid::Associations::RecordRelation::Accessors do
4
+
5
+ describe "#\{name}=" do
6
+
7
+ context "when the relation is a has_one" do
8
+ let(:league) { League.create }
9
+
10
+ context "when the relation does not exist" do
11
+ let!(:division) { league.build_division }
12
+
13
+ it "builds the record" do
14
+ expect(league).to have_division
15
+ expect(league.division).to eq(division)
16
+ end
17
+
18
+
19
+ it "does not save the record" do
20
+ expect(division).to_not be_persisted
21
+ end
22
+ end
23
+
24
+ context "when the relation does not exist and class_name is specified" do
25
+ let!(:division_setting) do
26
+ league.build_division_setting
27
+ end
28
+
29
+ it "builds the record" do
30
+ expect(league).to have_division_setting
31
+ expect(league.division_setting).to eq(division_setting)
32
+ end
33
+
34
+
35
+ it "does not save the record" do
36
+ expect(division_setting).to_not be_persisted
37
+ end
38
+ end
39
+
40
+ context "when the relation already does exist" do
41
+ let!(:original_division) { league.create_division }
42
+ let(:new_division) { Division.new }
43
+
44
+ before do
45
+ league.division = new_division
46
+ end
47
+
48
+ it "substitutes new record" do
49
+ expect(league.division).to eq(new_division)
50
+ end
51
+
52
+ it "removes old relation" do
53
+ expect(original_division.league_id).to be_nil
54
+ expect(original_division).to be_persisted
55
+ end
56
+ end
57
+
58
+ context "when foreign_key is defined" do
59
+ let(:post) { Post.new }
60
+ let!(:division) { post.build_division }
61
+
62
+ it "build the document" do
63
+ expect(post).to have_division
64
+ end
65
+
66
+ it "binds the reverse" do
67
+ expect(division).to have_post
68
+ end
69
+ end
70
+
71
+ context "when the relation is polymorphic" do
72
+ let(:address) { Address.new }
73
+ before do
74
+ league.address = address
75
+ end
76
+
77
+ it "builds the document" do
78
+ expect(league).to have_address
79
+ end
80
+
81
+ it "binds the reverse" do
82
+ expect(address).to have_target
83
+ expect(address.target).to eq(league)
84
+ end
85
+ end
86
+ end
87
+
88
+ context "when the relation is a belongs_to" do
89
+ context "when inverse is a has_one_document" do
90
+ let(:person) { Person.create }
91
+
92
+ context "when the relation does not exist" do
93
+ let!(:player) { person.build_player }
94
+
95
+ it "builds the record" do
96
+ expect(person).to have_player
97
+ expect(person.player).to eq(player)
98
+ end
99
+ end
100
+
101
+ context "when the relation already does exist" do
102
+ let!(:original_player) { person.create_player }
103
+ let(:new_player) { Player.new }
104
+
105
+ before do
106
+ person.player = new_player
107
+ end
108
+
109
+ it "substitutes new record" do
110
+ expect(person.player).to eq(new_player)
111
+ end
112
+
113
+ it "removes old relation" do
114
+ expect(person.player_id).to be_nil
115
+ end
116
+ end
117
+
118
+ context "when the relation is polymorphic" do
119
+ let(:stat) { Stat.create }
120
+ let(:player) { Player.create }
121
+
122
+ before do
123
+ stat.target = player
124
+ end
125
+
126
+ it "builds the record" do
127
+ expect(stat).to have_target
128
+ end
129
+
130
+ it "sets the attributes" do
131
+ expect(stat.target_id).to eq(player.id)
132
+ expect(stat.target_type).to eq(player.class.to_s)
133
+ end
134
+
135
+ it "reloads the record" do
136
+ expect(stat.target(true)).to eq(player)
137
+ end
138
+
139
+ it "binds the inverse" do
140
+ expect(player.stat).to eq(stat)
141
+ end
142
+ end
143
+ end
144
+
145
+ context "when inverse is a has_many_documents" do
146
+ let(:team) { Team.create}
147
+
148
+ context "when the relation does not exist" do
149
+ let!(:division) { team.build_division }
150
+
151
+ it "builds the record" do
152
+ expect(team).to have_division
153
+ expect(team.division).to eq(division)
154
+ end
155
+
156
+ it "does not save the record" do
157
+ expect(division).to_not be_persisted
158
+ end
159
+
160
+ it "binds the inverse" do
161
+ expect(division.teams).to eq([team])
162
+ end
163
+ end
164
+
165
+ context "when the relation already does exist" do
166
+ let!(:original_division) { team.create_division }
167
+ let(:new_division) { Division.new }
168
+
169
+ before do
170
+ team.division = new_division
171
+ end
172
+
173
+ it "substitutes new record" do
174
+ expect(team.division).to eq(new_division)
175
+ end
176
+ end
177
+ end
178
+
179
+ end
180
+
181
+ context "when the relation is a has_many_records" do
182
+ let(:team) { Team.create }
183
+ let(:players) { [Player.new] }
184
+
185
+ context "when the relation does not exist" do
186
+ before do
187
+ team.players = players
188
+ end
189
+
190
+ it "builds the record" do
191
+ expect(team).to have_players
192
+ end
193
+ end
194
+
195
+ context "when the relation already exists" do
196
+ let!(:original_players) { [team.players.create] }
197
+ let(:new_players) { [Player.new] }
198
+
199
+ before do
200
+ team.players = new_players
201
+ end
202
+
203
+ it "removes old relation" do
204
+ original_players.each do |player|
205
+ expect(player.team_id).to be_nil
206
+ end
207
+ end
208
+
209
+ end
210
+
211
+ context "when the relation is polymorphic" do
212
+ let(:address) { Address.new }
213
+ let(:person) { Person.create }
214
+
215
+ before do
216
+ person.addresses = [address]
217
+ end
218
+
219
+ it "builds the document" do
220
+ expect(person.addresses).to eq([address])
221
+ end
222
+
223
+ it "sets the attributes" do
224
+ expect(address.target_id).to eq(person.id)
225
+ expect(address.target_type).to eq(person.class.to_s)
226
+ end
227
+
228
+ it "binds the inverse" do
229
+ expect(address).to have_target
230
+ end
231
+ end
232
+
233
+ end
234
+
235
+ end
236
+
237
+ describe "#\{name}" do
238
+ let(:league) { League.create }
239
+
240
+ context "when the relation is a has_one_record" do
241
+ context "when relation exists" do
242
+ before do
243
+ Division.create(league_id: league.id)
244
+ end
245
+
246
+ it "finds the record" do
247
+ expect(league).to have_division
248
+ end
249
+ end
250
+
251
+ context "when relation does not exist" do
252
+ it "does not find the record" do
253
+ expect(league).to_not have_division
254
+ end
255
+ end
256
+
257
+ context "when relation is polymorphic" do
258
+ let!(:address) { Address.create }
259
+
260
+ before do
261
+ league.address = address
262
+ end
263
+
264
+ it "finds the record" do
265
+ expect(league.address).to eq(address)
266
+ end
267
+ end
268
+ end
269
+
270
+ context "when the relation is a has_many_records" do
271
+ let(:team) { Team.create }
272
+
273
+ context "when relation exists" do
274
+ before do
275
+ Player.create(name: 'b', team_id: team.id)
276
+ Player.create(name: 'a', team_id: team.id)
277
+ end
278
+
279
+ it "finds the records" do
280
+ expect(team).to have_players
281
+ end
282
+
283
+ it "finds the records in order" do
284
+ expect(team.players.first.name).to eq('a')
285
+ expect(team.players.last.name).to eq('b')
286
+ end
287
+ end
288
+ end
289
+
290
+ context "when the relation is a belongs_to" do
291
+ let(:player) do
292
+ Player.create
293
+ end
294
+
295
+ context "when relation exists" do
296
+ let(:person) do
297
+ Person.new(player_id: player.id)
298
+ end
299
+
300
+ it "builds the record" do
301
+ expect(person).to have_player
302
+ expect(person.player).to eq(player)
303
+ end
304
+ end
305
+
306
+ context "when relation does not exist" do
307
+ let(:person) do
308
+ Person.new
309
+ end
310
+
311
+ it "does not find record" do
312
+ expect(person).to_not have_player
313
+ end
314
+ end
315
+
316
+ context "when relation is polymorphic" do
317
+ let(:player) { Player.create }
318
+ let(:stat) { Stat.create(target_id: player.id, target_type: player.class) }
319
+
320
+ it "finds the document" do
321
+ expect(stat.target).to eq(player)
322
+ end
323
+ end
324
+ end
325
+
326
+ end
327
+
328
+ end