-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedition.rb
More file actions
133 lines (107 loc) · 3.26 KB
/
edition.rb
File metadata and controls
133 lines (107 loc) · 3.26 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Edition < ApplicationRecord
validates :title, presence: {
message: lambda do |edition, _|
I18n.t("activerecord.errors.models.edition.blank",
attribute_with_indefinite_article: edition.document.title_name_with_indefinite_article)
end,
}
validates :change_note, presence: true, if: :major_change?, on: :change_note
validates :major_change, inclusion: [true, false], on: :change_note
include Cloneable
include Documentable
include HasAuditTrail
include HasAuthBypassToken
include HasAuthors
include ValidatesDetails
include HasLeadOrganisation
include Workflow
include ValidatesUniquenessOfTitle
has_one :review_outcome, dependent: :destroy
has_one :fact_check_outcome, dependent: :destroy
has_many :domain_events
delegate :schema, to: :document
scope :current_versions, lambda {
published
}
scope :published, -> { where(state: "published") }
scope :most_recent_for_document, lambda {
where(updated_at: Edition.active.select("MAX(updated_at)").group(:document_id))
}
scope :most_recent_first, lambda {
order(updated_at: :desc)
}
scope :active, -> { where.not(state: Edition.inactive_states) }
def self.most_recent
most_recent_first.first
end
def first_edition?
document.editions.published.count.zero? ||
published? && document.editions.published.count == 1
end
def show_embed_codes?
published? || scheduled?
end
def any_embedded_objects_to_show?
document.schema.subschemas.map(&:id).any? do |subschema_id|
details[subschema_id].present?
end
end
def render(embed_code = document.embed_code)
ContentBlockTools::ContentBlock.new(
document_type: "content_block_#{block_type}",
content_id: document.content_id,
title:,
details:,
embed_code:,
).render
end
def add_object_to_details(object_type, body)
key = ObjectKey.new(details, object_type, body["title"]).to_s
details[object_type] ||= {}
details[object_type][key] = remove_destroyed body.to_h
end
def update_object_with_details(object_type, object_title, body)
details[object_type][object_title] = remove_destroyed body.to_h
end
def store_sole_object_in_details(object_type, body)
details[object_type] = remove_destroyed body.to_h
end
def has_entries_for_subschema_id?(subschema_id)
details[subschema_id].present?
end
def has_multiple_subschema_entries?
schema = document.schema
subschemas = schema.subschemas
total_entries = subschemas.inject(0) do |counter, subschema|
counter + (details[subschema.id].try(:count) || 0)
end
total_entries > 1
end
def default_order
document.schema.subschemas.sort_by(&:group_order).map { |subschema|
item_keys = details[subschema.block_type]&.keys || []
item_keys.map do |item_key|
"#{subschema.block_type}.#{item_key}"
end
}.flatten
end
def is_scheduling?
scheduled_publication.present?
end
def is_deletable?
can_transition?(:delete)
end
private
def remove_destroyed(item)
item.transform_values { |value|
case value
when Hash
remove_destroyed(value)
when Array
value.select { |i| !i.is_a?(Hash) || i.delete("_destroy") != "1" }
else
value
end
}.to_h
end
end