Skip to content

Trek::Fragmentable ​

Composes a record from reusable fragments, based on a configuration matrix mapping record keys to the attributes finding each fragment.

Configuration ​

In config/page_fragments.yml, read with config_for, so under shared: or an environment's key:

yaml
shared:
  matrix:
    home:
      - { namespace: home, key: intro }
      - { namespace: home, key: newsletter }
    about:
      - { namespace: about, key: team }

Each entry is passed to Fragment.find_by. Without the file, a record has no fragments.

Methods ​

MethodPurpose
fragmentsthe Fragment records configured for this record's key
fragments_attributes=bulk-updates fragments from nested form attributes

Usage ​

ruby
class Page < ApplicationRecord
  include Trek::Fragmentable
end

page = Page.find_by(key: "home")
page.fragments # => the home/intro and home/newsletter fragments

This is what powers the fragment editing UI on page forms in the admin.

Released under the MIT License.