Skip to content

Commit

Permalink
Fix updating the public_on date on persisted pages
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mamhoff committed Apr 21, 2022
1 parent f83cbd6 commit dcb6a05
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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? }
Expand Down
11 changes: 10 additions & 1 deletion spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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 }
Expand Down

0 comments on commit dcb6a05

Please sign in to comment.