Scheduling and Expiring Content

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:

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

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:

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:

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:

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!