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

Examples:

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 ExamplesController < ApplicationController
  def index
    @examples = Example.published
  end
end

View

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.publishedActiveRecord::Relation

Published scope

Retrieve only published resources

Examples:

Controller

class ExamplesController < ApplicationController
  def index
    @examples = Example.published
  end
end

Returns:

  • (ActiveRecord::Relation)

    published resources



# File 'lib/undercarriage/models/published_concern.rb', line 51

.unpublishedActiveRecord::Relation

Unpublished scope

Retrieve only unpublished resources

Examples:

Controller

class ExamplesController < ApplicationController
  def index
    @examples = Example.unpublished
  end
end

Returns:

  • (ActiveRecord::Relation)

    unpublished resources



80
81
82
83
84
85
86
# File 'lib/undercarriage/models/published_concern.rb', line 80

included do
  class_attribute :published_column
  self.published_column = :published_at

  scope :published, -> { where.not(published_column => nil) }
  scope :unpublished, -> { where(published_column => nil) }
end

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

Examples:

Controller or View

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

Returns:

  • (Boolean)

    if resource is published



99
100
101
# File 'lib/undercarriage/models/published_concern.rb', line 99

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

Examples:

Controller or View

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

Returns:

  • (Boolean)

    if resource is unpublished



113
114
115
# File 'lib/undercarriage/models/published_concern.rb', line 113

def unpublished?
  !published?
end