@@ -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?
0 commit comments