Module: Undercarriage::Models::PublishedConcern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/undercarriage/models/published_concern.rb
Overview
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 %>
Instance Method Summary collapse
-
#published? ⇒ Boolean
Published check.
-
#unpublished? ⇒ Boolean
Unpublished check.
Instance Method Details
#published? ⇒ Boolean
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
100 101 102 |
# File 'lib/undercarriage/models/published_concern.rb', line 100 def published? self[self.class.published_column].present? end |
#unpublished? ⇒ Boolean
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
115 116 117 |
# File 'lib/undercarriage/models/published_concern.rb', line 115 def unpublished? !published? end |