diff --git a/app/components/budgets/budget_component.html.erb b/app/components/budgets/budget_component.html.erb index d0855c9db..332920b2e 100644 --- a/app/components/budgets/budget_component.html.erb +++ b/app/components/budgets/budget_component.html.erb @@ -1,6 +1,6 @@ <% if budget.image.present? %>
+ style="<%= attached_background_css polymorphic_path(budget.image.variant(:large)) %>"> <% else %>
<% end %> diff --git a/app/components/budgets/budget_component.rb b/app/components/budgets/budget_component.rb index b11ebce85..4b842a3fd 100644 --- a/app/components/budgets/budget_component.rb +++ b/app/components/budgets/budget_component.rb @@ -1,5 +1,6 @@ class Budgets::BudgetComponent < ApplicationComponent attr_reader :budget + delegate :attached_background_css, to: :helpers def initialize(budget) @budget = budget diff --git a/app/helpers/images_helper.rb b/app/helpers/images_helper.rb index 06d63e9f8..787f8cf25 100644 --- a/app/helpers/images_helper.rb +++ b/app/helpers/images_helper.rb @@ -14,4 +14,8 @@ module ImagesHelper version: (version if image.persisted?), show_caption: show_caption end + + def attached_background_css(path) + "background-image: url('#{j path}');" + end end diff --git a/app/views/dashboard/poster/index.html.erb b/app/views/dashboard/poster/index.html.erb index e1cddb5d4..bbd988356 100644 --- a/app/views/dashboard/poster/index.html.erb +++ b/app/views/dashboard/poster/index.html.erb @@ -18,7 +18,7 @@

<% if proposal.image.present? %> -
+
<% else %>
);">
<% end %> diff --git a/app/views/dashboard/poster/index.pdf.erb b/app/views/dashboard/poster/index.pdf.erb index c762156d2..7c4eaebe1 100644 --- a/app/views/dashboard/poster/index.pdf.erb +++ b/app/views/dashboard/poster/index.pdf.erb @@ -23,7 +23,7 @@

<% if proposal.image.present? %> -
+
<% else %>
');">
<% end %> diff --git a/app/views/layouts/devise.html.erb b/app/views/layouts/devise.html.erb index 7ed3b55b1..26943bf56 100644 --- a/app/views/layouts/devise.html.erb +++ b/app/views/layouts/devise.html.erb @@ -10,7 +10,7 @@ <%= raw setting["html.per_page_code_body"] %>
)"> + style="<%= attached_background_css asset_url(image_path_for("auth_bg.jpg")) %>">

<%= link_to root_path do %> <%= image_tag(image_path_for("logo_header.png"), class: "float-left", alt: setting["org_name"]) %> diff --git a/spec/components/budgets/budget_component_spec.rb b/spec/components/budgets/budget_component_spec.rb index 02d94bc6a..7e46693d0 100644 --- a/spec/components/budgets/budget_component_spec.rb +++ b/spec/components/budgets/budget_component_spec.rb @@ -36,4 +36,43 @@ describe Budgets::BudgetComponent do 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 diff --git a/spec/factories/budgets.rb b/spec/factories/budgets.rb index 2e57a920b..57beb8737 100644 --- a/spec/factories/budgets.rb +++ b/spec/factories/budgets.rb @@ -65,6 +65,10 @@ FactoryBot.define do voting_style { "approval" } end + trait :with_image do + after(:create) { |budget| create(:image, imageable: budget) } + end + trait :with_winner do after(:create) { |budget| create(:budget_investment, :winner, budget: budget) } end diff --git a/spec/factories/files.rb b/spec/factories/files.rb index 5876c90c8..5aa2a9295 100644 --- a/spec/factories/files.rb +++ b/spec/factories/files.rb @@ -8,6 +8,10 @@ FactoryBot.define do association :imageable, factory: :proposal end + trait :budget_image do + association :imageable, factory: :budget + end + trait :budget_investment_image do association :imageable, factory: :budget_investment end diff --git a/spec/fixtures/files/clippy(with_brackets).jpg b/spec/fixtures/files/clippy(with_brackets).jpg new file mode 100644 index 000000000..1edb6659b Binary files /dev/null and b/spec/fixtures/files/clippy(with_brackets).jpg differ diff --git a/spec/fixtures/files/clippy_with_'quotes'.jpg b/spec/fixtures/files/clippy_with_'quotes'.jpg new file mode 100644 index 000000000..1edb6659b Binary files /dev/null and b/spec/fixtures/files/clippy_with_'quotes'.jpg differ diff --git a/spec/helpers/images_helper_spec.rb b/spec/helpers/images_helper_spec.rb new file mode 100644 index 000000000..f115a84f3 --- /dev/null +++ b/spec/helpers/images_helper_spec.rb @@ -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 diff --git a/spec/models/budget_spec.rb b/spec/models/budget_spec.rb index 79415f512..ff1ae010e 100644 --- a/spec/models/budget_spec.rb +++ b/spec/models/budget_spec.rb @@ -6,6 +6,7 @@ describe Budget do it_behaves_like "sluggable", updatable_slug_trait: :drafting it_behaves_like "reportable" it_behaves_like "globalizable", :budget + it_behaves_like "acts as imageable", :budget_image describe "scopes" do describe ".open" do diff --git a/spec/models/image_spec.rb b/spec/models/image_spec.rb index 6b4603923..dc1a908bc 100644 --- a/spec/models/image_spec.rb +++ b/spec/models/image_spec.rb @@ -2,6 +2,7 @@ require "rails_helper" describe Image do it_behaves_like "image validations", "budget_investment_image" + it_behaves_like "image validations", "budget_image" it_behaves_like "image validations", "proposal_image" it "stores attachments with Active Storage" do