Display first image available for milestones

This commit is contained in:
decabeza
2018-09-12 18:36:17 +02:00
parent d698a724ac
commit ba1a6b4cc8
3 changed files with 25 additions and 13 deletions

View File

@@ -6,4 +6,9 @@ module BudgetExecutionsHelper
.last.status_id == status rescue false }.count .last.status_id == status rescue false }.count
end end
def first_milestone_with_image(investment)
investment.milestones.order(publication_date: :asc, created_at: :asc)
.select{ |milestone| milestone.image.present? }.first
end
end end

View File

@@ -1,9 +1,9 @@
<% investment.milestones.order(publication_date: :desc).limit(1).each do |milestone| %> <% milestone = first_milestone_with_image(investment) %>
<% if milestone.image.present? %>
<% if milestone&.image.present? %>
<%= image_tag milestone.image_url(:large), alt: milestone.image.title %> <%= image_tag milestone.image_url(:large), alt: milestone.image.title %>
<% elsif investment.image.present? %> <% elsif investment.image.present? %>
<%= image_tag investment.image_url(:thumb), alt: investment.image.title %> <%= image_tag investment.image_url(:large), alt: investment.image.title %>
<% else %> <% else %>
<%= image_tag "budget_execution_no_image.jpg", alt: investment.title %> <%= image_tag "budget_execution_no_image.jpg", alt: investment.title %>
<% end %> <% end %>
<% end %>

View File

@@ -61,6 +61,7 @@ feature 'Executions' do
end end
context 'Images' do context 'Images' do
scenario 'renders milestone image if available' do scenario 'renders milestone image if available' do
milestone1 = create(:budget_investment_milestone, investment: investment1) milestone1 = create(:budget_investment_milestone, investment: investment1)
create(:image, imageable: milestone1) create(:image, imageable: milestone1)
@@ -99,15 +100,21 @@ feature 'Executions' do
expect(page).to have_css("img[alt='#{investment4.title}']") expect(page).to have_css("img[alt='#{investment4.title}']")
end end
scenario "renders last milestone's image if investment has multiple milestones with images associated" do scenario "renders first milestone's image if investment has multiple milestones with images associated" do
milestone1 = create(:budget_investment_milestone, investment: investment1, milestone1 = create(:budget_investment_milestone, investment: investment1,
publication_date: 2.weeks.ago) publication_date: Date.yesterday)
milestone2 = create(:budget_investment_milestone, investment: investment1, milestone2 = create(:budget_investment_milestone, investment: investment1,
publication_date: Date.yesterday) publication_date: Date.yesterday)
create(:image, imageable: milestone1, title: 'First milestone image') milestone3 = create(:budget_investment_milestone, investment: investment1,
create(:image, imageable: milestone2, title: 'Second milestone image') publication_date: Date.yesterday)
milestone4 = create(:budget_investment_milestone, investment: investment1,
publication_date: Date.yesterday)
create(:image, imageable: milestone2, title: 'Image for first milestone with image')
create(:image, imageable: milestone3, title: 'Image for second milestone with image')
visit budget_path(budget) visit budget_path(budget)
@@ -116,8 +123,8 @@ feature 'Executions' do
expect(page).to have_content(investment1.title) expect(page).to have_content(investment1.title)
expect(page).to have_css("img[alt='#{milestone2.image.title}']") expect(page).to have_css("img[alt='#{milestone2.image.title}']")
expect(page).not_to have_css("img[alt='#{milestone1.image.title}']")
end end
end end
context 'Filters' do context 'Filters' do