Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 0 additions & 26 deletions app/models/alchemy/page_version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,6 @@ class << self

before_destroy :delete_elements

# Determines if this version is public
#
# Takes the two timestamps +public_on+ and +public_until+
# and returns true if the time given (+Time.current+ per default)
# is in this timespan.
#
# @param time [DateTime] (Time.current)
# @returns Boolean
def public?(time = Current.preview_time)
already_public_for?(time) && still_public_for?(time)
end

# Determines if this version is already public for given time
# @param time [DateTime] (Current.preview_time)
# @returns Boolean
def already_public_for?(time = Current.preview_time)
!public_on.nil? && public_on <= time
end

# Determines if this version is still public for given time
# @param time [DateTime] (Current.preview_time)
# @returns Boolean
def still_public_for?(time = Current.preview_time)
public_until.nil? || public_until >= time
end

def element_repository
ElementsRepository.new(elements)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/alchemy/publishable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def scheduled?(at: Current.preview_time)
#
# @returns Boolean
def publishable?
!public_on.nil? && still_public_for?
!public_on.nil? && still_public_for?(at: Time.current)
end

# Determines if this record is already public for given time
Expand Down
37 changes: 37 additions & 0 deletions lib/alchemy/test_support/shared_publishable_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,41 @@
end
end
end

describe "#publishable?" do
context "when public_on is nil" do
let(:page_version) { build(factory_name, public_on: nil) }

it { expect(page_version.publishable?).to be(false) }
end

context "when public_on is set and public_until is nil" do
let(:page_version) { build(factory_name, public_on: Time.current) }

it { expect(page_version.publishable?).to be(true) }
end

context "when public_on is set and public_until is in the past" do
let(:page_version) do
build(factory_name,
public_on: Time.current - 2.days,
public_until: Time.current - 1.day)
end

it { expect(page_version.publishable?).to be(false) }
end

context "when Current.preview_time is set to a future time" do
let(:page_version) do
build(factory_name,
public_on: Time.current - 1.day,
public_until: Time.current + 1.day)
end

it "uses Time.current instead of the preview_time" do
Alchemy::Current.preview_time = Time.current + 1.week
expect(page_version.publishable?).to be(true)
end
end
end
end
Loading