Class: Versions::RestoreDraft

Inherits:
BaseService
  • Object
show all
Defined in:
app/services/postnhost/publishing/versions/restore_draft.rb

Constant Summary collapse

ATTRIBUTES =
{
  "Postnhost::Article" => %i[
    title title_tag og_title schema_headline schema_article_type meta_description custom_excerpt
    use_excerpt_as_meta_description content slug cover_image_alt cover_image
  ],
  "Postnhost::ArticleVariant" => %i[
    title title_tag og_title schema_headline meta_description custom_excerpt use_excerpt_as_meta_description content
  ],
  "Postnhost::Page" => %i[title title_tag og_title meta_description content slug],
  "Postnhost::PageVariant" => %i[title title_tag og_title meta_description content]
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(record:, version:) ⇒ RestoreDraft

Returns a new instance of RestoreDraft.



16
17
18
19
# File 'app/services/postnhost/publishing/versions/restore_draft.rb', line 16

def initialize(record:, version:)
  @record = record
  @version = version
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
# File 'app/services/postnhost/publishing/versions/restore_draft.rb', line 21

def call
  restored = record.class.transaction do
    record.lock!
    restore_locked!
  end
  success(restored, status: :ok)
rescue Error => e
  failure(e.messages)
rescue ActiveRecord::RecordInvalid => e
  failure(e.message)
end

#restore_locked!Object

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/postnhost/publishing/versions/restore_draft.rb', line 33

def restore_locked!
  validate_version!
  versioned = version.reify
  raise Error, "Version cannot be restored" unless versioned

  attributes.each do |attribute|
    if attribute == :cover_image
      record.write_attribute(:cover_image, versioned.cover_image.identifier.presence)
    else
      record.public_send("#{attribute}=", versioned.public_send(attribute))
    end
  end
  record.save!
  record
end