Trek::Pageable
The bridge between your content models (articles, products…) and their Page representation. The model owns the data; the page owns the title, slug, SEO and position.
What it adds
has_one :page, as: :pageable, dependent: :destroy, autosave: truewithaccepts_nested_attributes_for :page- Delegates
title,image,image_url,current_image,current_image_url,to_sto the page before_createguarantees the page existsto_paramuses the page slug on the public site (and the id in the admin)
Class macros
| Macro | Purpose |
|---|---|
has_position_from_page | delegates position to the page and adds a by_position scope |
has_root_scope | adds a root scope finding resources under their index page |
find_with_page_slug(slug) | class method — resource lookup by page slug |
Navigation helpers
parent?, parent, children?, children — traverse the page hierarchy from the resource side.
Usage
ruby
class Article < ApplicationRecord
include Trek::Pageable
has_position_from_page
end
article = Article.create(page_attributes: { title: "My Article" })
article.title # => "My Article" (delegated)
article.to_param # => "my-article"