Dedicated Controllers for Specific CMS File Formats

With Rails Connector (version 6.7.3 or higher) you can use dedicated controllers for every CMS file format. Creating a controller for a specific file format only requires that the controller class is created.

Common use cases are:

  • Organizing a large number of views into folders according to file formats
  • Providing multiple switchable views for a given CMS file
  • Processing session data, cookies, or URL parameters for specific CMS file formats
  • Using Rails’ autoload mechanism for the helper modules needed in the current context (without helper :all)
  • Handling POST requests to implement, for example, web forms via CMS files
  • Delivering CMS data in several different formats, such as XML, HTML, JSON

For every CMS file to be loaded, the Rails Connector looks for a controller whose name matches the name of the current CMS format. For example, a file with the Publication format would be delivered by a controller named PublicationController that descends from RailsConnector::DefaultCmsController. If no such controller exists, CmsController is used as a default.

The index action is still used as the default action to deliver CMS data. Custom actions can be evoked by overwriting the controller_action_name method of the Obj class.

The Rails Connector provides a special method that is useful when writing tests for CMS related controllers. Use request.for_cms_object to define which CMS object should be loaded in your test. The following example uses the rspec test framework:

describe CmsController, "request for HTML document" do
  before do
    controller.stub!(:ensure_object_is_permitted).and_return(true)
    controller.stub!(:ensure_object_is_active).and_return(true)
    controller.stub!(:set_google_expire_header).and_return(true)
    @obj = mock_model(Obj, :mime_type => 'text/html', :permalink => 'dummy')
    request.for_cms_object(@obj)
  end

  it "should render the view" do
    get 'index'
    response.should be_success
  end
end