-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathstandard_edition.rb
More file actions
204 lines (159 loc) · 5.57 KB
/
standard_edition.rb
File metadata and controls
204 lines (159 loc) · 5.57 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
class StandardEdition < Edition
include Edition::Identifiable
include Edition::Images
include ::Attachable
include Edition::Featurable
include Edition::AlternativeFormatProvider
include Edition::RoleAppointments
include Edition::TopicalEvents
include Edition::WorldLocations
include Edition::Organisations
include Edition::WorldwideOrganisations
include HasBlockContent
include StandardEdition::LeadImage
include StandardEdition::Taxon
FEATURED_DOCUMENTS_DISPLAY_LIMIT = 6
attr_accessor :current_tab_context
validates :configurable_document_type, presence: true, inclusion: { in: -> { ConfigurableDocumentType.all_keys } }
scope :with_news_article_document_type, -> { where(configurable_document_type: ConfigurableDocumentType.where_group("news_article").map(&:key)) }
def format_name
type_instance.label.downcase
end
def display_type
type_instance.label
end
def publishing_api_presenter
PublishingApi::StandardEditionPresenter
end
def update_configurable_document_type(new_type_key)
return false if !is_in_valid_state_for_type_conversion? ||
ConfigurableDocumentType.convertible_from(configurable_document_type).none? { |type| type.key == new_type_key }
self.configurable_document_type = new_type_key
if save(validate: false)
StandardEdition::HygieneEnforcer.new(self).cleanup!
return true
end
false
end
def body_required?
false
end
def body
block_content["body"]
end
def body=(_)
nil
end
def can_set_previously_published?
type_instance.settings["backdating_enabled"]
end
def translatable?
type_instance.settings["translations_enabled"]
end
def locale_can_be_changed?
translatable? && translations.size <= 1
end
def allows_lead_image?
type_instance.form("images").present?
end
def allows_image_attachments?
type_instance.settings["images"]["enabled"]
end
def allows_file_attachments?
type_instance.settings["file_attachments_enabled"]
end
def allows_features?
type_instance.settings["features_enabled"]
end
def can_be_associated_with_topical_events?
[
ConfigurableContentBlocks::Path.new("topical_event_ids"), # Legacy: delete when topical events migrated
ConfigurableContentBlocks::Path.new("topical_event_document_ids"),
].any? { |path| field_paths.include?(path) }
end
def can_be_marked_political?
type_instance.settings["history_mode_enabled"]
end
def change_note_required?
return false unless type_instance.settings["send_change_history"]
super
end
def base_path
"#{type_instance.settings['base_path_prefix']}/#{slug}"
end
def type_instance
ConfigurableDocumentType.find(configurable_document_type)
end
def group
type_instance.settings["configurable_document_group"]
end
def organisation_association_enabled?
current_tab_context_includes_field?("lead_organisation_ids") &&
field_paths.include?(ConfigurableContentBlocks::Path.new("lead_organisation_ids"))
end
def worldwide_organisation_association_required?
current_tab_context_includes_field?("worldwide_organisation_document_ids") &&
required_field_paths.include?(ConfigurableContentBlocks::Path.new("worldwide_organisation_document_ids"))
end
def world_location_association_required?
current_tab_context_includes_field?("world_location_ids") &&
required_field_paths.include?(ConfigurableContentBlocks::Path.new("world_location_ids"))
end
def is_in_valid_state_for_type_conversion?
%w[draft submitted rejected].include?(state)
end
def defines_tabs?
type_instance.dynamic_tabs.any?
end
def valid_tab_key?(key)
key == "documents" || type_instance.dynamic_tabs.any? { |tab| tab["id"] == key }
end
def default_tab
"documents"
end
def permitted_image_usages
type_instance.settings["images"]["usages"].each_with_object([]) do |(usage_key, config), result|
kinds = config["kinds"].map { |kind_name| Whitehall.image_kinds[kind_name] }
result << ImageUsage.new(key: usage_key, kinds:, **config.except("kinds"))
end
end
def error_labels
{}.tap do |labels|
ConfigurableContentBlocks::DefaultObject.root_block_for(self, "documents").each do |block|
# we only want labels for leaf fields, so do nothing if this field isn't one
next if block.respond_to? :field_blocks
labels[block.path.validation_error_attribute] = block.title
end
end
end
def error_field_order
%w[title summary] + ConfigurableContentBlocks::DefaultObject
.root_block_for(self, "documents")
.each_leaf
.map { |block| block.path.validation_error_attribute }
end
private
def field_paths(&block)
[].tap do |paths|
ConfigurableContentBlocks::DefaultObject.root_block_for(self, "documents").each do |field|
# we only want paths for leaf fields, so do nothing if this field isn't one
next if field.respond_to? :field_blocks
paths << field.path if !block_given? || block.call(field)
end
end
end
def required_field_paths
field_paths { |field| field.required == true }
end
def string_for_slug
title if primary_locale.to_sym == translation.locale
end
def current_tab_context_includes_field?(attribute_name)
return true if current_tab_context.blank? # assume we are checking against the entire edition
form = type_instance.form(current_tab_context)
return true if form.nil?
(form["fields"] || {}).any? do |_key, field|
Array(field["attribute_path"]).include?(attribute_name)
end
end
end