module Undercarriage::Models::PublishedConcern

Published

Based on the presence of a datetime in the `published_at` column (configurable) in the database. If there is a datetime in the column, it is considered published. You need to do your own validation to ensure the value is a datetime value

Usage

# Model
class Example < ApplicationRecord
  include Undercarriage::Models::PublishedConcern

  ##
  # The name of the column is expected to be `published_at`. If that is not the case for you, uncomment the
  # following to change the column name
  #
  # self.published_column = :ready_at

  ##
  # The following are useful helpers for the model. They are not part of the concern
  #
  scope :available, -> { published.where("#{published_column} <= ?", Time.current) }

  def available?
    published? && self[published_column] <= Time.current
  end

  scope :scheduled, -> { published.where("#{published_column} > ?", , Time.current) }

  def scheduled?
    published? && self[published_column] > Time.current
  end
end

# Controller
class PagesController < AdminController
  def index
    @examples = Example.published
  end
end

# View
<% @examples.each do |example| %>
  Published?: <%= example.published? %>
<% end %>

Public Instance Methods

published?() click to toggle source

Published check

Check if an item is published based on the presence of a value in the published column. This does not take into account whether the item is not currently available (scheduled). See module documentation for more information

Usage

@example.published? => true
@example.published? => false

@return [Boolean] if resource is published

# File lib/undercarriage/models/published_concern.rb, line 100
def published?
  self[self.class.published_column].present?
end
unpublished?() click to toggle source

Unpublished check

Check if an item is unpublished based on the lack of presence of a value in the published column

Usage

@example.unpublished? => true
@example.unpublished? => false

@return [Boolean] if resource is unpublished

# File lib/undercarriage/models/published_concern.rb, line 115
def unpublished?
  !published?
end