CMS Layouts as Extensible Page Types

Our Rails Connector makes it possible to access CMS data from within a Rails application. For this purpose, the Rails Connector provides a variable, @obj that represents the file to be delivered. You can, for example, access the value of the abstract field using @obj.abstract.

@obj is of the Obj class. Up to and including version 6.7.2, the Rails Connector automatically creates subclasses of Obj based on the file format name so that, for example, a CMS file that has the NewsArticle format automatically becomes an instance of the NewsArticle class.

From Version 6.7.3, subclasses of Obj are no longer created automatically. Thus, all CMS files are instances of the Obj class. If, however, the application includes a class that is based on Obj and has the same name as a file format, all files with this format automatically are instances of this class.

Format-specific classes can be used to extend CMS files to which a particular file format was assigned with new properties and methods. If, for example, all CMS files with the Product format should be ratable, you can define the Product model as follows:

# This Model describes the behavior of all CMS objects of the Product class.
class Product < Obj

  has_many :ratings, :dependent => :delete_all, :foreign_key => :obj_id

  def rate(score)
    rating = ratings.find_by_score(score) || ratings.build(:score => score)
    rating.count += 1
    rating.save
  end

  def count_for_score(score)
    rating = ratings.find_by_score(score)
    rating ? rating.count : 0
  end

  def rated?
    !ratings.empty?
  end

  def average_rating
    raise TypeError unless rated?
    sum, count = ratings.inject([0, 0]) do |(sum, count), rating|
      [sum + rating.score * rating.count, count + rating.count]
    end
    sum.to_f / count.to_f
  end

  def average_rating_in_percent
    if rated?
      (100 * average_rating / Rating::MAXIMUM).to_i
    else
      0
    end
  end

  def reset_rating
    ratings.clear
  end
end

Layout and File-Specific Action Names

The default action when accessing a CMS file obj is ìndex. To use other actions with individual formats, the controller_action_name method of the model concerned (which needs to be created if it does not exist) can be overwritten. You might, for example, take the action from an action field of the CMS file:

class Product < Obj
  # ...

  def controller_action_name
    self[:action]
    end

  end

To have the action to be called determined for all instances of Obj (independently of the file format), implement controller_action_name in the obj_extensions.rb file of your web application.

Note on Class Names

Ruby requires that class names follow the CamelCase notation, meaning that they must start with a capital letter. We strongly recommend to rename file formats in the CMS so that they follow this convention. The affected file formats can be determined using the check:obj_classes Rake task (included in the Rails Connector plugin). Afterwards, the formats can be renamed using the renameObjClass Tcl command.