mongoid 4.0.1 → 4.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -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
@@ -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
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.1
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: 2015-01-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
@@ -390,6 +390,7 @@ files:
390
390
  - spec/app/models/circus.rb
391
391
  - spec/app/models/code.rb
392
392
  - spec/app/models/comment.rb
393
+ - spec/app/models/contextable_item.rb
393
394
  - spec/app/models/contractor.rb
394
395
  - spec/app/models/cookie.rb
395
396
  - spec/app/models/country_code.rb
@@ -783,7 +784,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
783
784
  version: 1.3.6
784
785
  requirements: []
785
786
  rubyforge_project: mongoid
786
- rubygems_version: 2.2.2
787
+ rubygems_version: 2.4.5
787
788
  signing_key:
788
789
  specification_version: 4
789
790
  summary: Elegant Persistance in Ruby for MongoDB.
@@ -834,6 +835,7 @@ test_files:
834
835
  - spec/app/models/circus.rb
835
836
  - spec/app/models/code.rb
836
837
  - spec/app/models/comment.rb
838
+ - spec/app/models/contextable_item.rb
837
839
  - spec/app/models/contractor.rb
838
840
  - spec/app/models/cookie.rb
839
841
  - spec/app/models/country_code.rb