Skip to content

Commit

Permalink
5508 touch on custom field (#5829)
Browse files Browse the repository at this point in the history
* [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
  • Loading branch information
adviti-mishra committed Jun 11, 2024
1 parent 69485bf commit a5657dc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
75 changes: 75 additions & 0 deletions spec/mongoid/touchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,81 @@
end
end

context "when a custom field is specified" do

shared_examples "updates the child's updated_at" do

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

let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) }

after do
Timecop.return
end

let!(:label) do
TouchableSpec::Referenced::Label.create!
end

let(:band) do
TouchableSpec::Referenced::Band.create!(label: label)
end

before do
update_time
band.send(meth)
end

it "updates the child's timestamp" do
expect(band.updated_at).to eq(update_time)
expect(band.reload.updated_at).to eq(update_time)
end
end

shared_examples "updates the parent's custom field and updated_at" do

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

let(:update_time) { Timecop.freeze(Time.at(Time.now.to_i) + 2) }

after do
Timecop.return
end

let!(:label) do
TouchableSpec::Referenced::Label.create!
end

let!(:band) do
TouchableSpec::Referenced::Band.create!(label: label)
end

before do
update_time
band.send(meth)
end

it "updates the parent's custom field" do
expect(label.bands_updated_at).to eq(update_time)
expect(label.reload.bands_updated_at).to eq(update_time)
end

it "updates the parent's timestamp" do
expect(label.updated_at).to eq(update_time)
expect(label.reload.updated_at).to eq(update_time)
end

end

[:save, :destroy, :touch].each do |meth|
context "with #{meth} on referenced associations" do
let(:meth) { meth }
include_examples "updates the child's updated_at" unless meth == :destroy
include_examples "updates the parent's custom field and updated_at"
end
end
end

context 'multi-level' do

let!(:start_time) { Timecop.freeze(Time.at(Time.now.to_i)) }
Expand Down
16 changes: 16 additions & 0 deletions spec/mongoid/touchable_spec_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,21 @@ class Sofa

embedded_in :floor, touch: true, class_name: "TouchableSpec::Referenced::Floor"
end

class Label
include Mongoid::Document
include Mongoid::Timestamps

field :bands_updated_at, type: DateTime
has_many :bands, class_name: "TouchableSpec::Referenced::Band"
end

class Band
include Mongoid::Document
include Mongoid::Timestamps

belongs_to :label, touch: :bands_updated_at, class_name: "TouchableSpec::Referenced::Label"
end

end
end

0 comments on commit a5657dc

Please sign in to comment.