@@ -3,15 +3,10 @@ module Publishable
33 extend ActiveSupport ::Concern
44
55 included do
6- scope :draft , -> { where ( public_on : nil ) }
7- scope :scheduled , -> { where . not ( public_on : nil ) }
8-
6+ scope :draft , -> { Alchemy . config . publishable_resolver . draft ( all ) }
7+ scope :scheduled , -> { Alchemy . config . publishable_resolver . scheduled ( all ) }
98 scope :published , -> ( at : Current . preview_time ) {
10- scheduled
11- . where ( "#{ table_name } .public_on <= :at" , at :)
12- . where ( public_until : nil ) . or (
13- where ( "#{ table_name } .public_until > :at" , at :)
14- )
9+ Alchemy . config . publishable_resolver . published ( all , at :)
1510 }
1611
1712 validate do
@@ -25,42 +20,32 @@ module Publishable
2520
2621 # Determines if this record is public
2722 #
28- # Takes the two timestamps +public_on+ and +public_until+
29- # and returns true if the time given (+Time.current+ per default)
30- # is in this timespan.
31- #
32- # @param at [DateTime] (Time.current)
23+ # @param at [DateTime] (Current.preview_time)
3324 # @returns Boolean
3425 def public? ( at : Current . preview_time )
35- already_public_for? ( at : ) && still_public_for ?( at :)
26+ publishable_resolver . public ?( at :)
3627 end
3728 alias_method :public , :public?
3829
30+ # Determines if this record has a future publication or expiration event
31+ #
32+ # @param at [DateTime] (Current.preview_time)
33+ # @returns Boolean
3934 def scheduled? ( at : Current . preview_time )
40- ( public_on . present? && public_on > at ) || ( public_until . present? && public_until > at )
35+ publishable_resolver . scheduled? ( at : )
4136 end
4237
4338 # Determines if this record is publishable
4439 #
45- # A record is publishable if a +public_on+ timestamp is set and not expired yet.
46- #
4740 # @returns Boolean
4841 def publishable?
49- ! public_on . nil? && still_public_for ?
42+ publishable_resolver . publishable ?
5043 end
5144
52- # Determines if this record is already public for given time
53- # @param at [DateTime] (Time.current)
54- # @returns Boolean
55- def already_public_for? ( at : Current . preview_time )
56- !public_on . nil? && public_on <= at
57- end
45+ private
5846
59- # Determines if this record is still public for given time
60- # @param at [DateTime] (Time.current)
61- # @returns Boolean
62- def still_public_for? ( at : Current . preview_time )
63- public_until . nil? || public_until > at
47+ def publishable_resolver
48+ Alchemy . config . publishable_resolver . new ( self )
6449 end
6550 end
6651end
0 commit comments