Merge pull request #5106 from consul/fix_budget_background_image

Render background images with brackets in their names
This commit is contained in:
Javi Martín
2023-06-30 17:20:56 +02:00
committed by GitHub
14 changed files with 71 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
<% if budget.image.present? %> <% if budget.image.present? %>
<div class="budget-header with-background-image" <div class="budget-header with-background-image"
style="background-image: url(<%= polymorphic_path(budget.image.variant(:large)) %>);"> style="<%= attached_background_css polymorphic_path(budget.image.variant(:large)) %>">
<% else %> <% else %>
<div class="budget-header"> <div class="budget-header">
<% end %> <% end %>

View File

@@ -1,5 +1,6 @@
class Budgets::BudgetComponent < ApplicationComponent class Budgets::BudgetComponent < ApplicationComponent
attr_reader :budget attr_reader :budget
delegate :attached_background_css, to: :helpers
def initialize(budget) def initialize(budget)
@budget = budget @budget = budget

View File

@@ -14,4 +14,8 @@ module ImagesHelper
version: (version if image.persisted?), version: (version if image.persisted?),
show_caption: show_caption show_caption: show_caption
end end
def attached_background_css(path)
"background-image: url('#{j path}');"
end
end end

View File

@@ -18,7 +18,7 @@
</p> </p>
<div class="proposal-image"> <div class="proposal-image">
<% if proposal.image.present? %> <% if proposal.image.present? %>
<div class="overflow-image" style="background-image: url(<%= polymorphic_path(proposal.image.variant(:large)) %>);"></div> <div class="overflow-image" style="<%= attached_background_css polymorphic_path(proposal.image.variant(:large)) %>"></div>
<% else %> <% else %>
<div class="overflow-image" style="background-image: url(<%= asset_url "default_mailing.jpg" %>);"></div> <div class="overflow-image" style="background-image: url(<%= asset_url "default_mailing.jpg" %>);"></div>
<% end %> <% end %>

View File

@@ -23,7 +23,7 @@
</p> </p>
<div class="proposal-image"> <div class="proposal-image">
<% if proposal.image.present? %> <% if proposal.image.present? %>
<div class="overflow-image" style="background-image: url(<%= polymorphic_url(proposal.image.variant(:large)) %>);"></div> <div class="overflow-image" style="<%= attached_background_css polymorphic_url(proposal.image.variant(:large)) %>"></div>
<% else %> <% else %>
<div class="overflow-image" style="background-image:url('<%= "file://#{Rails.root.join("app","assets","images","default_mailing.jpg")}" %>');"></div> <div class="overflow-image" style="background-image:url('<%= "file://#{Rails.root.join("app","assets","images","default_mailing.jpg")}" %>');"></div>
<% end %> <% end %>

View File

@@ -10,7 +10,7 @@
<%= raw setting["html.per_page_code_body"] %> <%= raw setting["html.per_page_code_body"] %>
<div class="wrapper"> <div class="wrapper">
<div class="auth-image small-12 medium-3 column" <div class="auth-image small-12 medium-3 column"
style="background-image: url(<%= asset_url(image_path_for("auth_bg.jpg")) %>)"> style="<%= attached_background_css asset_url(image_path_for("auth_bg.jpg")) %>">
<h1 class="logo margin"> <h1 class="logo margin">
<%= link_to root_path do %> <%= link_to root_path do %>
<%= image_tag(image_path_for("logo_header.png"), class: "float-left", alt: setting["org_name"]) %> <%= image_tag(image_path_for("logo_header.png"), class: "float-left", alt: setting["org_name"]) %>

View File

@@ -36,4 +36,43 @@ describe Budgets::BudgetComponent do
end end
end end
end end
describe "budget image" do
it "does not show a background image when the budget has no image" do
render_inline Budgets::BudgetComponent.new(budget)
expect(page).to have_css ".budget-header"
expect(page).not_to have_css ".with-background-image"
expect(page).not_to have_css ".budget-header[style*='background-image:']"
end
it "shows a background image when the bugdet image is defined" do
budget = create(:budget, :with_image)
render_inline Budgets::BudgetComponent.new(budget)
expect(page).to have_css ".budget-header.with-background-image"
expect(page).to have_css ".budget-header[style*='background-image:'][style*='clippy.jpg']"
end
it "quotes the background image filename so it works with filenames with brackets" do
budget.update!(image: create(:image, attachment: fixture_file_upload("clippy(with_brackets).jpg")))
render_inline Budgets::BudgetComponent.new(budget)
expect(page).to have_css ".budget-header.with-background-image"
expect(page).to have_css ".budget-header[style*='background-image:']"\
"[style*='url(\\''][style*='clippy(with_brackets).jpg\\'']"
end
it "escapes single quotes in the background image filename" do
budget.update!(image: create(:image, attachment: fixture_file_upload("clippy_with_'quotes'.jpg")))
render_inline Budgets::BudgetComponent.new(budget)
expect(page).to have_css ".budget-header.with-background-image"
expect(page).to have_css ".budget-header[style*='background-image:']"\
"[style*='url(\\''][style*='clippy_with_\\\\\'quotes\\\\\'.jpg']"
end
end
end end

View File

@@ -65,6 +65,10 @@ FactoryBot.define do
voting_style { "approval" } voting_style { "approval" }
end end
trait :with_image do
after(:create) { |budget| create(:image, imageable: budget) }
end
trait :with_winner do trait :with_winner do
after(:create) { |budget| create(:budget_investment, :winner, budget: budget) } after(:create) { |budget| create(:budget_investment, :winner, budget: budget) }
end end

View File

@@ -8,6 +8,10 @@ FactoryBot.define do
association :imageable, factory: :proposal association :imageable, factory: :proposal
end end
trait :budget_image do
association :imageable, factory: :budget
end
trait :budget_investment_image do trait :budget_investment_image do
association :imageable, factory: :budget_investment association :imageable, factory: :budget_investment
end end

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

View File

@@ -0,0 +1,13 @@
require "rails_helper"
describe ImagesHelper do
describe "#attached_background_css" do
it "adds quotes around the path" do
expect(attached_background_css("myurl")).to eq "background-image: url('myurl');"
end
it "escapes quotes inside the path" do
expect(attached_background_css("url_'quotes'")).to eq "background-image: url('url_\\'quotes\\'');"
end
end
end

View File

@@ -6,6 +6,7 @@ describe Budget do
it_behaves_like "sluggable", updatable_slug_trait: :drafting it_behaves_like "sluggable", updatable_slug_trait: :drafting
it_behaves_like "reportable" it_behaves_like "reportable"
it_behaves_like "globalizable", :budget it_behaves_like "globalizable", :budget
it_behaves_like "acts as imageable", :budget_image
describe "scopes" do describe "scopes" do
describe ".open" do describe ".open" do

View File

@@ -2,6 +2,7 @@ require "rails_helper"
describe Image do describe Image do
it_behaves_like "image validations", "budget_investment_image" it_behaves_like "image validations", "budget_investment_image"
it_behaves_like "image validations", "budget_image"
it_behaves_like "image validations", "proposal_image" it_behaves_like "image validations", "proposal_image"
it "stores attachments with Active Storage" do it "stores attachments with Active Storage" do