-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpublish_service.rb
76 lines (62 loc) · 1.96 KB
/
publish_service.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# frozen_string_literal: true
class PublishService < ApplicationService
def initialize(edition, user, with_review:)
@edition = edition
@user = user
@with_review = with_review
end
def call
live_edition = document.live_edition
publish_assets(live_edition)
associate_with_government
publish_current_edition
supersede_live_edition(live_edition)
set_new_live_edition
set_first_published_at
document.reload
rescue GdsApi::BaseError => e
GovukError.notify(e)
raise
end
private
attr_reader :edition, :user, :with_review
delegate :document, to: :edition
def publish_assets(live_edition)
PublishAssetService.call(edition, live_edition)
end
def associate_with_government
return if edition.government
government = if edition.public_first_published_at
Government.for_date(edition.public_first_published_at)
else
Government.current
end
edition.assign_attributes(government_id: government&.content_id)
# We need to update the Publishing API if we're changing the government
PreviewService.call(edition) if edition.government_id_changed?
end
def publish_current_edition
GdsApi.publishing_api_v2.publish(
document.content_id,
nil, # Sending update_type is deprecated (now in payload)
locale: document.locale,
)
end
def supersede_live_edition(live_edition)
return unless live_edition
AssignEditionStatusService.call(live_edition, user, :superseded, update_last_edited: false)
live_edition.live = false
live_edition.save!
end
def set_new_live_edition
status = with_review ? :published : :published_but_needs_2i
AssignEditionStatusService.call(edition, user, status)
edition.access_limit = nil
edition.live = true
edition.save!
end
def set_first_published_at
return if document.first_published_at
document.update!(first_published_at: Time.current)
end
end