-
Notifications
You must be signed in to change notification settings - Fork 194
/
publishing.rb
93 lines (76 loc) · 2.68 KB
/
publishing.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
module Edition::Publishing
extend ActiveSupport::Concern
included do
has_one :unpublishing, dependent: :destroy
validates :major_change_published_at, presence: true, if: :published?
validate :change_note_present!, if: :change_note_required?
validate :attachment_passed_virus_scan!, if: :virus_check_required?
attr_accessor :skip_virus_status_check
scope :significant_change, -> { where(minor_change: false) }
end
module ClassMethods
def by_major_change_published_at
order(arel_table[:major_change_published_at].desc)
end
def unpublished_as(slug)
document = Document.at_slug(document_type, slug)
document && document.latest_edition && document.latest_edition.unpublishing
end
end
def first_published_version?
published_major_version.nil? || published_major_version == 1
end
def first_published_major_version?
published_major_version == 1 && published_minor_version.zero?
end
def published_version
if published_major_version && published_minor_version
"#{published_major_version}.#{published_minor_version}"
end
end
def change_note_required?
if deleted? || superseded?
false
elsif draft? && new_record?
false
else
other_editions.published.any?
end
end
def change_note_present!
if change_note.blank? && !minor_change
errors.add(:change_note, "can't be blank")
end
end
def virus_check_required?
allows_attachments? && published? && !skip_virus_status_check
end
def attachment_passed_virus_scan!
errors.add(:attachments, "must have passed virus scanning.") unless valid_virus_state?
end
def build_unpublishing(attributes = {})
super(attributes.merge(slug: slug, document_type: type))
end
def approve_retrospectively
if force_published?
self.force_published = false
save!
else
errors.add(:base, "This document has not been force-published")
false
end
end
def increment_version_number
if minor_change?
self.published_major_version = Edition.unscoped.where(document_id: document_id).maximum(:published_major_version) || 1
self.published_minor_version = (Edition.unscoped.where(document_id: document_id, published_major_version: published_major_version).maximum(:published_minor_version) || -1) + 1
else
self.published_major_version = (Edition.unscoped.where(document_id: document_id).maximum(:published_major_version) || 0) + 1
self.published_minor_version = 0
end
end
def reset_version_numbers
self.published_major_version = previous_edition.try(:published_major_version)
self.published_minor_version = previous_edition.try(:published_minor_version)
end
end