Skip to content

Commit 5cfda0d

Browse files
Merge pull request #10998 from alphagov/single-upload-of-image-kind
Single Upload of Image Kind [WHIT-2857]
2 parents cd9c725 + 3c70fed commit 5cfda0d

28 files changed

Lines changed: 399 additions & 70 deletions

app/components/admin/edition_images/uploaded_images_component.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<% if edition.configurable_document_type.present? %>
1+
<% if edition.configurable_document_type.present? && edition.allows_lead_image? %>
22
<%= render "govuk_publishing_components/components/heading", {
33
text: "Select a lead image",
44
font_size: "l",
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
# frozen_string_literal: true
22

33
class Admin::EditionImages::UploadedImagesComponent < ViewComponent::Base
4-
def initialize(edition:)
4+
def initialize(edition:, image_kind: Whitehall.image_kinds.fetch("default"))
55
@edition = edition
6+
@image_kind = image_kind
67
end
78

89
private
910

1011
attr_reader :edition
1112

1213
def document_images
13-
return edition.images unless edition.can_have_custom_lead_image?
14+
edition_images = edition.images.select { |image| Whitehall.image_kinds.fetch(image.image_data.image_kind) == @image_kind }
1415

15-
edition.images - [edition.lead_image].compact
16+
return edition_images unless edition.can_have_custom_lead_image?
17+
18+
edition_images - [edition.lead_image].compact
1619
end
1720
end

app/controllers/admin/edition_images_controller.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ def update
4141
def create
4242
@images = images_params.map { |image| create_image(image) }
4343

44-
if @images.empty?
45-
flash.now.alert = "No images selected. Choose a valid JPEG, PNG, SVG or GIF."
46-
elsif @images.all?(&:valid?)
44+
if @images.all?(&:valid?)
4745
@images.each(&:save)
4846
@edition.update_lead_image if @edition.can_have_custom_lead_image?
4947
PublishingApiDocumentRepublishingWorker.perform_async(@edition.document_id, false)
@@ -53,7 +51,11 @@ def create
5351
@images.each { |image| @edition.images.delete(image) }
5452
end
5553

56-
render :index
54+
if @images.count == 1 && @images.first.valid?
55+
redirect_to edit_admin_edition_image_path(@edition, @images.first.id)
56+
else
57+
render :index
58+
end
5759
end
5860

5961
def create_image(image)
@@ -119,7 +121,7 @@ def enforce_permissions!
119121
end
120122

121123
def images_params
122-
params.fetch(:images, []).map { |image| image.permit(image_data: %i[file]) }
124+
params.fetch(:images, []).map { |image| image.permit(image_data: %i[file image_kind]) }
123125
end
124126

125127
def image_data_params

app/models/concerns/edition/images.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ def process_associations_after_save(edition)
2121
end
2222

2323
included do
24-
has_many :images, foreign_key: "edition_id", dependent: :destroy
24+
has_many :images, foreign_key: "edition_id", dependent: :destroy do
25+
def of_kind(image_kind)
26+
joins(:image_data).where(image_data: { image_kind: image_kind })
27+
end
28+
end
2529

2630
accepts_nested_attributes_for :images, reject_if: :no_substantive_attributes?, allow_destroy: true
2731

app/models/configurable_document_types/government_response.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@
7979
"publishing_api_schema_name": "news_article",
8080
"publishing_api_document_type": "government_response",
8181
"rendering_app": "frontend",
82-
"images_enabled": true,
82+
"images": {
83+
"enabled": true,
84+
"permitted_image_kinds": [
85+
{
86+
"kind": "govspeak_embed",
87+
"multiple": true
88+
}
89+
]
90+
},
8391
"send_change_history": true,
8492
"file_attachments_enabled": true,
8593
"organisations": null,

app/models/configurable_document_types/history_page.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@
7272
"publishing_api_schema_name": "history",
7373
"publishing_api_document_type": "history",
7474
"rendering_app": "frontend",
75-
"images_enabled": true,
75+
"images": {
76+
"enabled": true,
77+
"permitted_image_kinds": [
78+
{
79+
"kind": "govspeak_embed",
80+
"multiple": true
81+
}
82+
]
83+
},
7684
"send_change_history": false,
7785
"organisations": ["af07d5a5-df63-4ddc-9383-6a666845ebe9"],
7886
"backdating_enabled": false,

app/models/configurable_document_types/news_story.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@
7979
"publishing_api_schema_name": "news_article",
8080
"publishing_api_document_type": "news_story",
8181
"rendering_app": "frontend",
82-
"images_enabled": true,
82+
"images": {
83+
"enabled": true,
84+
"permitted_image_kinds": [
85+
{
86+
"kind": "govspeak_embed",
87+
"multiple": true
88+
}
89+
]
90+
},
8391
"send_change_history": true,
8492
"file_attachments_enabled": true,
8593
"organisations": null,

app/models/configurable_document_types/press_release.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,15 @@
7979
"publishing_api_schema_name": "news_article",
8080
"publishing_api_document_type": "press_release",
8181
"rendering_app": "frontend",
82-
"images_enabled": true,
82+
"images": {
83+
"enabled": true,
84+
"permitted_image_kinds": [
85+
{
86+
"kind": "govspeak_embed",
87+
"multiple": true
88+
}
89+
]
90+
},
8391
"send_change_history": true,
8492
"file_attachments_enabled": true,
8593
"organisations": null,

app/models/configurable_document_types/topical_event.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,19 @@
111111
"publishing_api_schema_name": "topical_event",
112112
"publishing_api_document_type": "topical_event",
113113
"rendering_app": "collections",
114-
"images_enabled": false,
114+
"images": {
115+
"enabled": true,
116+
"permitted_image_kinds": [
117+
{
118+
"kind": "header",
119+
"multiple": false
120+
},
121+
{
122+
"kind": "logo",
123+
"multiple": false
124+
}
125+
]
126+
},
115127
"send_change_history": true,
116128
"file_attachments_enabled": false,
117129
"organisations": null,

app/models/configurable_document_types/world_news_story.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@
7878
"publishing_api_schema_name": "news_article",
7979
"publishing_api_document_type": "world_news_story",
8080
"rendering_app": "frontend",
81-
"images_enabled": true,
81+
"images": {
82+
"enabled": true,
83+
"permitted_image_kinds": [
84+
{
85+
"kind": "govspeak_embed",
86+
"multiple": true
87+
}
88+
]
89+
},
8290
"send_change_history": true,
8391
"file_attachments_enabled": true,
8492
"organisations": null,

0 commit comments

Comments
 (0)