Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgisbey committed Nov 2, 2023
1 parent 6e3e9fb commit 98fc488
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
15 changes: 4 additions & 11 deletions app/components/admin/edition_images/image_component.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# frozen_string_literal: true

class Admin::EditionImages::ImageComponent < ViewComponent::Base
def initialize(edition:, image:, last_image:)
def initialize(edition:, image:, last_image:, document_images_index:)
@edition = edition
@image = image
@last_image = last_image
@document_images_index = document_images_index
end

private

attr_reader :edition, :image, :last_image
attr_reader :edition, :image, :last_image, :document_images_index

def preview_alt_text
"Image #{find_index_from_non_lead_images + 1}"
"Image #{document_images_index + 1}"
end

def caption
Expand All @@ -27,14 +28,6 @@ def image_markdown
edition.images_have_unique_filenames? ? "[Image: #{image.filename}]" : "!!#{find_image_index + 1}"
end

def find_index_from_non_lead_images
if edition.respond_to?(:non_lead_images)
edition.non_lead_images.find_index(image)
else
edition.images.find_index(image)
end
end

def find_image_index
edition.images.find_index(image)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
} %>

<ul class="govuk-list">
<% document_images.each do |image| %>
<%= render Admin::EditionImages::ImageComponent.new(edition: edition, image: image, last_image: image == document_images.last) %>
<% document_images.each_with_index do |image, index| %>
<%= render Admin::EditionImages::ImageComponent.new(
edition: edition,
image: image,
last_image: image == document_images.last,
document_images_index: index,
) %>
<% end %>
</ul>
<% else %>
Expand Down
14 changes: 7 additions & 7 deletions test/components/admin/edition_images/image_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase
test "renders the correct default fields" do
image = build_stubbed(:image, caption: "caption", alt_text: "alt text")
edition = build_stubbed(:draft_publication, images: [image])
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false, document_images_index: 0))

assert_selector ".govuk-grid-row .govuk-grid-column-one-third img[alt='Image 1']"
assert_selector ".govuk-grid-row .govuk-grid-column-two-thirds .govuk-body:nth-child(1)", text: "Caption: caption"
Expand All @@ -22,7 +22,7 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase
test "renders placeholder text for caption and alt text when none has been provided" do
image = build_stubbed(:image, caption: nil, alt_text: nil)
edition = build_stubbed(:draft_publication, images: [image])
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false, document_images_index: 0))

assert_selector ".govuk-grid-row .govuk-grid-column-two-thirds .govuk-body:nth-child(1)", text: "Caption: None"
assert_selector ".govuk-grid-row .govuk-grid-column-two-thirds .govuk-body:nth-child(2)", text: "Alt text: None"
Expand All @@ -35,23 +35,23 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase
gif_image_data = build_stubbed(:image_data, file: gif)
images = [build_stubbed(:image, image_data: jpeg_image_data), build_stubbed(:image, image_data: gif_image_data)]
edition = build_stubbed(:draft_publication, images:)
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.first, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.first, last_image: false, document_images_index: 0))

assert_selector "input[value='[Image: 960x640_jpeg.jpg]']"
end

test "image index markdown used when edition has duplicate image filenames" do
images = [build_stubbed(:image), build_stubbed(:image)]
edition = build_stubbed(:draft_publication, images:)
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.first, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.first, last_image: false, document_images_index: 0))

assert_selector "input[value='!!1']"
end

test "image index markdown handles a lead image being present correctly" do
images = [build_stubbed(:image), build_stubbed(:image), build_stubbed(:image)]
edition = build_stubbed(:draft_news_article, images:, lead_image: images.second)
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.third, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image: images.third, last_image: false, document_images_index: 0))

assert_selector "input[value='!!3']"
end
Expand All @@ -60,7 +60,7 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase
image = build_stubbed(:image, image_data: build_stubbed(:image_data_with_no_assets))
edition = build_stubbed(:draft_news_article, images: [image])

render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: false, document_images_index: 0))

assert_selector ".govuk-tag", text: "Processing"
end
Expand All @@ -69,7 +69,7 @@ class Admin::EditionImages::ImageComponentTest < ViewComponent::TestCase
image = build_stubbed(:image)
edition = build_stubbed(:draft_news_article, images: [image])

render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: true))
render_inline(Admin::EditionImages::ImageComponent.new(edition:, image:, last_image: true, document_images_index: 0))

assert_selector ".app-view-edition-resource__section-break", count: 0
end
Expand Down

0 comments on commit 98fc488

Please sign in to comment.