Skip to content

Commit 62a0df5

Browse files
work on upload screen
1 parent cd05768 commit 62a0df5

15 files changed

Lines changed: 241 additions & 99 deletions

app/components/admin/edition_images/image_component.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ def initialize(edition:, image:, last_image:)
77
@last_image = last_image
88
end
99

10+
def embeddable_image?
11+
Whitehall.image_kinds.fetch(@image.image_data.image_kind).permitted_uses.include?("govspeak_embed")
12+
end
13+
1014
private
1115

1216
attr_reader :edition, :image, :last_image

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? && @edition.type_instance.form("images").present? %>
1+
<% if edition.configurable_document_type.present? %>
22
<%= render "govuk_publishing_components/components/heading", {
33
text: "Select a lead image",
44
font_size: "l",
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
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
7+
end
8+
9+
def embeddable_image?
10+
@image_kind.permitted_uses.include?("govspeak_embed")
611
end
712

813
private
914

1015
attr_reader :edition
1116

1217
def document_images
13-
return edition.images unless edition.can_have_custom_lead_image?
18+
edition_images = edition.images.select { |image| Whitehall.image_kinds.fetch(image.image_data.image_kind) == @image_kind }
19+
20+
return edition_images unless edition.can_have_custom_lead_image?
1421

15-
edition.images - [edition.lead_image].compact
22+
edition_images - [edition.lead_image].compact
1623
end
1724
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/edition.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ def can_have_custom_lead_image?
433433
is_a?(Edition::CustomLeadImage)
434434
end
435435

436+
def allows_multiple_of_image_kind?(image_kind)
437+
image_kind == "govspeak_embed"
438+
end
439+
436440
def images_have_unique_filenames?
437441
names = images.map(&:filename)
438442
names.uniq.length == names.length

app/models/standard_edition.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def body
4343
block_content["body"]
4444
end
4545

46+
def lead_image
47+
block_content["image"]
48+
end
49+
4650
def body=(_)
4751
nil
4852
end
@@ -63,6 +67,16 @@ def allows_image_attachments?
6367
type_instance.settings["images"]["enabled"]
6468
end
6569

70+
def allows_multiple_of_image_kind?(image_kind)
71+
return if type_instance.settings["images"]["permitted_image_kinds"].blank?
72+
73+
type_instance_image_kind = type_instance.settings["images"]["permitted_image_kinds"].detect { |permitted_image_kind| permitted_image_kind["kind"] == image_kind }
74+
75+
return if type_instance_image_kind.blank?
76+
77+
type_instance_image_kind["multiple"]
78+
end
79+
6680
def allows_file_attachments?
6781
type_instance.settings["file_attachments_enabled"]
6882
end

app/views/admin/edition_images/_image_upload.html.erb

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,53 +3,57 @@
33
text = image_kind.permitted_uses.include?("govspeak_embed") ? "Upload an image" : "Upload an #{image_kind.display_name} image"
44
%>
55

6-
<%= form_tag(
7-
admin_edition_images_path(@edition),
8-
multipart: true,
9-
) do %>
10-
<%= render "govuk_publishing_components/components/file_upload", {
11-
label: {
12-
text:,
13-
},
14-
heading_level: 2,
15-
heading_size: "l",
16-
javascript: true,
17-
multiple: true,
18-
name: "images[][image_data][file]",
19-
id: "edition_images_image_data_file",
20-
hint: raw('Images can be JPEG, PNG, SVG or GIF files. If you are uploading more than one image, <a class="govuk-link" href="https://www.gov.uk/guidance/how-to-publish-on-gov-uk/images-and-videos">read the image guidance</a> on using unique file names.'),
21-
error_items: @new_image.present? ? errors_for(@new_image.errors, :"image_data.file") : nil,
22-
accept: "image/png, image/jpeg, image/gif, image/svg+xml",
23-
} %>
24-
25-
<% if image_kind.present? %>
26-
<%= hidden_field_tag("image[image_data][image_kind]", @edition.permitted_image_kinds.first.name) %>
27-
<% else %>
28-
<%= render "govuk_publishing_components/components/radio", {
29-
heading: "What kind of image is this?",
30-
name: "image[image_data][image_kind]",
31-
items: @edition.permitted_image_kinds.map do |image_kind|
32-
{
33-
value: image_kind.name,
34-
text: image_kind.display_name,
35-
}
36-
end,
6+
<% unless !@edition.allows_multiple_of_image_kind?(image_kind.permitted_uses.first) && @edition.images.select { |image| Whitehall.image_kinds.fetch(image.image_data.image_kind) == image_kind }.count == 1 %>
7+
<%= form_tag(
8+
admin_edition_images_path(@edition),
9+
multipart: true,
10+
) do %>
11+
<%= render "govuk_publishing_components/components/file_upload", {
12+
label: {
13+
text:,
14+
},
15+
heading_level: 2,
16+
heading_size: "l",
17+
javascript: true,
18+
multiple: @edition.allows_multiple_of_image_kind?(image_kind.permitted_uses.first),
19+
name: "images[][image_data][file]",
20+
id: "edition_images_image_data_file",
21+
hint: raw('Images can be JPEG, PNG, SVG or GIF files. If you are uploading more than one image, <a class="govuk-link" href="https://www.gov.uk/guidance/how-to-publish-on-gov-uk/images-and-videos">read the image guidance</a> on using unique file names.'),
22+
error_items: @new_image.present? ? errors_for(@new_image.errors, :"image_data.file") : nil,
23+
accept: "image/png, image/jpeg, image/gif, image/svg+xml",
3724
} %>
38-
<% end %>
3925

40-
<%= render "govuk_publishing_components/components/details", {
41-
title: "You must use an SVG for charts and diagrams",
42-
} do %>
43-
SVGs allow users to magnify images without losing quality.
44-
Find out <%= link_to "how to create an SVG file (opens in new tab)",
45-
"https://www.gov.uk/guidance/how-to-publish-on-gov-uk/images-and-videos#creating-an-svg-file",
46-
class: "govuk-link",
47-
target: "_blank",
48-
rel: "noopener" %>.
26+
<% if image_kind.present? %>
27+
<%= hidden_field_tag("images[][image_data][image_kind]", @edition.permitted_image_kinds.first.name) %>
28+
<% else %>
29+
<%= render "govuk_publishing_components/components/radio", {
30+
heading: "What kind of image is this?",
31+
name: "images[][image_data][image_kind]",
32+
items: @edition.permitted_image_kinds.map do |image_kind|
33+
{
34+
value: image_kind.name,
35+
text: image_kind.display_name,
36+
}
37+
end,
38+
} %>
4939
<% end %>
5040

51-
<%= render "govuk_publishing_components/components/button", {
52-
text: "Upload",
53-
margin_bottom: 10,
54-
} %>
41+
<%= render "govuk_publishing_components/components/details", {
42+
title: "You must use an SVG for charts and diagrams",
43+
} do %>
44+
SVGs allow users to magnify images without losing quality.
45+
Find out <%= link_to "how to create an SVG file (opens in new tab)",
46+
"https://www.gov.uk/guidance/how-to-publish-on-gov-uk/images-and-videos#creating-an-svg-file",
47+
class: "govuk-link",
48+
target: "_blank",
49+
rel: "noopener" %>.
50+
<% end %>
51+
52+
<%= render "govuk_publishing_components/components/button", {
53+
text: "Upload",
54+
margin_bottom: 10,
55+
} %>
56+
<% end %>
5557
<% end %>
58+
59+
<%= render Admin::EditionImages::UploadedImagesComponent.new(edition: @edition, image_kind:) %>

app/views/admin/edition_images/index.html.erb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,8 @@
1010
items: secondary_navigation_tabs_items(@edition, request.path),
1111
} %>
1212

13-
<% if @edition.is_a?(StandardEdition) %>
14-
<% @edition.permitted_image_kinds.each do |image_kind| %>
15-
<%= render "image_upload", edition: @edition, image_kind: %>
16-
<% end %>
17-
<% else %>
18-
<%= render "image_upload", edition: @edition %>
13+
<% @edition.permitted_image_kinds.each do |image_kind| %>
14+
<%= render "image_upload", edition: @edition, image_kind: %>
1915
<% end %>
20-
21-
<%= render Admin::EditionImages::UploadedImagesComponent.new(edition: @edition) %>
2216
</div>
2317
</div>

config/image_kinds.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ topical_event_header:
112112
permitted_uses:
113113
- header
114114
versions:
115-
- name: hero_desktop_2x
115+
- name: s800_header
116116
width: 800
117117
height: 800
118118
topical_event_logo:
@@ -122,6 +122,6 @@ topical_event_logo:
122122
permitted_uses:
123123
- logo
124124
versions:
125-
- name: hero_desktop_2x
125+
- name: s800_logo
126126
width: 800
127127
height: 800

features/edition-images-javascript.feature

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,39 @@ Feature: Images tab on edit edition
1111
When I visit the images tab of the document with images
1212
Then I should see a list with 2 images
1313
When I upload a 960x960 image
14+
Then I should see the image cropper in the following edit screen
15+
When I update the image details and save
1416
Then I should see a list with 3 images
17+
Then I should not see that the image requires cropping
18+
19+
@javascript
20+
Scenario: Images uploaded with cropping required
21+
And a draft document with images exists
22+
When I visit the images tab of the document with images
23+
Then I should see a list with 2 images
24+
When I upload multiple images including a 960x960 image
25+
Then I should see a list with 4 images
1526
Then I should see that the image requires cropping
1627
When I click to edit the details of the image that needs to be cropped
28+
Then I should see the image cropper in the following edit screen
1729
When I update the image details and save
18-
Then I should not see that the image requires cropping
30+
Then I should see a list with 4 images
31+
Then I should not see that the image requires cropping
32+
33+
@javascript
34+
Scenario: Image uploaded with no cropping required
35+
And a draft publication "New Draft Publication" exists
36+
When I visit the images tab of the document "New Draft Publication"
37+
And I upload a 960x640 image
38+
Then I should not see the image cropper in the following edit screen
39+
When I update the image details and save
40+
Then I should see a list with 1 image
1941

2042
@javascript
2143
Scenario: Uploading a file with a duplicated filename
2244
When a draft document with images exists
2345
And I visit the images tab of the document with images
2446
And I upload a 960x960 image
47+
When I update the image details and save
2548
And I upload a 960x960 image
2649
Then I should get 1 error message

0 commit comments

Comments
 (0)