Working with the Content API

peakpg edited this page Mar 26, 2013 · 2 revisions
Clone this wiki locally

While the Content API in BrowserCMS builds on top of ActiveRecord, there are some slight behavior differences to be aware of.

This guide is valid for BrowserCMS v3.3.x – v3.5.x.

Working with the Content API

One of the central features that the content API adds to models is versioning and publishing. Each content block can either be published or in draft. The data for a block is split between two tables, the primary table and it’s version table. The primary table stores the ‘live’ version of a block, typically the last ‘Published’ version of a block. The versions table stores all other versions, including future edits which are unpublished.

Differences between ActiveRecord and Content API

This can cause some confusion when using basic ActiveRecord operations, where you might not get what you expect. For example, suppose we create an Event Block


class Event < ActiveRecord::Base
  acts_as_content_block
end

event = Event.create!(:name=>"Event #1", :save_and_publish=>true)
event.name = "Event #2"
event.save!

assert_equals "Event #2", Event.find(event.id) # This is false, and will fail.

In this case, “Event #2” is a draft, stored in the ‘events_versions’ table. To create and publish the event, you can do this:


event = Event.create!(:name=>"Event #1", :save_and_publish=>true)
event.name = "Event #2"
event.publish! # This will both publish and save the record.

assert_equals "Event #2", Event.find(event.id) # This is now true.