mongoid 4.0.0 → 4.0.2

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 (63) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +48 -1
  3. data/README.md +6 -2
  4. data/lib/config/locales/en.yml +2 -2
  5. data/lib/mongoid/atomic.rb +2 -2
  6. data/lib/mongoid/attributes.rb +2 -0
  7. data/lib/mongoid/contextual/aggregable/memory.rb +2 -2
  8. data/lib/mongoid/contextual/memory.rb +5 -5
  9. data/lib/mongoid/contextual/mongo.rb +13 -3
  10. data/lib/mongoid/criteria/#findable.rb# +141 -0
  11. data/lib/mongoid/document.rb +7 -7
  12. data/lib/mongoid/extensions.rb +13 -0
  13. data/lib/mongoid/findable.rb +27 -5
  14. data/lib/mongoid/persistable/creatable.rb +2 -1
  15. data/lib/mongoid/persistable/settable.rb +1 -1
  16. data/lib/mongoid/persistable/updatable.rb +2 -1
  17. data/lib/mongoid/query_cache.rb +10 -2
  18. data/lib/mongoid/railtie.rb +2 -15
  19. data/lib/mongoid/railties/database.rake +1 -1
  20. data/lib/mongoid/relations/accessors.rb +2 -2
  21. data/lib/mongoid/relations/binding.rb +1 -1
  22. data/lib/mongoid/relations/bindings/referenced/many_to_many.rb +1 -1
  23. data/lib/mongoid/relations/builders/embedded/one.rb +1 -1
  24. data/lib/mongoid/relations/builders/nested_attributes/one.rb +1 -1
  25. data/lib/mongoid/relations/counter_cache.rb +2 -2
  26. data/lib/mongoid/relations/many.rb +21 -0
  27. data/lib/mongoid/relations/one.rb +1 -1
  28. data/lib/mongoid/relations/referenced/many.rb +4 -4
  29. data/lib/mongoid/relations/referenced/many_to_many.rb +5 -5
  30. data/lib/mongoid/relations/synchronization.rb +4 -4
  31. data/lib/mongoid/relations/targets/enumerable.rb +10 -10
  32. data/lib/mongoid/reloadable.rb +3 -3
  33. data/lib/mongoid/sessions/options.rb +7 -2
  34. data/lib/mongoid/threaded.rb +26 -15
  35. data/lib/mongoid/traversable.rb +6 -2
  36. data/lib/mongoid/validatable/uniqueness.rb +3 -3
  37. data/lib/mongoid/version.rb +1 -1
  38. data/lib/rails/generators/mongoid/config/templates/mongoid.yml +33 -1
  39. data/spec/app/models/contextable_item.rb +5 -0
  40. data/spec/app/models/id_key.rb +6 -0
  41. data/spec/mongoid/#atomic_spec.rb# +365 -0
  42. data/spec/mongoid/attributes/nested_spec.rb +139 -144
  43. data/spec/mongoid/attributes_spec.rb +36 -0
  44. data/spec/mongoid/contextual/atomic_spec.rb +7 -13
  45. data/spec/mongoid/contextual/memory_spec.rb +7 -2
  46. data/spec/mongoid/criteria/modifiable_spec.rb +5 -8
  47. data/spec/mongoid/criteria_spec.rb +86 -75
  48. data/spec/mongoid/document_spec.rb +9 -5
  49. data/spec/mongoid/extensions_spec.rb +14 -0
  50. data/spec/mongoid/findable_spec.rb +99 -11
  51. data/spec/mongoid/persistable/creatable_spec.rb +41 -0
  52. data/spec/mongoid/persistable/savable_spec.rb +37 -0
  53. data/spec/mongoid/persistable/settable_spec.rb +23 -0
  54. data/spec/mongoid/positional_spec.rb +5 -10
  55. data/spec/mongoid/query_cache_spec.rb +32 -0
  56. data/spec/mongoid/relations/embedded/many_spec.rb +74 -0
  57. data/spec/mongoid/relations/referenced/many_spec.rb +153 -0
  58. data/spec/mongoid/relations/referenced/many_to_many_spec.rb +49 -0
  59. data/spec/mongoid/reloadable_spec.rb +23 -0
  60. data/spec/mongoid/sessions/options_spec.rb +2 -1
  61. data/spec/mongoid/sessions_spec.rb +30 -0
  62. data/spec/spec_helper.rb +2 -0
  63. metadata +10 -3
@@ -15,8 +15,7 @@ describe Mongoid::Positional do
15
15
  "$set" => {
16
16
  "field" => "value",
17
17
  "children.0.field" => "value",
18
- "children.0.children.1.children.3.field" => "value",
19
- "children.0.children.1.children.3.field" => "value",
18
+ "children.0.children.1.children.3.field" => "value"
20
19
  },
21
20
  "$pushAll" => {
22
21
  "children.0.children.1.children.3.fields" => [ "value", "value" ]
@@ -113,8 +112,7 @@ describe Mongoid::Positional do
113
112
  "$set" => {
114
113
  "field" => "value",
115
114
  "children.$.field" => "value",
116
- "children.$.children.1.children.3.field" => "value",
117
- "children.$.children.1.children.3.field" => "value",
115
+ "children.$.children.1.children.3.field" => "value"
118
116
  },
119
117
  "$pushAll" => {
120
118
  "children.$.children.1.children.3.fields" => [ "value", "value" ]
@@ -142,8 +140,7 @@ describe Mongoid::Positional do
142
140
  "$set" => {
143
141
  "field" => "value",
144
142
  "children.0.field" => "value",
145
- "children.0.children.1.children.3.field" => "value",
146
- "children.0.children.1.children.3.field" => "value",
143
+ "children.0.children.1.children.3.field" => "value"
147
144
  },
148
145
  "$pushAll" => {
149
146
  "children.0.children.1.children.3.fields" => [ "value", "value" ]
@@ -172,8 +169,7 @@ describe Mongoid::Positional do
172
169
  "$set" => {
173
170
  "field" => "value",
174
171
  "children.$.field" => "value",
175
- "children.0.children.$.children.3.field" => "value",
176
- "children.0.children.$.children.3.field" => "value",
172
+ "children.0.children.$.children.3.field" => "value"
177
173
  },
178
174
  "$pushAll" => {
179
175
  "children.0.children.$.children.3.fields" => [ "value", "value" ]
@@ -206,8 +202,7 @@ describe Mongoid::Positional do
206
202
  "$set" => {
207
203
  "field" => "value",
208
204
  "children.$.field" => "value",
209
- "children.0.children.1.children.$.field" => "value",
210
- "children.0.children.1.children.$.field" => "value",
205
+ "children.0.children.1.children.$.field" => "value"
211
206
  },
212
207
  "$pushAll" => {
213
208
  "children.0.children.1.children.$.fields" => [ "value", "value" ]
@@ -177,6 +177,38 @@ describe Mongoid::QueryCache do
177
177
  end
178
178
  end
179
179
 
180
+ context "when querying a very large collection" do
181
+
182
+ before do
183
+ 123.times { Band.create! }
184
+ end
185
+
186
+ it "returns the right number of records" do
187
+ expect(Band.all.to_a.length).to eq(123)
188
+ end
189
+
190
+ it "#pluck returns the same count of objects" do
191
+ expect(Band.pluck(:name).length).to eq(123)
192
+ end
193
+
194
+ context "when loading all the documents" do
195
+
196
+ before do
197
+ Band.all.to_a
198
+ end
199
+
200
+ it "caches the complete result of the query" do
201
+ expect_no_queries do
202
+ expect(Band.all.to_a.length).to eq(123)
203
+ end
204
+ end
205
+
206
+ it "returns the same count of objects when using #pluck" do
207
+ expect(Band.pluck(:name).length).to eq(123)
208
+ end
209
+ end
210
+ end
211
+
180
212
  context "when inserting an index" do
181
213
 
182
214
  it "does not cache the query" do
@@ -2120,6 +2120,80 @@ describe Mongoid::Relations::Embedded::Many do
2120
2120
  end
2121
2121
  end
2122
2122
 
2123
+ describe "#find_or_create_by!" do
2124
+
2125
+ let(:person) do
2126
+ Person.create
2127
+ end
2128
+
2129
+ let!(:address) do
2130
+ person.addresses.build(street: "Bourke", city: "Melbourne")
2131
+ end
2132
+
2133
+ context "when the document exists" do
2134
+
2135
+ let(:found) do
2136
+ person.addresses.find_or_create_by!(street: "Bourke")
2137
+ end
2138
+
2139
+ it "returns the document" do
2140
+ expect(found).to eq(address)
2141
+ end
2142
+ end
2143
+
2144
+ context "when the document does not exist" do
2145
+
2146
+ let(:found) do
2147
+ person.addresses.find_or_create_by!(street: "King") do |address|
2148
+ address.state = "CA"
2149
+ end
2150
+ end
2151
+
2152
+ it "sets the new document attributes" do
2153
+ expect(found.street).to eq("King")
2154
+ end
2155
+
2156
+ it "returns a newly persisted document" do
2157
+ expect(found).to be_persisted
2158
+ end
2159
+
2160
+ it "calls the passed block" do
2161
+ expect(found.state).to eq("CA")
2162
+ end
2163
+
2164
+ context "when validation fails" do
2165
+
2166
+ it "raises an error" do
2167
+ expect {
2168
+ person.addresses.find_or_create_by!(street: "1")
2169
+ }.to raise_error(Mongoid::Errors::Validations)
2170
+ end
2171
+ end
2172
+ end
2173
+
2174
+ context "when the child belongs to another document" do
2175
+
2176
+ let(:product) do
2177
+ Product.create
2178
+ end
2179
+
2180
+ let(:purchase) do
2181
+ Purchase.create
2182
+ end
2183
+
2184
+ let(:line_item) do
2185
+ purchase.line_items.find_or_create_by(
2186
+ product_id: product.id,
2187
+ product_type: product.class.name
2188
+ )
2189
+ end
2190
+
2191
+ it "properly creates the document" do
2192
+ expect(line_item.product).to eq(product)
2193
+ end
2194
+ end
2195
+ end
2196
+
2123
2197
  describe "#find_or_initialize_by" do
2124
2198
 
2125
2199
  let(:person) do
@@ -2560,6 +2560,136 @@ describe Mongoid::Relations::Referenced::Many do
2560
2560
  end
2561
2561
  end
2562
2562
 
2563
+ describe "#find_or_create_by!" do
2564
+
2565
+ context "when the relation is not polymorphic" do
2566
+
2567
+ let(:person) do
2568
+ Person.create
2569
+ end
2570
+
2571
+ let!(:post) do
2572
+ person.posts.create(title: "Testing")
2573
+ end
2574
+
2575
+ context "when the document exists" do
2576
+
2577
+ let(:found) do
2578
+ person.posts.find_or_create_by!(title: "Testing")
2579
+ end
2580
+
2581
+ it "returns the document" do
2582
+ expect(found).to eq(post)
2583
+ end
2584
+
2585
+ it "keeps the document in the relation" do
2586
+ expect(found.person).to eq(person)
2587
+ end
2588
+ end
2589
+
2590
+ context "when the document does not exist" do
2591
+
2592
+ context "when there is no criteria attached" do
2593
+
2594
+ let(:found) do
2595
+ person.posts.find_or_create_by!(title: "Test") do |post|
2596
+ post.content = "The Content"
2597
+ end
2598
+ end
2599
+
2600
+ it "sets the new document attributes" do
2601
+ expect(found.title).to eq("Test")
2602
+ end
2603
+
2604
+ it "returns a newly persisted document" do
2605
+ expect(found).to be_persisted
2606
+ end
2607
+
2608
+ it "calls the passed block" do
2609
+ expect(found.content).to eq("The Content")
2610
+ end
2611
+
2612
+ it "keeps the document in the relation" do
2613
+ expect(found.person).to eq(person)
2614
+ end
2615
+ end
2616
+
2617
+ context "when a criteria is attached" do
2618
+
2619
+ let(:found) do
2620
+ person.posts.recent.find_or_create_by!(title: "Test")
2621
+ end
2622
+
2623
+ it "sets the new document attributes" do
2624
+ expect(found.title).to eq("Test")
2625
+ end
2626
+
2627
+ it "returns a newly persisted document" do
2628
+ expect(found).to be_persisted
2629
+ end
2630
+
2631
+ it "keeps the document in the relation" do
2632
+ expect(found.person).to eq(person)
2633
+ end
2634
+ end
2635
+ end
2636
+ end
2637
+
2638
+ context "when the relation is polymorphic" do
2639
+
2640
+ let(:movie) do
2641
+ Movie.create
2642
+ end
2643
+
2644
+ let!(:rating) do
2645
+ movie.ratings.create(value: 1)
2646
+ end
2647
+
2648
+ context "when the document exists" do
2649
+
2650
+ let(:found) do
2651
+ movie.ratings.find_or_create_by!(value: 1)
2652
+ end
2653
+
2654
+ it "returns the document" do
2655
+ expect(found).to eq(rating)
2656
+ end
2657
+
2658
+ it "keeps the document in the relation" do
2659
+ expect(found.ratable).to eq(movie)
2660
+ end
2661
+ end
2662
+
2663
+ context "when the document does not exist" do
2664
+
2665
+ let(:found) do
2666
+ movie.ratings.find_or_create_by!(value: 3)
2667
+ end
2668
+
2669
+ it "sets the new document attributes" do
2670
+ expect(found.value).to eq(3)
2671
+ end
2672
+
2673
+ it "returns a newly persisted document" do
2674
+ expect(found).to be_persisted
2675
+ end
2676
+
2677
+ it "keeps the document in the relation" do
2678
+ expect(found.ratable).to eq(movie)
2679
+ end
2680
+
2681
+ context "when validation fails" do
2682
+
2683
+ it "raises an error" do
2684
+ expect {
2685
+ movie.comments.find_or_create_by!(title: "")
2686
+ }.to raise_error(Mongoid::Errors::Validations)
2687
+ end
2688
+ end
2689
+ end
2690
+ end
2691
+ end
2692
+
2563
2693
  describe "#find_or_initialize_by" do
2564
2694
 
2565
2695
  context "when the relation is not polymorphic" do
@@ -3555,4 +3685,27 @@ describe Mongoid::Relations::Referenced::Many do
3555
3685
  expect(person.posts.open).to eq([ post ])
3556
3686
  end
3557
3687
  end
3688
+
3689
+ context "when accessing a relation named parent" do
3690
+ let!(:parent) do
3691
+ Odd.create(name: "odd parent")
3692
+ end
3693
+
3694
+ let(:child) do
3695
+ Even.create(parent_id: parent.id, name: "original even child")
3696
+ end
3697
+
3698
+ it "updates the child after accessing the parent" do
3699
+ # Access parent relation on the child to make sure it is loaded
3700
+ child.parent
3701
+
3702
+ new_child_name = "updated even child"
3703
+
3704
+ child.name = new_child_name
3705
+ child.save!
3706
+
3707
+ reloaded = Even.find(child.id)
3708
+ expect(reloaded.name).to eq(new_child_name)
3709
+ end
3710
+ end
3558
3711
  end
@@ -2466,6 +2466,55 @@ describe Mongoid::Relations::Referenced::ManyToMany do
2466
2466
  end
2467
2467
  end
2468
2468
 
2469
+ describe "#find_or_create_by!" do
2470
+
2471
+ context "when the relation is not polymorphic" do
2472
+
2473
+ let(:person) do
2474
+ Person.create
2475
+ end
2476
+
2477
+ let!(:preference) do
2478
+ person.preferences.create(name: "Testing")
2479
+ end
2480
+
2481
+ context "when the document exists" do
2482
+
2483
+ let(:found) do
2484
+ person.preferences.find_or_create_by!(name: "Testing")
2485
+ end
2486
+
2487
+ it "returns the document" do
2488
+ expect(found).to eq(preference)
2489
+ end
2490
+ end
2491
+
2492
+ context "when the document does not exist" do
2493
+
2494
+ let(:found) do
2495
+ person.preferences.find_or_create_by!(name: "Test")
2496
+ end
2497
+
2498
+ it "sets the new document attributes" do
2499
+ expect(found.name).to eq("Test")
2500
+ end
2501
+
2502
+ it "returns a newly persisted document" do
2503
+ expect(found).to be_persisted
2504
+ end
2505
+
2506
+ context "when validation fails" do
2507
+
2508
+ it "raises an error" do
2509
+ expect {
2510
+ person.preferences.find_or_create_by!(name: "A")
2511
+ }.to raise_error(Mongoid::Errors::Validations)
2512
+ end
2513
+ end
2514
+ end
2515
+ end
2516
+ end
2517
+
2469
2518
  describe "#find_or_initialize_by" do
2470
2519
 
2471
2520
  context "when the relation is not polymorphic" do
@@ -261,5 +261,28 @@ describe Mongoid::Reloadable do
261
261
  end
262
262
  end
263
263
  end
264
+
265
+ context "when overriding #id alias" do
266
+
267
+ let!(:object) do
268
+ IdKey.create(key: 'foo')
269
+ end
270
+
271
+ let!(:from_db) do
272
+ IdKey.find(object._id).tap do |object|
273
+ object.key = 'bar'
274
+ object.save
275
+ end
276
+ end
277
+
278
+ it "reloads the object attributes from the db" do
279
+ object.reload
280
+ expect(object.key).to eq('bar')
281
+ end
282
+
283
+ it "reload should return self" do
284
+ expect(object.reload).to eq(from_db)
285
+ end
286
+ end
264
287
  end
265
288
  end
@@ -65,7 +65,8 @@ describe Mongoid::Sessions::Options do
65
65
  end
66
66
 
67
67
  it "passes down the options to collection" do
68
- expect_any_instance_of(Moped::Session).to receive(:with).with(options).and_return({})
68
+ session = Band.mongo_session
69
+ expect_any_instance_of(Moped::Session).to receive(:with).with(options).and_return(session)
69
70
  instance.collection
70
71
  end
71
72
  end
@@ -409,6 +409,36 @@ describe Mongoid::Sessions do
409
409
  end
410
410
  end
411
411
 
412
+ context "when overridden the database with store_in" do
413
+
414
+ before do
415
+ Band.store_in(database: database_id_alt)
416
+ end
417
+
418
+ context "on instance level" do
419
+
420
+ let(:band) do
421
+ Band.new.with({:read=>:primary})
422
+ end
423
+
424
+ it "uses the new database" do
425
+ expect(band.mongo_session.send(:current_database).name).to eq database_id_alt
426
+ end
427
+
428
+ context "when using another database before" do
429
+
430
+ before do
431
+ band
432
+ User.create!
433
+ end
434
+
435
+ it "uses the new database" do
436
+ expect(band.mongo_session.send(:current_database).name).to eq database_id_alt
437
+ end
438
+ end
439
+ end
440
+ end
441
+
412
442
  context "when overriding to a monghq single server", config: :mongohq do
413
443
 
414
444
  shared_examples_for "an overridden session to a mongohq single server" do
data/spec/spec_helper.rb CHANGED
@@ -93,6 +93,8 @@ ActiveSupport::Inflector.inflections do |inflect|
93
93
  inflect.singular("address_components", "address_component")
94
94
  end
95
95
 
96
+ I18n.config.enforce_available_locales = false
97
+
96
98
  RSpec.configure do |config|
97
99
  config.include Mongoid::SpecHelpers
98
100
  config.raise_errors_for_deprecations!
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 4.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Durran Jordan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-23 00:00:00.000000000 Z
11
+ date: 2015-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -115,6 +115,7 @@ files:
115
115
  - lib/mongoid/contextual/text_search.rb
116
116
  - lib/mongoid/copyable.rb
117
117
  - lib/mongoid/criteria.rb
118
+ - lib/mongoid/criteria/#findable.rb#
118
119
  - lib/mongoid/criteria/findable.rb
119
120
  - lib/mongoid/criteria/inspectable.rb
120
121
  - lib/mongoid/criteria/marshalable.rb
@@ -389,6 +390,7 @@ files:
389
390
  - spec/app/models/circus.rb
390
391
  - spec/app/models/code.rb
391
392
  - spec/app/models/comment.rb
393
+ - spec/app/models/contextable_item.rb
392
394
  - spec/app/models/contractor.rb
393
395
  - spec/app/models/cookie.rb
394
396
  - spec/app/models/country_code.rb
@@ -429,6 +431,7 @@ files:
429
431
  - spec/app/models/home.rb
430
432
  - spec/app/models/house.rb
431
433
  - spec/app/models/html_writer.rb
434
+ - spec/app/models/id_key.rb
432
435
  - spec/app/models/image.rb
433
436
  - spec/app/models/implant.rb
434
437
  - spec/app/models/item.rb
@@ -537,6 +540,7 @@ files:
537
540
  - spec/app/models/writer.rb
538
541
  - spec/config/mongoid.yml
539
542
  - spec/helpers.rb
543
+ - spec/mongoid/#atomic_spec.rb#
540
544
  - spec/mongoid/atomic/modifiers_spec.rb
541
545
  - spec/mongoid/atomic/paths/embedded/many_spec.rb
542
546
  - spec/mongoid/atomic/paths/embedded/one_spec.rb
@@ -780,7 +784,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
780
784
  version: 1.3.6
781
785
  requirements: []
782
786
  rubyforge_project: mongoid
783
- rubygems_version: 2.2.2
787
+ rubygems_version: 2.4.5
784
788
  signing_key:
785
789
  specification_version: 4
786
790
  summary: Elegant Persistance in Ruby for MongoDB.
@@ -831,6 +835,7 @@ test_files:
831
835
  - spec/app/models/circus.rb
832
836
  - spec/app/models/code.rb
833
837
  - spec/app/models/comment.rb
838
+ - spec/app/models/contextable_item.rb
834
839
  - spec/app/models/contractor.rb
835
840
  - spec/app/models/cookie.rb
836
841
  - spec/app/models/country_code.rb
@@ -871,6 +876,7 @@ test_files:
871
876
  - spec/app/models/home.rb
872
877
  - spec/app/models/house.rb
873
878
  - spec/app/models/html_writer.rb
879
+ - spec/app/models/id_key.rb
874
880
  - spec/app/models/image.rb
875
881
  - spec/app/models/implant.rb
876
882
  - spec/app/models/item.rb
@@ -979,6 +985,7 @@ test_files:
979
985
  - spec/app/models/writer.rb
980
986
  - spec/config/mongoid.yml
981
987
  - spec/helpers.rb
988
+ - spec/mongoid/#atomic_spec.rb#
982
989
  - spec/mongoid/atomic/modifiers_spec.rb
983
990
  - spec/mongoid/atomic/paths/embedded/many_spec.rb
984
991
  - spec/mongoid/atomic/paths/embedded/one_spec.rb