Class: Alchemy::Publishable::TimestampResolver

Inherits:
Object
  • Object
show all
Defined in:
app/models/alchemy/publishable/timestamp_resolver.rb

Overview

Default resolver for Publishable records.

Uses the public_on and public_until timestamp columns to determine whether a record is public, scheduled, or publishable.

This class defines the interface that custom resolvers configured via Alchemy.config.publishable_resolver must implement.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publishable) ⇒ TimestampResolver

Returns a new instance of TimestampResolver.



34
35
36
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 34

def initialize(publishable)
  @publishable = publishable
end

Class Method Details

.draft(publishables) ⇒ Object

Returns records without a public_on date set.



16
17
18
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 16

def draft(publishables)
  publishables.where(public_on: nil)
end

.published(publishables, at:) ⇒ Object

Returns records that are public at the given time.



26
27
28
29
30
31
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 26

def published(publishables, at:)
  table_name = publishables.table_name
  scheduled(publishables)
    .where("#{table_name}.public_on <= :at", at:)
    .where("#{table_name}.public_until IS NULL OR #{table_name}.public_until > :at", at:)
end

.scheduled(publishables) ⇒ Object

Returns records with a public_on date set.



21
22
23
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 21

def scheduled(publishables)
  publishables.where.not(public_on: nil)
end

Instance Method Details

#public?(at: Current.preview_time) ⇒ Boolean

Determines if the record is public at the given time.

Returns:

  • (Boolean)


39
40
41
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 39

def public?(at: Current.preview_time)
  already_public_for?(at:) && still_public_for?(at:)
end

#publishable? ⇒ Boolean

Determines if the record is publishable.

A record is publishable if a public_on timestamp is set and not expired yet.

Returns:

  • (Boolean)


53
54
55
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 53

def publishable?
  !publishable.public_on.nil? && still_public_for?(at: Time.current)
end

#scheduled?(at: Current.preview_time) ⇒ Boolean

Determines if the record has a future publication or expiration event.

Returns:

  • (Boolean)


44
45
46
47
# File 'app/models/alchemy/publishable/timestamp_resolver.rb', line 44

def scheduled?(at: Current.preview_time)
  (publishable.public_on.present? && publishable.public_on > at) ||
    (publishable.public_until.present? && publishable.public_until > at)
end