|
588 | 588 | expect(person.age_before_type_cast).to eq("old")
|
589 | 589 | end
|
590 | 590 | end
|
| 591 | + |
| 592 | + context "when reloading" do |
| 593 | + |
| 594 | + let(:product) do |
| 595 | + Product.create!(price: '1') |
| 596 | + end |
| 597 | + |
| 598 | + before do |
| 599 | + product.reload |
| 600 | + end |
| 601 | + |
| 602 | + it "resets the attributes_before_type_cast to the attributes hash" do |
| 603 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 604 | + end |
| 605 | + |
| 606 | + it "the *_before_type_cast method returns the demongoized value" do |
| 607 | + expect(product.price_before_type_cast).to eq(1) |
| 608 | + end |
| 609 | + end |
| 610 | + |
| 611 | + context "when reloading and writing a demongoizable value" do |
| 612 | + |
| 613 | + let(:product) do |
| 614 | + Product.create!.tap do |product| |
| 615 | + Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' }}) |
| 616 | + end |
| 617 | + end |
| 618 | + |
| 619 | + before do |
| 620 | + product.reload |
| 621 | + end |
| 622 | + |
| 623 | + it "resets the attributes_before_type_cast to the attributes hash" do |
| 624 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 625 | + end |
| 626 | + |
| 627 | + it "the *_before_type_cast method returns the mongoized value" do |
| 628 | + expect(product.price_before_type_cast).to eq('1') |
| 629 | + end |
| 630 | + end |
| 631 | + |
| 632 | + context "when reading from the db" do |
| 633 | + |
| 634 | + let(:product) do |
| 635 | + Product.create!(price: '1') |
| 636 | + end |
| 637 | + |
| 638 | + let(:from_db) do |
| 639 | + Product.find(product.id) |
| 640 | + end |
| 641 | + |
| 642 | + it "resets the attributes_before_type_cast to the attributes hash" do |
| 643 | + expect(from_db.attributes_before_type_cast).to eq(from_db.attributes) |
| 644 | + end |
| 645 | + |
| 646 | + it "the *_before_type_cast method returns the demongoized value" do |
| 647 | + expect(from_db.price_before_type_cast).to eq(1) |
| 648 | + end |
| 649 | + end |
| 650 | + |
| 651 | + context "when reading from the db after writing a demongoizable value" do |
| 652 | + |
| 653 | + let(:product) do |
| 654 | + Product.create!.tap do |product| |
| 655 | + Product.collection.update_one({ _id: product.id }, { :$set => { price: '1' }}) |
| 656 | + end |
| 657 | + end |
| 658 | + |
| 659 | + let(:from_db) do |
| 660 | + Product.find(product.id) |
| 661 | + end |
| 662 | + |
| 663 | + it "resets the attributes_before_type_cast to the attributes hash" do |
| 664 | + expect(from_db.attributes_before_type_cast).to eq(from_db.attributes) |
| 665 | + end |
| 666 | + |
| 667 | + it "the *_before_type_cast method returns the mongoized value" do |
| 668 | + expect(from_db.price_before_type_cast).to eq('1') |
| 669 | + end |
| 670 | + end |
| 671 | + |
| 672 | + context "when making a new model" do |
| 673 | + |
| 674 | + context "when using new with no options" do |
| 675 | + let(:product) { Product.new } |
| 676 | + |
| 677 | + it "sets the attributes_before_type_cast to the attributes hash" do |
| 678 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 679 | + end |
| 680 | + end |
| 681 | + |
| 682 | + context "when using new with options" do |
| 683 | + let(:product) { Product.new(price: '1') } |
| 684 | + |
| 685 | + let(:abtc) do |
| 686 | + product.attributes.merge('price' => '1') |
| 687 | + end |
| 688 | + |
| 689 | + it "has the attributes before type cast" do |
| 690 | + expect(product.attributes_before_type_cast).to eq(abtc) |
| 691 | + end |
| 692 | + end |
| 693 | + |
| 694 | + context "when persisting the model" do |
| 695 | + let(:product) { Product.new(price: '1') } |
| 696 | + |
| 697 | + let(:abtc) do |
| 698 | + product.attributes.merge('price' => '1') |
| 699 | + end |
| 700 | + |
| 701 | + before do |
| 702 | + expect(product.attributes_before_type_cast).to eq(abtc) |
| 703 | + product.save! |
| 704 | + end |
| 705 | + |
| 706 | + it "resets the attributes_before_type_cast to the attributes" do |
| 707 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 708 | + end |
| 709 | + end |
| 710 | + |
| 711 | + context "when using create! without options" do |
| 712 | + let(:product) { Product.create! } |
| 713 | + |
| 714 | + it "resets the attributes_before_type_cast to the attributes" do |
| 715 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 716 | + end |
| 717 | + end |
| 718 | + |
| 719 | + context "when using create! with options" do |
| 720 | + let(:product) { Product.create!(price: '1') } |
| 721 | + |
| 722 | + it "resets the attributes_before_type_cast to the attributes" do |
| 723 | + expect(product.attributes_before_type_cast).to eq(product.attributes) |
| 724 | + end |
| 725 | + end |
| 726 | + end |
591 | 727 | end
|
592 | 728 |
|
593 | 729 | describe "#setter=" do
|
|
741 | 877 | end
|
742 | 878 | end
|
743 | 879 | end
|
| 880 | + |
| 881 | + context "when the field needs to be mongoized" do |
| 882 | + |
| 883 | + before do |
| 884 | + product.price = "1" |
| 885 | + product.save! |
| 886 | + end |
| 887 | + |
| 888 | + it "mongoizes the value" do |
| 889 | + expect(product.price).to eq(1) |
| 890 | + end |
| 891 | + |
| 892 | + it "stores the value in the mongoized form" do |
| 893 | + expect(product.attributes_before_type_cast["price"]).to eq(1) |
| 894 | + end |
| 895 | + end |
744 | 896 | end
|
745 | 897 |
|
746 | 898 | describe "#defaults" do
|
|
0 commit comments