|
733 | 733 | end
|
734 | 734 | end
|
735 | 735 |
|
| 736 | + context "when a custom field is specified" do |
| 737 | + |
| 738 | + shared_examples "updates the child's updated_at" do |
| 739 | + |
| 740 | + let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } |
| 741 | + |
| 742 | + let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } |
| 743 | + |
| 744 | + after do |
| 745 | + Timecop.return |
| 746 | + end |
| 747 | + |
| 748 | + let!(:label) do |
| 749 | + TouchableSpec::Referenced::Label.create! |
| 750 | + end |
| 751 | + |
| 752 | + let(:band) do |
| 753 | + TouchableSpec::Referenced::Band.create!(label: label) |
| 754 | + end |
| 755 | + |
| 756 | + before do |
| 757 | + update_time |
| 758 | + band.send(meth) |
| 759 | + end |
| 760 | + |
| 761 | + it "updates the child's timestamp" do |
| 762 | + expect(band.updated_at).to eq(update_time) |
| 763 | + expect(band.reload.updated_at).to eq(update_time) |
| 764 | + end |
| 765 | + end |
| 766 | + |
| 767 | + shared_examples "updates the parent's custom field and updated_at" do |
| 768 | + |
| 769 | + let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) } |
| 770 | + |
| 771 | + let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) } |
| 772 | + |
| 773 | + after do |
| 774 | + Timecop.return |
| 775 | + end |
| 776 | + |
| 777 | + let!(:label) do |
| 778 | + TouchableSpec::Referenced::Label.create! |
| 779 | + end |
| 780 | + |
| 781 | + let!(:band) do |
| 782 | + TouchableSpec::Referenced::Band.create!(label: label) |
| 783 | + end |
| 784 | + |
| 785 | + before do |
| 786 | + update_time |
| 787 | + band.send(meth) |
| 788 | + end |
| 789 | + |
| 790 | + it "updates the parent's custom field" do |
| 791 | + expect(label.bands_updated_at).to eq(update_time) |
| 792 | + expect(label.reload.bands_updated_at).to eq(update_time) |
| 793 | + end |
| 794 | + |
| 795 | + it "updates the parent's timestamp" do |
| 796 | + expect(label.updated_at).to eq(update_time) |
| 797 | + expect(label.reload.updated_at).to eq(update_time) |
| 798 | + end |
| 799 | + |
| 800 | + end |
| 801 | + |
| 802 | + [:save, :destroy, :touch].each do |meth| |
| 803 | + context "with #{meth} on referenced associations" do |
| 804 | + let(:meth) { meth } |
| 805 | + include_examples "updates the child's updated_at" unless meth == :destroy |
| 806 | + include_examples "updates the parent's custom field and updated_at" |
| 807 | + end |
| 808 | + end |
| 809 | + end |
| 810 | + |
736 | 811 | context 'multi-level' do
|
737 | 812 |
|
738 | 813 | let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
|
|
0 commit comments