Learn how Scrivito CMS can help you deliver amazing digital experiences
See Scrivito CMS in action

Using Object Classes

Using Object Classes

Object classes and their models

The Scrivito SDK makes the CMS data model available to your Rails application. In this model, all CMS objects are represented by the “Obj” base class and the classes derived from it. The functionality provided in “Obj” is the default you can override or extend in the derived object classes, e.g. “Homepage.” For details on how to create object classes, see Creating and Modifying Object Classes.

To illustrate how the object class hierarchy helps to provide functionality for different page types, imagine a website where pages of almost all types include a breadcrumb navigation. The template for this might contain the following:

<%= render("header") %> <%= render("breadcrumbs") %> <%= render("content") %>

After a while you notice that breadcrumbs are useless on the homepage, so you need to render them conditionally:

<%= render("header") %> <%= render("breadcrumbs") if @obj.show_breadcrumbs? %> <%= render("content") %>

Then, in the “Obj” model, provide the “show_breadcrumbs?” method so that breadcrumbs are displayed on pages of all types:

# app/models/obj.rb class Obj < Scrivito::BasicObj def show_breadcrumbs? true end end

Now, since we don't want breadcrumbs to appear on the homepage (or all homepages if there are several of them), we can switch off the breadcrumbs for pages of this particular type:

class Homepage < Obj def show_breadcrumbs? false end end

In this approach, it's the developer who decides on the page types that include or don't include breadcrumbs, not the editors. For letting editors choose, you require an attribute, e.g. “show_breadcrumbs” the editors can fill in, either for all page types or just selected ones such as “Homepage,” like so:

class Homepage < Obj attribute :content, :widgetlist attribute :show_breadcrumbs, :enum, values: ["yes", "no"]   def show_breadcrumbs?     self[:show_breadcrumbs] == "yes"   end end

Finding a CMS object's neighbors

The “Obj” class provides built-in functionality related to the neighborhood of CMS objects. The neighborhood relationship of an object with respect to other objects is defined by their individual path. If, for example, an object's path is “/en/company/about”, this and all other objects whose path components except for the last one are the same, e.g. “/en/company/contact”, are siblings. They are children of their parent object which is represented by the second last path component, “/en/company” in this example.

Note that the path components between the root object and the object concerned not necessarily need to exist as CMS objects. However, they must exist for being able to query their children or parent, for example.

“Obj” includes methods for finding CMS objects, e.g. by their path (“find_by_path”) or permalink (“find_by_permalink”), and for searching all of the object hierarchy.

In-place editing functionality

“Obj” also offers functionality often required when building websites, e.g. “slug” or “permalink.” Additionally, several hook methods are provided that support editorial work, e.g. “valid_page_classes_beneath” and “description_for_editor.” The first one lets you limit the page types offered when creating a page underneath a particular CMS object path. “description_for_editor” allows you to provide custom descriptions for pages. These descriptions show up in the changes list of a working copy, and you might want them to be derived from a particular attribute, e.g. “title”:

class Obj < Scrivito::BasicObj def description_for_editor if binary? name else title.presence || "<untitled #{obj_class}>" end end end