Class: ArticleVariants::SnapshotWriter

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

Constant Summary collapse

ATTRIBUTES =
i[
  language_id title title_tag og_title schema_headline meta_description custom_excerpt auto_excerpt
  use_excerpt_as_meta_description content
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(variant:) ⇒ SnapshotWriter

Returns a new instance of SnapshotWriter.



9
10
11
# File 'app/services/postnhost/publishing/article_variants/snapshot_writer.rb', line 9

def initialize(variant:)
  @variant = variant
end

Instance Method Details

#callObject

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/postnhost/publishing/article_variants/snapshot_writer.rb', line 13

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

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