|
476 | 476 | end
|
477 | 477 | end
|
478 | 478 | end
|
| 479 | + |
| 480 | + context "when including key word args" do |
| 481 | + |
| 482 | + let(:person) { Person.new } |
| 483 | + |
| 484 | + context "when only including from" do |
| 485 | + |
| 486 | + context "when the object has not changed" do |
| 487 | + |
| 488 | + it "returns false" do |
| 489 | + expect(person.send(:attribute_changed?, :score, from: nil)).to be false |
| 490 | + end |
| 491 | + |
| 492 | + it "returns false using (attribute)_changed?" do |
| 493 | + expect(person.score_changed?(from: nil)).to be false |
| 494 | + end |
| 495 | + end |
| 496 | + |
| 497 | + context "when the object has changed from the wrong item" do |
| 498 | + |
| 499 | + before do |
| 500 | + person.score = 2 |
| 501 | + end |
| 502 | + |
| 503 | + it "returns false" do |
| 504 | + expect(person.send(:attribute_changed?, :score, from: 1)).to be false |
| 505 | + end |
| 506 | + |
| 507 | + it "returns false using (attribute)_changed?" do |
| 508 | + expect(person.score_changed?(from: 1)).to be false |
| 509 | + end |
| 510 | + end |
| 511 | + |
| 512 | + context "when the object has changed from the correct item" do |
| 513 | + |
| 514 | + before do |
| 515 | + person.score = 2 |
| 516 | + end |
| 517 | + |
| 518 | + it "returns true" do |
| 519 | + expect(person.send(:attribute_changed?, :score, from: nil)).to be true |
| 520 | + end |
| 521 | + |
| 522 | + it "returns true using (attribute)_changed?" do |
| 523 | + expect(person.score_changed?(from: nil)).to be true |
| 524 | + end |
| 525 | + end |
| 526 | + end |
| 527 | + |
| 528 | + context "when only including to" do |
| 529 | + |
| 530 | + context "when the object has not changed" do |
| 531 | + |
| 532 | + it "returns false" do |
| 533 | + expect(person.send(:attribute_changed?, :score, to: nil)).to be false |
| 534 | + end |
| 535 | + |
| 536 | + it "returns false using (attribute)_changed?" do |
| 537 | + expect(person.score_changed?(to: nil)).to be false |
| 538 | + end |
| 539 | + end |
| 540 | + |
| 541 | + context "when the object has changed to the wrong item" do |
| 542 | + |
| 543 | + before do |
| 544 | + person.score = 2 |
| 545 | + end |
| 546 | + |
| 547 | + it "returns false" do |
| 548 | + expect(person.send(:attribute_changed?, :score, to: 1)).to be false |
| 549 | + end |
| 550 | + |
| 551 | + it "returns false using (attribute)_changed?" do |
| 552 | + expect(person.score_changed?(to: 1)).to be false |
| 553 | + end |
| 554 | + end |
| 555 | + |
| 556 | + context "when the object has changed to the correct item" do |
| 557 | + |
| 558 | + before do |
| 559 | + person.score = 2 |
| 560 | + end |
| 561 | + |
| 562 | + it "returns true" do |
| 563 | + expect(person.send(:attribute_changed?, :score, to: 2)).to be true |
| 564 | + end |
| 565 | + |
| 566 | + it "returns true using (attribute)_changed?" do |
| 567 | + expect(person.score_changed?(to: 2)).to be true |
| 568 | + end |
| 569 | + end |
| 570 | + end |
| 571 | + |
| 572 | + context "when including from and to" do |
| 573 | + |
| 574 | + context "when the object has not changed" do |
| 575 | + |
| 576 | + it "returns false" do |
| 577 | + expect(person.send(:attribute_changed?, :score, from: nil, to: nil)).to be false |
| 578 | + end |
| 579 | + |
| 580 | + it "returns false using (attribute)_changed?" do |
| 581 | + expect(person.score_changed?(from: nil, to: nil)).to be false |
| 582 | + end |
| 583 | + end |
| 584 | + |
| 585 | + context "when only the from is correct" do |
| 586 | + |
| 587 | + before do |
| 588 | + person.score = 2 |
| 589 | + end |
| 590 | + |
| 591 | + it "returns false" do |
| 592 | + expect(person.send(:attribute_changed?, :score, from: nil, to: 3)).to be false |
| 593 | + end |
| 594 | + |
| 595 | + it "returns false using (attribute)_changed?" do |
| 596 | + expect(person.score_changed?(from: nil, to: 3)).to be false |
| 597 | + end |
| 598 | + end |
| 599 | + |
| 600 | + context "when only the to is correct" do |
| 601 | + |
| 602 | + before do |
| 603 | + person.score = 2 |
| 604 | + end |
| 605 | + |
| 606 | + it "returns false" do |
| 607 | + expect(person.send(:attribute_changed?, :score, from: 1, to: 2)).to be false |
| 608 | + end |
| 609 | + |
| 610 | + it "returns false using (attribute)_changed?" do |
| 611 | + expect(person.score_changed?(from: 1, to: 2)).to be false |
| 612 | + end |
| 613 | + end |
| 614 | + |
| 615 | + context "when the from and to are correct" do |
| 616 | + |
| 617 | + before do |
| 618 | + person.score = 2 |
| 619 | + end |
| 620 | + |
| 621 | + it "returns true" do |
| 622 | + expect(person.send(:attribute_changed?, :score, from: nil, to: 2)).to be true |
| 623 | + end |
| 624 | + |
| 625 | + it "returns true using (attribute)_changed?" do |
| 626 | + expect(person.score_changed?(from: nil, to: 2)).to be true |
| 627 | + end |
| 628 | + end |
| 629 | + |
| 630 | + context "when value is mongoized" do |
| 631 | + |
| 632 | + before do |
| 633 | + person.score = "2" |
| 634 | + end |
| 635 | + |
| 636 | + it "returns true with mongoized value" do |
| 637 | + expect(person.send(:attribute_changed?, :score, from: nil, to: 2)).to be true |
| 638 | + end |
| 639 | + |
| 640 | + it "returns true with mongoized value using (attribute)_changed?" do |
| 641 | + expect(person.score_changed?(from: nil, to: 2)).to be true |
| 642 | + end |
| 643 | + end |
| 644 | + |
| 645 | + context "when value is mongoized" do |
| 646 | + |
| 647 | + before do |
| 648 | + person.score = "2" |
| 649 | + end |
| 650 | + |
| 651 | + it "returns false with unmongoized value" do |
| 652 | + expect(person.send(:attribute_changed?, :score, from: nil, to: "2")).to be false |
| 653 | + end |
| 654 | + |
| 655 | + it "returns false with unmongoized value using (attribute)_changed?" do |
| 656 | + expect(person.score_changed?(from: nil, to: "2")).to be false |
| 657 | + end |
| 658 | + end |
| 659 | + end |
| 660 | + end |
479 | 661 | end
|
480 | 662 |
|
481 | 663 | describe "#attribute_changed_from_default?" do
|
|
0 commit comments