Class: Publication::Saver

Inherits:
BaseService show all
Defined in:
app/services/publication/saver.rb

Overview

Service object: saver.

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #process, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Class Method Details

.call(id: nil, publication: nil, publication_params: {}, user: nil) ⇒ Object



11
12
13
# File 'app/services/publication/saver.rb', line 11

def self.call(id: nil, publication: nil, publication_params: {}, user: nil)
  new.call(id:, publication:, publication_params:, user:)
end

Instance Method Details

#call(id: nil, publication: nil, publication_params: {}, user: nil) ⇒ Object

user: is retained as part of the CRM service API. Controllers pass the
authorized actor even though publication persistence does not consume it.
:reek:UnusedParameters



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/services/publication/saver.rb', line 18

def call(id: nil, publication: nil, publication_params: {}, user: nil) # rubocop:disable Lint/UnusedMethodArgument
  publication ||= Item.publications.find(id) if id
  publication ||= initialize_publication
  result = nil

  Item.transaction do
    track_page_placement = publication.persisted? && attribute_supplied?(publication_params, :tags)
    previous_page_paths = publication.publication_landing_page_paths if track_page_placement
    publication.assign_attributes(publication_params)
    track_page_placement_change(publication, previous_page_paths) if track_page_placement

    if publication.save
      changed_literature = publication.has_literature_changed?
      if changed_literature || publication.primary_image.nil?
        # A cache-relevant save already has one PublicationUpdated callback
        # waiting for this transaction to commit. Otherwise the generated
        # cover publishes its own update after assigning primary_image_id.
        publish_cover_update = !publication.publication_cache_relevant_change?
        publication.create_primary_image_from_pdf(publish_update: publish_cover_update)
      end
      # search_text is refreshed asynchronously by UploadMarkdownExtractionWorker
      # off the PublicationPdfChanged event a literature change publishes — no
      # synchronous docling call in the save path.
      result = Result.new(publication: publication, saved: true)
    else
      result = Result.new(publication: publication, saved: false, errors: publication.errors.full_messages)
      raise ActiveRecord::Rollback
    end
  end

  reset_rolled_back_page_placement(publication) unless result.saved?
  result
end