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 }