From dcb6a0565b74594b0e314e0b5b54e30e48a5d928 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Thu, 21 Apr 2022 14:33:42 +0200 Subject: [PATCH] Fix updating the public_on date on persisted pages When updating a page's `public_on` date, we need to save the associated public page as well. This commit accomplishes that. Prior to this commit, we would set the `public_on` date on the public version, but not persist it. --- app/models/alchemy/page.rb | 2 +- spec/models/alchemy/page_spec.rb | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/models/alchemy/page.rb b/app/models/alchemy/page.rb index f16f253248..d7bfd7f67f 100644 --- a/app/models/alchemy/page.rb +++ b/app/models/alchemy/page.rb @@ -117,7 +117,7 @@ class Page < BaseRecord has_many :nodes, class_name: "Alchemy::Node", inverse_of: :page has_many :versions, class_name: "Alchemy::PageVersion", inverse_of: :page, dependent: :destroy has_one :draft_version, -> { drafts }, class_name: "Alchemy::PageVersion" - has_one :public_version, -> { published }, class_name: "Alchemy::PageVersion" + has_one :public_version, -> { published }, class_name: "Alchemy::PageVersion", autosave: -> { persisted? } before_validation :set_language, if: -> { language.nil? } diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index a9af4cc3a1..2b0159120c 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -1397,7 +1397,7 @@ def copy_children_to(new_parent) end describe "#public_on=" do - let(:time) { Time.now } + let(:time) { 1.hour.ago } subject { page.public_on = time } @@ -1409,6 +1409,15 @@ def copy_children_to(new_parent) expect(page.public_version.public_on).to be_within(1.second).of(time) end + context "when the page is persisted" do + let(:page) { create(:alchemy_page, :public) } + + it "sets public_on on the public version" do + page.update(public_on: time) + expect(page.reload.public_version.public_on).to be_within(1.second).of(time) + end + end + context "and the time is nil" do let(:page) { build(:alchemy_page, :public) } let(:time) { nil }