Skip to content

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: true with accepts_nested_attributes_for :page
  • Delegates title, image, image_url, current_image, current_image_url, to_s to the page
  • before_create guarantees the page exists
  • to_param uses the page slug on the public site (and the id in the admin)

Class macros

MacroPurpose
has_position_from_pagedelegates position to the page and adds a by_position scope
has_root_scopeadds a root scope finding resources under their index page
find_with_page_slug(slug)class method — resource lookup by page slug

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"

Released under the MIT License.