From 4d674bf643a8fe7c85aa4e2734f8038d74848918 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 3 Aug 2022 13:21:15 +0200 Subject: [PATCH 1/5] Remove duplication in dashboard layout head Apparently the dashboard branch wasn't updated after we extracted a common head for all layouts in commit 6e4f697ce, so when said branch was merged we reintroduced the duplication. Furthermore, we forgot to add to the dashboard layout the changes we were applying to the common head partial. --- app/views/layouts/dashboard.html.erb | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/app/views/layouts/dashboard.html.erb b/app/views/layouts/dashboard.html.erb index 2460ba948..cf33f6f4f 100644 --- a/app/views/layouts/dashboard.html.erb +++ b/app/views/layouts/dashboard.html.erb @@ -1,16 +1,9 @@ - - - + <%= render "layouts/common_head", default_title: setting["org_name"] %> <%= render "layouts/meta_tags" %> - <%= content_for?(:title) ? yield(:title) : setting["org_name"] %> <%= content_for :canonical %> - <%= stylesheet_link_tag "application" %> - <%= javascript_include_tag "application", "data-turbolinks-track" => "reload" %> - <%= csrf_meta_tags %> - <%= favicon_link_tag "favicon.ico" %> <%= favicon_link_tag image_path_for("apple-touch-icon-200.png"), rel: "icon apple-touch-icon", sizes: "200x200", From f0ab7bcfcc36f83689f0b3f22dfba441952797f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 3 Aug 2022 15:14:02 +0200 Subject: [PATCH 2/5] Move executions image partial to a component This way it'll be easier to write tests for it. We also get rid of another helper file :). --- .../budgets/executions/image_component.html.erb} | 2 -- .../budgets/executions/image_component.rb | 14 ++++++++++++++ app/helpers/budget_executions_helper.rb | 6 ------ app/views/budgets/executions/_investments.html.erb | 2 +- 4 files changed, 15 insertions(+), 9 deletions(-) rename app/{views/budgets/executions/_image.html.erb => components/budgets/executions/image_component.html.erb} (84%) create mode 100644 app/components/budgets/executions/image_component.rb delete mode 100644 app/helpers/budget_executions_helper.rb diff --git a/app/views/budgets/executions/_image.html.erb b/app/components/budgets/executions/image_component.html.erb similarity index 84% rename from app/views/budgets/executions/_image.html.erb rename to app/components/budgets/executions/image_component.html.erb index ebe933504..37b638a50 100644 --- a/app/views/budgets/executions/_image.html.erb +++ b/app/components/budgets/executions/image_component.html.erb @@ -1,5 +1,3 @@ -<% milestone = first_milestone_with_image(investment) %> - <% if milestone&.image.present? %> <%= image_tag milestone.image.variant(:large), alt: milestone.image.title %> <% elsif investment.image.present? %> diff --git a/app/components/budgets/executions/image_component.rb b/app/components/budgets/executions/image_component.rb new file mode 100644 index 000000000..8b559350b --- /dev/null +++ b/app/components/budgets/executions/image_component.rb @@ -0,0 +1,14 @@ +class Budgets::Executions::ImageComponent < ApplicationComponent + attr_reader :investment + + def initialize(investment) + @investment = investment + end + + private + + def milestone + investment.milestones.order_by_publication_date + .select { |milestone| milestone.image.present? }.last + end +end diff --git a/app/helpers/budget_executions_helper.rb b/app/helpers/budget_executions_helper.rb deleted file mode 100644 index ff94140c5..000000000 --- a/app/helpers/budget_executions_helper.rb +++ /dev/null @@ -1,6 +0,0 @@ -module BudgetExecutionsHelper - def first_milestone_with_image(investment) - investment.milestones.order_by_publication_date - .select { |milestone| milestone.image.present? }.last - end -end diff --git a/app/views/budgets/executions/_investments.html.erb b/app/views/budgets/executions/_investments.html.erb index cc416c6ba..4a8d20834 100644 --- a/app/views/budgets/executions/_investments.html.erb +++ b/app/views/budgets/executions/_investments.html.erb @@ -7,7 +7,7 @@
<%= link_to budget_investment_path(@budget, investment, anchor: "tab-milestones"), data: { "equalizer-watch": true } do %> - <%= render "image", investment: investment %> + <%= render Budgets::Executions::ImageComponent.new(investment) %>
<%= investment.title %>
From 00ea1bf719ad5bb5af52604b7a0a0471bc4cbd1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 3 Aug 2022 15:17:49 +0200 Subject: [PATCH 3/5] Fix customization of budget_execution_no_image We forgot to change the line rendering the image in commits 3574bf867c and 810bdae37a, and so the custom image was being ignored. Note that, in the test, we're stubbing a constant instead of adding a new image. The main reason is that, if we add a new image, forks would have to change the image when they change the `VALID_IMAGES` constant; otherwise the tests would fail. --- .../executions/image_component.html.erb | 2 +- .../budgets/executions/image_component.rb | 1 + .../executions/image_component_spec.rb | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/components/budgets/executions/image_component_spec.rb diff --git a/app/components/budgets/executions/image_component.html.erb b/app/components/budgets/executions/image_component.html.erb index 37b638a50..df3709290 100644 --- a/app/components/budgets/executions/image_component.html.erb +++ b/app/components/budgets/executions/image_component.html.erb @@ -3,5 +3,5 @@ <% elsif investment.image.present? %> <%= image_tag investment.image.variant(:large), alt: investment.image.title %> <% else %> - <%= image_tag "budget_execution_no_image.jpg", alt: investment.title %> + <%= image_tag image_path_for("budget_execution_no_image.jpg"), alt: investment.title %> <% end %> diff --git a/app/components/budgets/executions/image_component.rb b/app/components/budgets/executions/image_component.rb index 8b559350b..3058ee46d 100644 --- a/app/components/budgets/executions/image_component.rb +++ b/app/components/budgets/executions/image_component.rb @@ -1,5 +1,6 @@ class Budgets::Executions::ImageComponent < ApplicationComponent attr_reader :investment + delegate :image_path_for, to: :helpers def initialize(investment) @investment = investment diff --git a/spec/components/budgets/executions/image_component_spec.rb b/spec/components/budgets/executions/image_component_spec.rb new file mode 100644 index 000000000..a40d8d1ce --- /dev/null +++ b/spec/components/budgets/executions/image_component_spec.rb @@ -0,0 +1,25 @@ +require "rails_helper" + +describe Budgets::Executions::ImageComponent do + context "investment and milestone without image" do + let(:component) { Budgets::Executions::ImageComponent.new(Budget::Investment.new) } + + it "shows the default image" do + render_inline component + + expect(page).to have_css "img[src*='budget_execution_no_image']" + end + + it "shows a custom default image when available" do + stub_const("#{SiteCustomization::Image}::VALID_IMAGES", { "budget_execution_no_image" => [260, 80] }) + create(:site_customization_image, + name: "budget_execution_no_image", + image: fixture_file_upload("logo_header-260x80.png")) + + render_inline component + + expect(page).to have_css "img[src$='logo_header-260x80.png']" + expect(page).not_to have_css "img[src*='budget_execution_no_image']" + end + end +end From 11bed74678218805e193abd65c3bbd01fe2a7d04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 3 Aug 2022 15:49:22 +0200 Subject: [PATCH 4/5] Extract constant to configure valid mime types This way it'll be possible to overwrite the valid mime types in a custom model. --- app/models/site_customization/image.rb | 4 +++- spec/models/site_customization/image_spec.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index 185da4a25..d832cf40a 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -11,10 +11,12 @@ class SiteCustomization::Image < ApplicationRecord "logo_email" => [400, 80] }.freeze + VALID_MIME_TYPES = %w[image/jpeg image/png].freeze + has_attachment :image validates :name, presence: true, uniqueness: true, inclusion: { in: ->(*) { VALID_IMAGES.keys }} - validates :image, file_content_type: { allow: ["image/png", "image/jpeg"], if: -> { image.attached? }} + validates :image, file_content_type: { allow: ->(*) { VALID_MIME_TYPES }, if: -> { image.attached? }} validate :check_image def self.all_images diff --git a/spec/models/site_customization/image_spec.rb b/spec/models/site_customization/image_spec.rb index 945046144..55534b535 100644 --- a/spec/models/site_customization/image_spec.rb +++ b/spec/models/site_customization/image_spec.rb @@ -44,4 +44,18 @@ describe SiteCustomization::Image do expect(map).not_to be_valid end end + + it "dynamically validates the valid mime types" do + stub_const("#{SiteCustomization::Image}::VALID_MIME_TYPES", ["image/gif"]) + + gif = build(:site_customization_image, + name: "logo_header", + image: fixture_file_upload("logo_header.gif")) + expect(gif).to be_valid + + png = build(:site_customization_image, + name: "logo_header", + image: fixture_file_upload("logo_header.png")) + expect(png).not_to be_valid + end end From 10cd182774e157d12d2ac49f29ffcf8c04edaa44 Mon Sep 17 00:00:00 2001 From: decabeza Date: Thu, 19 May 2022 20:34:09 +0200 Subject: [PATCH 5/5] Add more images to admin site customization --- app/assets/stylesheets/layout.scss | 2 +- .../budgets/investment_component.html.erb | 2 +- .../budgets/investment_component.rb | 2 +- .../widget/feeds/process_component.html.erb | 2 +- .../widget/feeds/process_component.rb | 1 + app/models/site_customization/image.rb | 13 ++++++++-- app/views/layouts/_common_head.html.erb | 2 +- app/views/layouts/devise.html.erb | 3 ++- .../budgets/investment_component_spec.rb | 23 +++++++++++++++--- .../widget/feeds/process_component_spec.rb | 20 +++++++++++++++ spec/fixtures/files/favicon_custom.ico | Bin 0 -> 1150 bytes spec/system/site_customization/images_spec.rb | 23 ++++++++++++++++++ 12 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 spec/fixtures/files/favicon_custom.ico create mode 100644 spec/system/site_customization/images_spec.rb diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss index 0377aa35f..9ca78ef7f 100644 --- a/app/assets/stylesheets/layout.scss +++ b/app/assets/stylesheets/layout.scss @@ -998,7 +998,7 @@ footer { } .auth-image { - background: $brand image-url("auth_bg.jpg"); + background-color: $brand; background-repeat: no-repeat; background-size: cover; color: $white; diff --git a/app/components/budgets/investment_component.html.erb b/app/components/budgets/investment_component.html.erb index 1b05e2c9b..f3047550e 100644 --- a/app/components/budgets/investment_component.html.erb +++ b/app/components/budgets/investment_component.html.erb @@ -2,7 +2,7 @@ <% if investment.image.present? %> <%= image_tag investment.image.variant(:large), alt: investment.image.title.unicode_normalize %> <% else %> - <%= image_tag "budget_investment_no_image.jpg", alt: investment.title %> + <%= image_tag(image_path_for("budget_investment_no_image.jpg"), alt: investment.title) %> <% end %> <% if investment.should_show_vote_count? || investment.should_show_price? %> diff --git a/app/components/budgets/investment_component.rb b/app/components/budgets/investment_component.rb index e52af44d9..11e258deb 100644 --- a/app/components/budgets/investment_component.rb +++ b/app/components/budgets/investment_component.rb @@ -1,5 +1,5 @@ class Budgets::InvestmentComponent < ApplicationComponent - delegate :locale_and_user_status, :namespaced_budget_investment_path, to: :helpers + delegate :locale_and_user_status, :namespaced_budget_investment_path, :image_path_for, to: :helpers attr_reader :investment def initialize(investment) diff --git a/app/components/widget/feeds/process_component.html.erb b/app/components/widget/feeds/process_component.html.erb index 7bc071a2b..d05d53e3a 100644 --- a/app/components/widget/feeds/process_component.html.erb +++ b/app/components/widget/feeds/process_component.html.erb @@ -1,7 +1,7 @@
<%= link_to url_for(process) do %>
- <%= image_tag("welcome_process.png", alt: "") %> + <%= image_tag(image_path_for("welcome_process.png"), alt: "") %>
<%= t("welcome.feed.process_label") %>

<%= process.title %>

diff --git a/app/components/widget/feeds/process_component.rb b/app/components/widget/feeds/process_component.rb index ab8065663..ab38d0801 100644 --- a/app/components/widget/feeds/process_component.rb +++ b/app/components/widget/feeds/process_component.rb @@ -1,4 +1,5 @@ class Widget::Feeds::ProcessComponent < ApplicationComponent + delegate :image_path_for, to: :helpers attr_reader :process def initialize(process) diff --git a/app/models/site_customization/image.rb b/app/models/site_customization/image.rb index d832cf40a..7d69f74c1 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -6,12 +6,21 @@ class SiteCustomization::Image < ApplicationRecord "social_media_icon" => [470, 246], "social_media_icon_twitter" => [246, 246], "apple-touch-icon-200" => [200, 200], + "auth_bg" => [1280, 1500], "budget_execution_no_image" => [800, 600], + "budget_investment_no_image" => [800, 600], + "favicon" => [16, 16], "map" => [420, 500], - "logo_email" => [400, 80] + "logo_email" => [400, 80], + "welcome_process" => [370, 185] }.freeze - VALID_MIME_TYPES = %w[image/jpeg image/png].freeze + VALID_MIME_TYPES = %w[ + image/jpeg + image/png + image/vnd.microsoft.icon + image/x-icon + ].freeze has_attachment :image diff --git a/app/views/layouts/_common_head.html.erb b/app/views/layouts/_common_head.html.erb index eb64eeced..369c5261e 100644 --- a/app/views/layouts/_common_head.html.erb +++ b/app/views/layouts/_common_head.html.erb @@ -10,4 +10,4 @@ <% end %> <%= javascript_include_tag "application", "data-turbolinks-track" => "reload" %> <%= csrf_meta_tags %> -<%= favicon_link_tag "favicon.ico" %> +<%= favicon_link_tag image_path_for("favicon.ico") %> diff --git a/app/views/layouts/devise.html.erb b/app/views/layouts/devise.html.erb index c781d4403..e9ebac423 100644 --- a/app/views/layouts/devise.html.erb +++ b/app/views/layouts/devise.html.erb @@ -9,7 +9,8 @@ <%= raw setting["html.per_page_code_body"] %>
-
+
)">

<%= 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/investment_component_spec.rb b/spec/components/budgets/investment_component_spec.rb index 7db17ae8d..fe25ac490 100644 --- a/spec/components/budgets/investment_component_spec.rb +++ b/spec/components/budgets/investment_component_spec.rb @@ -11,11 +11,26 @@ describe Budgets::InvestmentComponent do expect(page).to have_css "img[alt='#{investment.image.title}']" end - it "shows the default image when investment has not an image defined" do - investment = create(:budget_investment) - render_inline Budgets::InvestmentComponent.new(investment) + context "investment without an image" do + let(:component) { Budgets::InvestmentComponent.new(create(:budget_investment)) } - expect(page).to have_css "img[src*='budget_investment_no_image']" + it "shows the default image" do + render_inline component + + expect(page).to have_css "img[src*='budget_investment_no_image']" + end + + it "shows a custom default image when available" do + stub_const("#{SiteCustomization::Image}::VALID_IMAGES", { "budget_investment_no_image" => [260, 80] }) + create(:site_customization_image, + name: "budget_investment_no_image", + image: fixture_file_upload("logo_header-260x80.png")) + + render_inline component + + expect(page).to have_css "img[src$='logo_header-260x80.png']" + expect(page).not_to have_css "img[src*='budget_investment_no_image']" + end end it "shows supports count when budget is valuating" do diff --git a/spec/components/widget/feeds/process_component_spec.rb b/spec/components/widget/feeds/process_component_spec.rb index 0f0d2327f..a7be34f17 100644 --- a/spec/components/widget/feeds/process_component_spec.rb +++ b/spec/components/widget/feeds/process_component_spec.rb @@ -20,4 +20,24 @@ describe Widget::Feeds::ProcessComponent do expect(page).to have_css("img[alt='1. No Poverty']") end + + describe "image" do + it "shows the default image" do + render_inline component + + expect(page).to have_css "img[src*='welcome_process']" + end + + it "shows a custom default image when available" do + stub_const("#{SiteCustomization::Image}::VALID_IMAGES", { "welcome_process" => [260, 80] }) + create(:site_customization_image, + name: "welcome_process", + image: fixture_file_upload("logo_header-260x80.png")) + + render_inline component + + expect(page).to have_css "img[src$='logo_header-260x80.png']" + expect(page).not_to have_css "img[src*='welcome_process']" + end + end end diff --git a/spec/fixtures/files/favicon_custom.ico b/spec/fixtures/files/favicon_custom.ico new file mode 100644 index 0000000000000000000000000000000000000000..18704329681d67a60c0beb385dcab5c060143556 GIT binary patch literal 1150 zcmd5)yAgsw5M5?001{)!OzA)(>$uD|sDKj4OfBI`pa26)u!QqHhSg;~B4-?rdF^Izl$&9{_N@ejCNPFG+6s@!_OH$VhBuag7Pf(h}% z0~~8LCs+^g!D!5hPOrexSL+#Dm*YMB9NuHH_3Xp5H*>^{PLGGld7T9~&L542^7q|> zZvdXJt?!uwZUpS!v_JbpYh9h{5j=f$?4#C?8#%1+HZ*$8_lU?mjy;BD{r8L(b^p*p wMyHpqkNdM8yzA8)!kA~L^YaXY`fCKoy{z7aYK+jDC1>SrbAH_yN$DFr0VF` [260, 80] }) + create(:site_customization_image, + name: "auth_bg", + image: fixture_file_upload("logo_header-260x80.png")) + + visit new_user_session_path + + expect(page).to have_css "[style*='background-image:'][style*='logo_header-260x80.png']" + expect(page).not_to have_css "[style*='auth_bg']" + end +end