Skip to content

Commit

Permalink
Use lead_image assoication in uploaded_images_component
Browse files Browse the repository at this point in the history
  • Loading branch information
davidgisbey committed Nov 2, 2023
1 parent 208325a commit dc69754
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@
<% end %>

<div class="govuk-grid-row">
<% if lead_image %>
<% if lead_image_hash %>
<div class="govuk-grid-column-one-third">
<img src="<%= lead_image[:url] %>" alt="<%= lead_image[:preview_alt_text] %>" class="app-view-edition-resource__preview">
<% unless lead_image[:all_image_asset_variants_uploaded] %><span class="govuk-tag govuk-tag--green">Processing</span><% end %>
<img src="<%= lead_image_hash[:url] %>" alt="<%= lead_image_hash[:preview_alt_text] %>" class="app-view-edition-resource__preview">
<% unless lead_image_hash[:all_image_asset_variants_uploaded] %><span class="govuk-tag govuk-tag--green">Processing</span><% end %>
</div>
<div class="govuk-grid-column-two-thirds">
<p class="govuk-body"><strong>Caption: </strong><%= lead_image[:caption] %></p>
<p class="govuk-body"><strong>Alt text: </strong><%= lead_image[:alt_text] %></p>
<p class="govuk-body"><strong>Caption: </strong><%= lead_image_hash[:caption] %></p>
<p class="govuk-body"><strong>Alt text: </strong><%= lead_image_hash[:alt_text] %></p>
</div>
<% end %>

<% if lead_image || @edition.type == "CaseStudy" %>
<% if lead_image_hash || @edition.type == "CaseStudy" %>
<%= form_with(url: update_image_display_option_admin_edition_path(@edition), method: :patch) do |form| %>
<div class="app-view-edition-resource__actions govuk-grid-column-full govuk-button-group">
<% if @edition.type == "CaseStudy" %>
Expand All @@ -41,8 +41,8 @@
} %>
<% end %>

<% if lead_image %>
<% lead_image[:links].each do |link| %><%= link %><% end %>
<% if lead_image_hash %>
<% lead_image_hash[:links].each do |link| %><%= link %><% end %>
<% end %>
</div>
<% end %>
Expand Down
29 changes: 15 additions & 14 deletions app/components/admin/edition_images/uploaded_images_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,37 @@
class Admin::EditionImages::UploadedImagesComponent < ViewComponent::Base
def initialize(edition:)
@edition = edition
@lead_image = has_lead_image? ? @edition.images.first : nil
@document_images = @edition.images.drop(has_lead_image? ? 1 : 0)
end

def has_lead_image?
can_have_lead_image? && @edition.images.any?
end

def can_have_lead_image?
@edition.can_have_custom_lead_image?
end

def lead_image
image_to_hash @lead_image, 0 if has_lead_image?
@lead_image ||= can_have_lead_image? ? @edition.lead_image : nil
end

def lead_image_hash
image_to_hash(lead_image, 0) if lead_image
end

def document_images
@document_images.map.with_index(1) { |image, index| image_to_hash image, index }
@document_images ||= (@edition.images - [lead_image].compact)
.map.with_index(1) { |image, index| image_to_hash(image, index) }
end

def new_image_display_option
if @edition.image_display_option == "no_image"
return has_lead_image? ? "custom_image" : "organisation_image"
if @edition.image_display_option == "no_image" && @edition.images.present?
"custom_image"
elsif @edition.image_display_option == "no_image"
"organisation_image"
else
"no_image"
end

"no_image"
end

def update_image_display_option_button_text
if has_lead_image?
if @edition.images.present?
return "#{new_image_display_option == 'no_image' ? 'Hide' : 'Show'} lead image"
end

Expand All @@ -56,7 +57,7 @@ def all_image_asset_variants_uploaded?(image)
def image_to_hash(image, index)
{
url: image.url,
preview_alt_text: index.zero? ? "Lead image" : "Image #{index}",
preview_alt_text: lead_image == image ? "Lead image" : "Image #{index}",
caption: image.caption.presence || "None",
alt_text: image.alt_text.presence || "None",
markdown: unique_names? ? "[Image: #{image.filename}]" : "!!#{index}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,37 @@
require "test_helper"

class Admin::EditionImages::UploadedImagesComponentTest < ViewComponent::TestCase
test "lead image rendered for case study" do
test "lead image rendered for case study with default hidden field that toggles organisation image off" do
images = [build_stubbed(:image), build_stubbed(:image)]
edition = build_stubbed(:draft_case_study, images:)
edition = build_stubbed(:draft_case_study, images:, lead_image: images.first)
render_inline(Admin::EditionImages::UploadedImagesComponent.new(edition:))

assert_selector "img", count: 2
assert_selector "img[alt='Lead image']"
assert_selector "img[alt='Image 1']"
assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='no_image']", visible: :hidden
end

test "has the correct hidden field for toggling 'image_display_option' when it's 'no_image' and no images have been to the case study" do
edition = build_stubbed(:draft_case_study, image_display_option: "no_image")
render_inline(Admin::EditionImages::UploadedImagesComponent.new(edition:))

assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='organisation_image']", visible: :hidden
end

test "has the correct hidden field for toggling 'image_display_option' when it's 'organisation_image' and no images have been to the case study" do
edition = build_stubbed(:draft_case_study, image_display_option: "organisation_image")
render_inline(Admin::EditionImages::UploadedImagesComponent.new(edition:))

assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='no_image']", visible: :hidden
end

test "has the correct hidden field for toggling 'image_display_option' when it's 'no_image' and an image has been to the case study" do
image = build_stubbed(:image)
edition = build_stubbed(:draft_case_study, image_display_option: "no_image", images: [image])
render_inline(Admin::EditionImages::UploadedImagesComponent.new(edition:))

assert_selector "input[type='hidden'][name='edition[image_display_option]'][value='custom_image']", visible: :hidden
end

test "lead image not rendered for publication" do
Expand Down

0 comments on commit dc69754

Please sign in to comment.