Skip to content
Petr Mlčoch edited this page Jul 20, 2022 · 6 revisions

CMS pages consist of atoms. Atoms can be added to any model using the Folio::HasAtoms concern. Use Folio::HasAtoms::Basic for single locale or Folio::HasAtoms::Localized for localized atoms.

class ProjectNamespace::Model < ApplicationRecord
  include Folio::HasAtoms::Basic
end

Generating atoms

regular atom:

$ rails g folio:atom foo
      create  app/models/project_namespace/atom/foo.rb
      create  app/cells/project_namespace/atom/foo_cell.rb
      create  app/cells/project_namespace/atom/foo/show.slim
      create  test/cells/project_namespace/atom/foo_cell_test.rb

molecule-forming atom:

$ rails g folio:molecule person
      create  app/models/project_namespace/atom/person.rb
      create  app/cells/project_namespace/molecule/people_cell.rb
      create  app/cells/project_namespace/molecule/people/show.slim
      create  test/cells/project_namespace/molecule/people_cell_test.rb

Check other folio generators (rails g), eg. : rails g folio:prepared_atom all.

Atom definition

Atom models are subclasses of Folio::Atom::Base. Each STI variant defines it's view cell and a set of constants that define its structure. Example:

class ProjectNamespace::Atom::Foo < Folio::Atom::Base
  ATTACHMENTS = %i[cover]

  STRUCTURE = {
    title: :string,
    content: :richtext,
    orientation: %w[vertical horizontal]
  }

  ASSOCIATIONS = {
    product: %w[ProjectNamespace::Product],
  }

  def self.cell_name
    'project_namespace/atom/foo'
  end

  def self.console_icon
    :eye
  end
end

Structure

Data is stored-to and read-from a single JSON data column using method_missing.

Use STRUCTURE constant to define atom fields. Valid options:

symbol decsription
string same as psql string
text same as psql text
richtext same as text, but using a richtext editor in console
code unescaped HTML used for embeds
integer same as psql integer
float same as psql float
date same as psql date
datetime same as psql datetime
color string field with a color picker in console
boolean same as psql boolean

Instead of a symbol you can also pass an array of values. Such key would be stored as a string and displayed as a select in console.

Associations

Data is stored-to and read-from a single JSON associations column using method_missing. Each defined association is stored as a key with a hash of id and base class name { id: 'id', type: 'type' }.

ProjectNamespace::Atom::MyAtom.last.associations
=> {"product"=>{"id"=>"10", "type"=>"ProjectNamespace::Product"}}

Attachments

Which of the placements keys of Folio::HasAttachments should be handled in console.

Atom views

Atoms are rendered using Folio::AtomsHelper via render_atoms_in_molecules. Each atom has either a cell_name or a molecule_cell_name class method defining the name of the cell used for rendering.

Atoms vs Molecules

Atoms can be grouped together into molecules for easier rendering of lists. When molecule_cell_name is present, all neighbouring atoms (based on position) get grouped into an array, which is served to the cell defined in molecule_cell_name as model. When cell_name is present, atom is passed as model to the cell.