Skip to content

Trek::Pages::Versionable

Content versioning for pages: snapshot the current content and image as a PageVersion, then keep editing while the snapshot stays live.

What it adds

  • has_many :versions (most recent first) with a current sub-scope, plus accepts_nested_attributes_for :versions

Methods

MethodPurpose
create_current_version_from_page!snapshots the page's content and image as the new current version
current_versionthe live version (or nil when not versioned)
current_content / current_sectionscontent from the current version, falling back to the page itself
current_image / current_image_urlimage from the current version, falling back to the page, then its parent

Usage

Versioning is opt-in per page via the versioned boolean column:

ruby
page.update(versioned: true)
page.create_current_version_from_page!  # freeze what's live
page.update(content: new_draft)         # keep editing safely
page.current_content                    # => still the frozen snapshot

Released under the MIT License.