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/views/budgets/executions/_image.html.erb b/app/components/budgets/executions/image_component.html.erb similarity index 65% rename from app/views/budgets/executions/_image.html.erb rename to app/components/budgets/executions/image_component.html.erb index ebe933504..df3709290 100644 --- a/app/views/budgets/executions/_image.html.erb +++ b/app/components/budgets/executions/image_component.html.erb @@ -1,9 +1,7 @@ -<% milestone = first_milestone_with_image(investment) %> - <% if milestone&.image.present? %> <%= image_tag milestone.image.variant(:large), alt: milestone.image.title %> <% 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 new file mode 100644 index 000000000..3058ee46d --- /dev/null +++ b/app/components/budgets/executions/image_component.rb @@ -0,0 +1,15 @@ +class Budgets::Executions::ImageComponent < ApplicationComponent + attr_reader :investment + delegate :image_path_for, to: :helpers + + 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/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/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/models/site_customization/image.rb b/app/models/site_customization/image.rb index 185da4a25..7d69f74c1 100644 --- a/app/models/site_customization/image.rb +++ b/app/models/site_customization/image.rb @@ -6,15 +6,26 @@ 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 + image/vnd.microsoft.icon + image/x-icon + ].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/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 %>
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/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", 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/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 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 000000000..187043296 Binary files /dev/null and b/spec/fixtures/files/favicon_custom.ico differ 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 diff --git a/spec/system/site_customization/images_spec.rb b/spec/system/site_customization/images_spec.rb new file mode 100644 index 000000000..4674eaf1d --- /dev/null +++ b/spec/system/site_customization/images_spec.rb @@ -0,0 +1,23 @@ +require "rails_helper" + +describe "Site customization images" do + scenario "Custom favicon" do + create(:site_customization_image, name: "favicon", image: fixture_file_upload("favicon_custom.ico")) + + visit root_path + + expect(page).to have_css("link[rel='shortcut icon'][href$='favicon_custom.ico']", visible: :hidden) + end + + scenario "Custom auth background" do + stub_const("#{SiteCustomization::Image}::VALID_IMAGES", { "auth_bg" => [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