Skip to content

Commit

Permalink
Merge pull request #2050 from tvdeyen/remove-public-version-on-unpublish
Browse files Browse the repository at this point in the history
Destroy public version if public checkbox is unset
  • Loading branch information
tvdeyen authored Mar 23, 2021
2 parents 94190bb + bb27d61 commit 72b5134
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion app/models/alchemy/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,14 @@ def publish!(current_time = Time.current)
# Sets the public_on date on the published version
#
# Builds a new version if none exists yet.
# Destroys public version if empty time is set
#
def public_on=(time)
if public_version
if public_version && time.blank?
public_version.destroy!
# Need to reset the public version on the instance so we do not need to reload
self.public_version = nil
elsif public_version
public_version.public_on = time
else
versions.build(public_on: time)
Expand Down
47 changes: 47 additions & 0 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,53 @@ class AnotherUrlPathClass; end
end
end

describe "#public_on=" do
let(:time) { Time.now }

subject { page.public_on = time }

context "when there is a public version" do
let(:page) { build(:alchemy_page, :public) }

it "sets public_on on the public version" do
subject
expect(page.public_version.public_on).to be_within(1.second).of(time)
end

context "and the time is nil" do
let(:page) { build(:alchemy_page, :public) }
let(:time) { nil }

it "destroys the public version" do
expect(page.public_version).to be
subject
expect(page.public_version).not_to be
end
end

context "and the time is empty string" do
let(:page) { build(:alchemy_page, :public) }
let(:time) { "" }

it "destroys the public version" do
expect(page.public_version).to be
subject
expect(page.public_version).not_to be
end
end
end

context "when there is no public version" do
let(:page) { build(:alchemy_page) }

it "builds a public version and sets public_on on it" do
subject
expect(page.versions.last).to be
expect(page.versions.last.public_on).to be_within(1.second).of(time)
end
end
end

describe "#public_on" do
subject(:public_on) { page.public_on }

Expand Down

0 comments on commit 72b5134

Please sign in to comment.