Class: Pages::SnapshotWriter

Inherits:
Object
  • Object
show all
Defined in:
app/services/postnhost/publishing/pages/snapshot_writer.rb

Constant Summary collapse

ATTRIBUTES =
%i[language_id title title_tag og_title meta_description content slug].freeze

Instance Method Summary collapse

Constructor Details

#initialize(page:) ⇒ SnapshotWriter

Returns a new instance of SnapshotWriter.



6
7
8
# File 'app/services/postnhost/publishing/pages/snapshot_writer.rb', line 6

def initialize(page:)
  @page = page
end

Instance Method Details

#callObject

Raises:



10
11
12
13
14
15
16
17
18
19
20
# File 'app/services/postnhost/publishing/pages/snapshot_writer.rb', line 10

def call
  validate!
  version = page.paper_trail.save_with_version
  raise Error, "could not capture the snapshot source version" unless version&.persisted?

  snapshot = page.page_snapshot || page.build_page_snapshot
  snapshot.assign_attributes(page.attributes.symbolize_keys.slice(*ATTRIBUTES).merge(paper_trail_version: version))
  snapshot.published_at ||= Time.current
  snapshot.save!
  snapshot
end