Skip to content

Commit a5657dc

Browse files
5508 touch on custom field (#5829)
* [MONGOID-5508] Added a class for Label based on ticket example * [MONGOID-5508] Added a Band class based on issue example * [MONGOID-5508] Added class_name to Band,Label and included timestamps for managing updated_at * [MONGOID-5508] Added spec to test field is updated along with timestamps for parent and child * [MONGOID-5508] Added test for codepaths of save and destroy and fixed utc conversion. Off by 2 miliseconds * [MONGOID-5508] It passes tests with save, destroy, and touch codepaths too
1 parent 69485bf commit a5657dc

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

spec/mongoid/touchable_spec.rb

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,81 @@
733733
end
734734
end
735735

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+
736811
context 'multi-level' do
737812

738813
let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }

spec/mongoid/touchable_spec_models.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,21 @@ class Sofa
150150

151151
embedded_in :floor, touch: true, class_name: "TouchableSpec::Referenced::Floor"
152152
end
153+
154+
class Label
155+
include Mongoid::Document
156+
include Mongoid::Timestamps
157+
158+
field :bands_updated_at, type: DateTime
159+
has_many :bands, class_name: "TouchableSpec::Referenced::Band"
160+
end
161+
162+
class Band
163+
include Mongoid::Document
164+
include Mongoid::Timestamps
165+
166+
belongs_to :label, touch: :bands_updated_at, class_name: "TouchableSpec::Referenced::Label"
167+
end
168+
153169
end
154170
end

0 commit comments

Comments
 (0)