Class: PageVariants::SnapshotWriter

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(variant:) ⇒ SnapshotWriter

Returns a new instance of SnapshotWriter.



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

def initialize(variant:)
  @variant = variant
end

Instance Method Details

#callObject

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/postnhost/publishing/page_variants/snapshot_writer.rb', line 10

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

  snapshot = variant.page_variant_snapshot || variant.build_page_variant_snapshot
  snapshot.assign_attributes(
    variant.attributes.symbolize_keys.slice(*ATTRIBUTES).merge(
      page_id: variant.page_id,
      paper_trail_version: version
    )
  )
  snapshot.published_at ||= Time.current
  snapshot.save!
  snapshot
end