Scheduling and Expiring Content

Scrivito lets you auto-publish and auto-expire content in the future.

No matter whether you’re looking to prepare press releases and blog posts to publish later, hide information for an event once it is over, publish information that is replaced regularly, or publish special deals that expire, Scrivito has got you covered.

There are three steps necessary:

  1. Add two date attributes to the corresponding page object class.
  2. Make these attributes editable in place.
  3. Enforce these two attributes in your app.

We will go through these steps in detail below.

This guide is based on our instructions for integrating Scrivito you can complete in no time after creating your Scrivito account.

Create a new object class with two date attributes

Let's start by creating a new type of CMS object, SpecialDealPage:

$ rails g scrivito:page SpecialDealPage
      create  app/models/special_deal_page.rb
      create  app/controllers/special_deal_page_controller.rb
      create  app/views/special_deal_page/index.html.erb
      create  app/views/special_deal_page/details.html.erb
      create  app/views/special_deal_page/thumbnail.html.erb

Open the generated model file in your editor and add these two attributes to the model class:

# app/models/special_deal_page.rb
class SpecialDealPage < Obj
  # …
  attribute :valid_from, :date
  attribute :valid_until, :date
  # …

Now create a new page and you will notice that the “Select Page Type” dialog now includes the “SpecialDealPage” type.

You can customize the appearance of the page type in the selection dialog by means of the  app/views/special_deal_page/thumbnail.html.erb template.

Go ahead and create such a special deal page.

Turn the date attributes into editable page properties

You can now create special deals directly on the website, but you cannot set those shiny new date attributes yet. Open the page menu on the right hand side of the Scrivito control panel and select “Page properties” from it. The properties dialog lets you edit the title only.

To turn the dates into editable page properties, open app/views/special_deal_page/details.html.erb and replace its contents with the following:

  <h3>Page Title</h3>
  <%= scrivito_tag :div, @obj, :title %>

  <h3>Deal valid from</h3>
  <%= scrivito_tag :div, @obj, :valid_from %>

  <h3>Deal valid until</h3>
  <%= scrivito_tag :div, @obj, :valid_until %>

Re-open the page properties dialog, and you will notice that there are three attributes offered for editing now. Set “Deal valid from” to tomorrow.

Enforce the date attributes in your application

You can now create a special deal and assign dates indicating when the deal will start and end, but the deal will still appear any time you request the page.

You still need to enforce the dates in your app. This is done in the controller specific to the page type. You generated this controller at the beginning of this guide. Open app/controllers/special_deal_page_controller.rb and replace it with the following:

class SpecialDealPageController < CmsController
  before_filter only: :index do
  	if (@obj.valid_from && @obj.valid_from > ||
       (@obj.valid_until && @obj.valid_until <
  	  render :expired, status: 404

The before_filter should be only applied to the index action. This way it doesn't block the details, thumbnail, or any other views. Inside the filter it checks if there is a valid_from date defined and whether it is still in the future, or if valid_until is defined and whether this date is in the past. Is one of the conditions met, the before_filter renders the template for expired deals with an HTTP 404 status.

Create the app/views/special_deal_page/expired.html.erb template and set its content to the following:

  Sorry, this deal has expired.

If you reload the page now (and you've set valid_from to tomorrow), you should see the above message. You can still access the page properties to change valid_from to yesterday, for example. After closing the page properties dialog, the page is reloaded, and the message about the expired deal should be gone.

Congratulations, you've just implemented a content scheduling and expiring mechanism!