active_mongoid 0.1.0

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.
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,60 @@
1
+ module ActiveMongoid
2
+ module Finders
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def find(*args)
8
+ key = args.flatten.first
9
+ if !key.is_a?(Fixnum) && (key.is_a?(::ActiveMongoid::BSON::ObjectId) || ::ActiveMongoid::BSON::ObjectId.legal?(key))
10
+ where({_id: key.to_s}).first.tap do |obj|
11
+ raise ActiveRecord::RecordNotFound unless obj
12
+ end
13
+ else
14
+ FinderProxy.new(super(*args))
15
+ end
16
+ end
17
+
18
+ def where(opts = :chain, *rest)
19
+ if opts && opts.respond_to?(:select)
20
+ bson_opts = opts.select{|k,v| v.is_a?(::ActiveMongoid::BSON::ObjectId)}
21
+
22
+ if bson_opts[:id]
23
+ opts.delete(:id)
24
+ bson_opts[:_id] = bson_opts.delete(:id)
25
+ end
26
+
27
+ bson_opts.each do |k,v|
28
+ bson_opts[k] = v.to_s
29
+ end
30
+
31
+ opts.merge!(bson_opts)
32
+ end
33
+ FinderProxy.new(super(opts, *rest))
34
+ end
35
+
36
+ def scoped(options = nil)
37
+ FinderProxy.new(super(options))
38
+ end
39
+
40
+ def includes(*args)
41
+ FinderProxy.new(super(*args))
42
+ end
43
+
44
+ def joins(*args)
45
+ FinderProxy.new(super(*args))
46
+ end
47
+
48
+ def select(select = nil)
49
+ FinderProxy.new(
50
+ if block_given?
51
+ load_target.select.each { |e| yield e }
52
+ else
53
+ scoped.select(select)
54
+ end
55
+ )
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveMongoid
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,330 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveMongoid::Associations::DocumentRelation::Accessors do
4
+
5
+ describe "#\{name}=" do
6
+
7
+ context "when the relation is a has_one" do
8
+ let(:player) { Player.create }
9
+
10
+ context "when the relation does not exist" do
11
+ let!(:person) { player.build_person }
12
+
13
+ it "builds the document" do
14
+ expect(player).to have_person
15
+ end
16
+
17
+
18
+ it "does not save the document" do
19
+ expect(person).to_not be_persisted
20
+ end
21
+ end
22
+
23
+ context "when the relation already exists" do
24
+ let!(:original_person) { player.create_person }
25
+ let(:new_person) { Person.new }
26
+
27
+ before do
28
+ player.person = new_person
29
+ end
30
+
31
+ it "substitutes new document" do
32
+ expect(player.person).to eq(new_person)
33
+ end
34
+
35
+ it "removes old relation" do
36
+ expect(original_person.player_id).to be_nil
37
+ end
38
+ end
39
+
40
+ context "when foreign_key is defined" do
41
+ let!(:post) { player.build_post }
42
+
43
+ it "build the document" do
44
+ expect(player).to have_post
45
+ end
46
+
47
+ it "binds the reverse" do
48
+ expect(post).to have_player
49
+ end
50
+ end
51
+
52
+ context "when the relation is polymorphic" do
53
+ let(:stat) { Stat.new }
54
+ before do
55
+ player.stat = stat
56
+ end
57
+
58
+ it "builds the document" do
59
+ expect(player).to have_stat
60
+ end
61
+
62
+ it "binds the reverse" do
63
+ expect(stat).to have_target
64
+ expect(stat.target).to eq(player)
65
+ end
66
+ end
67
+
68
+ end
69
+
70
+ context "when relation is a belongs_to" do
71
+ context "when inverse is has_one_record" do
72
+ let(:division) { Division.create }
73
+
74
+ context "when the relation does not exist" do
75
+
76
+ let!(:league) do
77
+ division.build_league
78
+ end
79
+
80
+ it "builds the document" do
81
+ expect(division).to have_league
82
+ end
83
+
84
+ it "does not save the document" do
85
+ expect(league).to_not be_persisted
86
+ end
87
+
88
+ end
89
+
90
+ context "when the relation already exists" do
91
+ let!(:original_league) { division.create_league }
92
+ let(:new_league) { League.new }
93
+
94
+ before do
95
+ division.league = new_league
96
+ end
97
+
98
+ it "substitutes new document" do
99
+ expect(division.league).to eq(new_league)
100
+ end
101
+
102
+ it "removes old relation" do
103
+ expect(division.league_id).to eq(new_league.id)
104
+ end
105
+ end
106
+
107
+ context "when the relation is polymorphic" do
108
+ let(:address) { Address.create }
109
+ let(:league) { League.create }
110
+
111
+ before do
112
+ address.target = league
113
+ end
114
+
115
+ it "builds the document" do
116
+ expect(address).to have_target
117
+ end
118
+
119
+ it "sets the attributes" do
120
+ expect(address.target_id).to eq(league.id)
121
+ expect(address.target_type).to eq(league.class.to_s)
122
+ end
123
+
124
+ it "reloads the document" do
125
+ expect(address.target(true)).to eq(league)
126
+ end
127
+
128
+ it "binds the inverse" do
129
+ expect(league.address).to eq(address)
130
+ end
131
+ end
132
+ end
133
+
134
+ context "when inverse is a has_many_records" do
135
+ let(:player) { Player.create }
136
+
137
+ context "when the relation does not exist" do
138
+ let!(:team) { player.build_team }
139
+
140
+ it "builds the document" do
141
+ expect(player).to have_team
142
+ end
143
+
144
+ it "does not save the document" do
145
+ expect(team).to_not be_persisted
146
+ end
147
+
148
+ it "binds the inverse" do
149
+ expect(team.players).to eq([player])
150
+ end
151
+
152
+ end
153
+
154
+ context "when the relation already exists" do
155
+ let!(:original_team) { player.create_team }
156
+ let(:new_team) { Team.new }
157
+
158
+ before do
159
+ player.team = new_team
160
+ end
161
+
162
+ it "substitutes new document" do
163
+ expect(player.team).to eq(new_team)
164
+ end
165
+
166
+ it "removes old relation" do
167
+ expect(player.team_id).to eq(new_team.id)
168
+ end
169
+ end
170
+ end
171
+
172
+ end
173
+
174
+ context "when the relation is a has_many_documents" do
175
+ let(:division) { Division.create }
176
+ let(:teams) { [Team.new] }
177
+
178
+ context "when the relation does not exist" do
179
+
180
+ before do
181
+ division.teams = teams
182
+ end
183
+
184
+ it "builds the document" do
185
+ expect(division).to have_teams
186
+ end
187
+ end
188
+
189
+ context "when the relation already exists" do
190
+ let!(:original_teams) { [division.teams.create] }
191
+ let(:new_teams) { [Team.new] }
192
+
193
+ before do
194
+ division.teams = new_teams
195
+ end
196
+
197
+ it "substitutes new document" do
198
+ expect(division.teams).to eq(new_teams)
199
+ end
200
+
201
+ it "removes old relation" do
202
+ original_teams.each do |team|
203
+ expect(team.division_id).to be_nil
204
+ end
205
+ end
206
+ end
207
+
208
+ context "when the relation is polymorphic" do
209
+ let(:address) { Address.new }
210
+ let(:person) { Person.create }
211
+
212
+ before do
213
+ person.addresses = [address]
214
+ end
215
+
216
+ it "builds the document" do
217
+ expect(person.addresses).to eq([address])
218
+ end
219
+
220
+ it "sets the attributes" do
221
+ expect(address.target_id).to eq(person.id)
222
+ expect(address.target_type).to eq(person.class.to_s)
223
+ end
224
+
225
+ it "binds the inverse" do
226
+ expect(address).to have_target
227
+ end
228
+ end
229
+
230
+ end
231
+
232
+ end
233
+
234
+ describe "#\{name}" do
235
+ context "when the relation is a has_one_document" do
236
+ let(:player) { Player.create }
237
+
238
+ context "when relation exists" do
239
+ before do
240
+ Person.create(player_id: player.id)
241
+ end
242
+
243
+ it "finds the document" do
244
+ expect(player).to have_person
245
+ end
246
+ end
247
+
248
+ context "when relation does not exist" do
249
+ it "does not find the document" do
250
+ expect(player).to_not have_person
251
+ end
252
+ end
253
+
254
+ context "when relation is polymorphic" do
255
+ let!(:stat) { Stat.create(target_id: player.id, target_type: player.class) }
256
+
257
+ it "finds the document" do
258
+ expect(player.stat).to eq(stat)
259
+ end
260
+ end
261
+ end
262
+
263
+ context "when the relation is a belongs_to_document" do
264
+ let(:league) { League.create }
265
+
266
+ context "when relation exists" do
267
+ let(:division) { Division.new(league_id: league.id) }
268
+
269
+ it "builds the record" do
270
+ expect(division).to have_league
271
+ expect(division.league).to eq(league)
272
+ end
273
+ end
274
+
275
+ context "when relation does not exist" do
276
+ let(:divison) { Division.new }
277
+
278
+ it "does not find record" do
279
+ expect(divison).to_not have_league
280
+ end
281
+ end
282
+
283
+ context "when relation is polymorphic" do
284
+ let(:address) { Address.create(target_id: league.id, target_type: league.class) }
285
+
286
+ it "finds the document" do
287
+ expect(address.target).to eq(league)
288
+ end
289
+ end
290
+
291
+ end
292
+
293
+ context "when the relation is a has_many_documents" do
294
+ let(:division) { Division.create }
295
+
296
+ context "when relation exists" do
297
+ before do
298
+ Team.create(name: 'b', division_id: division.id)
299
+ Team.create(name: 'a', division_id: division.id)
300
+ end
301
+
302
+ it "finds the records" do
303
+ expect(division).to have_teams
304
+ end
305
+
306
+ it "finds the records in order" do
307
+ expect(division.teams.first.name).to eq('a')
308
+ expect(division.teams.last.name).to eq('b')
309
+ end
310
+ end
311
+
312
+ context "when relation does not exist" do
313
+ it "does not find the document" do
314
+ expect(division).to_not have_teams
315
+ end
316
+ end
317
+
318
+ context "when relation is polymorphic" do
319
+ let!(:stat) { Stat.create(target_id: division.id, target_type: division.class) }
320
+
321
+ it "finds the document" do
322
+ expect(division.stats).to eq([stat])
323
+ end
324
+ end
325
+
326
+ end
327
+
328
+ end
329
+
330
+ end
@@ -0,0 +1,157 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::DocumentRelation::AutoSave do
4
+
5
+ describe ".autosave_documents" do
6
+
7
+ describe "relation is a has_one" do
8
+
9
+ before(:all) do
10
+ Player.autosave_documents(Player.am_relations["person"].merge!(autosave: true))
11
+ end
12
+
13
+ after(:all) do
14
+ Player.reset_callbacks(:save)
15
+ end
16
+
17
+ let(:player) do
18
+ Player.new
19
+ end
20
+
21
+ context "when the option is provided" do
22
+
23
+ let(:person) do
24
+ Person.new(name: "Foo")
25
+ end
26
+
27
+ before do
28
+ player.person = person
29
+ end
30
+
31
+ context "when saving the parent document" do
32
+
33
+ before do
34
+ player.save
35
+ end
36
+
37
+ it "does save the self" do
38
+ expect(player).to be_persisted
39
+ end
40
+
41
+ it "does save the relation" do
42
+ expect(person).to be_persisted
43
+ end
44
+
45
+ it "does save the relation id" do
46
+ expect(player.id).to_not be_nil
47
+ expect(person.player_id).to eq(player.id)
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ describe "relation is a belongs_to" do
57
+
58
+ before(:all) do
59
+ Division.autosave_documents(Division.am_relations["league"].merge!(autosave: true))
60
+ end
61
+
62
+ after(:all) do
63
+ Division.reset_callbacks(:save)
64
+ end
65
+
66
+ let(:division) do
67
+ Division.new
68
+ end
69
+
70
+ context "when the option is provided" do
71
+
72
+ let(:league) do
73
+ League.new(name: "Foo")
74
+ end
75
+
76
+ before do
77
+ division.league = league
78
+ end
79
+
80
+ context "when saving the parent document" do
81
+
82
+ before do
83
+ division.save
84
+ end
85
+
86
+ it "does save self" do
87
+ expect(division).to be_persisted
88
+ end
89
+
90
+ it "does save the relation" do
91
+ expect(league).to be_persisted
92
+ end
93
+
94
+ it "does save the relation id" do
95
+ expect(league.id).to_not be_nil
96
+ expect(division.league_id).to eq(league.id)
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ describe "relation is a has_many" do
106
+
107
+ before(:all) do
108
+ Division.autosave_documents(Division.am_relations["teams"].merge!(autosave: true))
109
+ end
110
+
111
+ after(:all) do
112
+ Division.reset_callbacks(:save)
113
+ end
114
+
115
+ let(:division) do
116
+ Division.new
117
+ end
118
+
119
+ let(:team) do
120
+ Team.new(name: "Foo")
121
+ end
122
+
123
+ context "when the option is provided" do
124
+
125
+ before do
126
+ division.teams << team
127
+ end
128
+
129
+ context "when saving the parent document" do
130
+
131
+ before do
132
+ division.save
133
+ end
134
+
135
+ it "does save self" do
136
+ expect(division).to be_persisted
137
+ end
138
+
139
+ it "does save the relation" do
140
+ expect(team).to be_persisted
141
+ end
142
+
143
+ it "does save the relation id" do
144
+ expect(division.id).to_not be_nil
145
+ expect(team.division_id).to eq(division.id)
146
+ end
147
+
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ end
155
+
156
+ end
157
+