-
Notifications
You must be signed in to change notification settings - Fork 3
HowTo: Render Non Occams Models as Pages
Andrew vonderLuft edited this page Aug 26, 2023
·
2 revisions
Sometimes you want to render your regular app objects like a Occams::Cms::Page. Enter the Occams::Cms::WithFragments concern. (Note: This used to be called cms_manageable, but with the release of comfy 2.0, has been converted into a concern.)
An example from the previous implementation of this feature as cms_manageable:
class BoringModel < ActiveRecord::Base
validates :is_awesome, presence: true
cms_manageable
# Your job to figure out a nice way to handle this...
def layout
Cms::Layout.new(content: "<p>{{ cms:page:random_content }}</p>")
end
end
# Let's assume we have an instance of our BoringModel
boring_model = BoringModel.first
# Set some blocks that match the layout we defined in the class...
boring_model.blocks_attributes = [{ :identifier => 'random_content', :content => 'not so boring anymore ;-)' }]
boring_model.render
#=> "<p>not so boring anymore ;-)</p>"
Here are the steps to take:
- Add
include Occams::Cms::WithFragments
in your model. - Add a
content_cache
column to your objects table. - Associate your object with a occams layout.
Occams::Cms::WithFragments
adds
belongs_to :layout, class_name: "Occams::Cms::Layout"
- Associate your object to occams fragments which store the content that will be rendered.
Occams::Cms::WithFragments
adds
has_many :fragments,
class_name: "Occams::Cms::Fragment",
as: :record,
autosave: true,
dependent: :destroy
- Profit!
For further information, see the Occams::Cms::WithFragment
module, which is also used by Occams::Cms::Page
to handle the relationship between comfy pages and fragments. Dogfooding for the win! Remember, 'blocks' are now called fragments.
Links
- The Occams::Cms::WithFragment concern - https://github.com/avonderluft/occams/blob/main/app/models/concerns/occams/cms/with_fragments.rb