Touch the associated record when assigning targets

So the cache will expire properly.
This commit is contained in:
Javi Martín
2021-01-22 17:54:17 +01:00
parent 6470ff85be
commit 101625ebab
2 changed files with 11 additions and 1 deletions

View File

@@ -1,6 +1,6 @@
class SDG::Relation < ApplicationRecord
validates :related_sdg_id, uniqueness: { scope: [:related_sdg_type, :relatable_id, :relatable_type] }
belongs_to :relatable, polymorphic: true, optional: false
belongs_to :relatable, polymorphic: true, optional: false, touch: true
belongs_to :related_sdg, polymorphic: true, optional: false
end

View File

@@ -159,6 +159,16 @@ describe SDG::Relatable do
expect(relatable.reload.sdg_goals).to match_array [SDG::Goal[1], SDG::Goal[3], SDG::Goal[4]]
expect(relatable.reload.sdg_targets).to match_array [SDG::Target[1.1], SDG::Target[4.1]]
end
it "touches the associated record" do
relatable.sdg_related_list = "1.1, 2.1, 2.2"
travel(10.seconds) do
relatable.sdg_related_list = "1.1, 2.1, 2.2, 3.1"
expect(relatable.updated_at).to eq Time.current
end
end
end
describe "#sdg_review" do