Class: Articles::SnapshotWriter

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

Constant Summary collapse

ATTRIBUTES =
%i[
  language_id title title_tag og_title schema_headline schema_article_type meta_description custom_excerpt
  auto_excerpt use_excerpt_as_meta_description content slug cover_image_alt top_pick
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(article:) ⇒ SnapshotWriter

Returns a new instance of SnapshotWriter.



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

def initialize(article:)
  @article = article
end

Instance Method Details

#callObject

Raises:



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

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

  snapshot = article.article_snapshot || article.build_article_snapshot
  snapshot.assign_attributes(snapshot_attributes.merge(paper_trail_version: version))
  snapshot.published_at ||= Time.current
  snapshot.save!
  replace_categories!(snapshot)
  replace_authors!(snapshot)
  replace_suggestions!(snapshot)
  clear_schedule!
  snapshot
end