-
Notifications
You must be signed in to change notification settings - Fork 193
/
Copy pathedition_publisher.rb
71 lines (56 loc) · 1.97 KB
/
edition_publisher.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
require "data_hygiene/govspeak_link_validator"
class EditionPublisher < EditionService
def failure_reason
@failure_reason ||= failure_reasons.first
end
def failure_reasons
return @failure_reasons if @failure_reasons
reasons = []
reasons << "This edition is invalid: #{edition.errors.full_messages.to_sentence}" unless edition.valid?
reasons << "This edition contains links which violate linking guidelines" if govspeak_link_errors.any?
reasons << "An edition that is #{edition.current_state} cannot be #{past_participle}" unless can_transition?
reasons << "Scheduled editions cannot be published. This edition is scheduled for publication on #{edition.scheduled_publication}" if scheduled_for_publication?
@failure_reasons = reasons
end
def govspeak_link_errors
@govspeak_link_errors ||= DataHygiene::GovspeakLinkValidator.new(edition.body).errors
end
def verb
"publish"
end
private
def prepare_edition
flag_if_political_content!
edition.access_limited = false
edition.major_change_published_at = Time.zone.now unless edition.minor_change?
edition.make_public_at(edition.major_change_published_at)
edition.increment_version_number
end
def fire_transition!
super
supersede_previous_editions!
delete_unpublishing!
end
def editions_to_supersede
edition.document.editions
.where(state: %i[published unpublished])
.excluding(edition)
end
def supersede_previous_editions!
editions_to_supersede.each do |edition|
edition.supersede
edition.save!(validate: false)
end
end
def delete_unpublishing!
edition.unpublishing.destroy! if edition.unpublishing.present?
end
def scheduled_for_publication?
# Just using edition.scheduled? misses submitted editions
edition.scheduled_publication.present?
end
def flag_if_political_content!
return if edition.document.live?
edition.political = PoliticalContentIdentifier.political?(edition)
end
end