Skip to content

Commit e1a1583

Browse files
committed
reworked pinned links period scoped to fix some edge cases + added respective predicates
1 parent 3a2f881 commit e1a1583

3 files changed

Lines changed: 35 additions & 21 deletions

File tree

app/models/pinned_link.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@ class PinnedLink < ApplicationRecord
77
includes(:post, post: [:community])
88
}
99

10-
# a past link is one that ended in the past
1110
scope :past, lambda {
1211
timed.where('shown_before < ?', DateTime.now)
1312
}
1413

15-
# a current link is not timed or started in the past and ends in the future
1614
scope :current, lambda {
1715
where(shown_after: nil, shown_before: nil).or(
18-
timed.where('shown_after <= ?', DateTime.now)
19-
.where('shown_before > ?', DateTime.now)
16+
timed.where('shown_after is null or shown_after <= ?', DateTime.now)
17+
.where.not('shown_before < ?', DateTime.now)
2018
)
2119
}
2220

23-
# a future link is one that both starts and ends in the future
2421
scope :future, lambda {
2522
timed.where('shown_after > ?', DateTime.now)
26-
.where('shown_before > ?', DateTime.now)
23+
.where('shown_before is null or shown_before >= ?', DateTime.now)
2724
}
2825

2926
scope :timed, lambda {
@@ -34,6 +31,27 @@ class PinnedLink < ApplicationRecord
3431

3532
validate :check_post_or_url
3633

34+
# Is the link not timed or started in the past & hasn't ended yet?
35+
# @param now [DateTime, nil] timestamp to compare to
36+
# @return [Boolean] check result
37+
def current?(now = DateTime.now)
38+
!timed? || !(future?(now) || past?(now))
39+
end
40+
41+
# Does the link start in the future?
42+
# @param now [DateTime, nil] timestamp to compare to
43+
# @return [Boolean] check result
44+
def future?(now = DateTime.now)
45+
shown_after.present? && shown_after > now && (shown_before.nil? || shown_before >= now)
46+
end
47+
48+
# Has the link ended in the past?
49+
# @param now [DateTime, nil] timestamp to compare to
50+
# @return [Boolean] check result
51+
def past?(now = DateTime.now)
52+
shown_before.present? && shown_before < now
53+
end
54+
3755
# Is the link time-constrained?
3856
# @return [Boolean] check result
3957
def timed?

test/controllers/pinned_links_controller_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,31 @@ class PinnedLinksControllerTest < ActionController::TestCase
5252
links_ids = pinned_links.map(&:id)
5353
assert(@links.all? { |link| links_ids.include?(link.id) })
5454

55-
get :index, params: { period: 'past' }
55+
get :index, params: { period: 'current' }
5656
@links = assigns(:links)
57+
5758
assert_response(:success)
5859
assert @links.any?
59-
6060
@links.each do |link|
61-
assert link.timed? && link.shown_before < now
61+
assert link.current?(now)
6262
end
6363

64-
get :index, params: { period: 'current' }
64+
get :index, params: { period: 'past' }
6565
@links = assigns(:links)
66+
6667
assert_response(:success)
6768
assert @links.any?
68-
6969
@links.each do |link|
70-
assert !link.timed? || (link.shown_before > now && link.shown_after <= now)
70+
assert link.past?(now)
7171
end
7272

7373
get :index, params: { period: 'future' }
7474
@links = assigns(:links)
75+
7576
assert_response(:success)
7677
assert @links.any?
77-
7878
@links.each do |link|
79-
assert link.timed? && link.shown_before > now && link.shown_after > now
79+
assert link.future?(now)
8080
end
8181
end
8282

test/models/pinned_link_test.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class PinnedLinkTest < ActiveSupport::TestCase
55
timed = PinnedLink.timed
66

77
assert timed.any?
8-
98
timed.each do |link|
109
assert link.timed?, "link \"#{link.label}\" is not timed"
1110
end
@@ -16,9 +15,8 @@ class PinnedLinkTest < ActiveSupport::TestCase
1615
current = PinnedLink.current
1716

1817
assert current.any?
19-
2018
current.each do |link|
21-
assert !link.timed? || (link.shown_before > now && link.shown_after <= now)
19+
assert link.current?(now)
2220
end
2321
end
2422

@@ -27,9 +25,8 @@ class PinnedLinkTest < ActiveSupport::TestCase
2725
past = PinnedLink.past
2826

2927
assert past.any?
30-
3128
past.each do |link|
32-
assert link.timed? && link.shown_before < now
29+
assert link.past?(now)
3330
end
3431
end
3532

@@ -38,9 +35,8 @@ class PinnedLinkTest < ActiveSupport::TestCase
3835
future = PinnedLink.future
3936

4037
assert future.any?
41-
4238
future.each do |link|
43-
assert link.timed? && link.shown_before > now && link.shown_after > now
39+
assert link.future?(now)
4440
end
4541
end
4642
end

0 commit comments

Comments
 (0)