diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 588bb83f9..793f6e867 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -61,8 +61,12 @@ def rss_feed auto_discovery_link_tag :rss, events_path(format: :xml), title: 'Event-Feed' end + def label_logo_asset(label_id = Whitelabel[:label_id]) + asset_with_fallback("labels/#{label_id}.png", fallback: 'logo.png') + end + def icon(type) - path = image_path Whitelabel.label ? "labels/#{Whitelabel[:label_id]}.ico" : 'favicon.ico' + path = image_path favicon_asset tag.link rel: type, href: path end @@ -93,6 +97,26 @@ def user_name(user) private + def favicon_asset + return 'favicon.ico' unless Whitelabel.label + + asset_with_fallback("labels/#{Whitelabel[:label_id]}.ico", fallback: 'favicon.ico') + end + + def asset_with_fallback(logical_path, fallback:) + return logical_path if asset_file_exists?(logical_path) + + fallback + end + + def asset_file_exists?(logical_path) + [ + Rails.root.join('app/assets/images', logical_path), + Rails.root.join('public', logical_path), + Rails.root.join('public/images', logical_path) + ].any?(&:exist?) + end + def markdown_parser @markdown_parser ||= Redcarpet::Markdown.new Redcarpet::Render::Safe, autolink: true, space_after_headers: true end diff --git a/app/views/application/_headline.slim b/app/views/application/_headline.slim index 701505829..463ca7ffc 100644 --- a/app/views/application/_headline.slim +++ b/app/views/application/_headline.slim @@ -7,7 +7,7 @@ header.d-none.d-xl-block - else .col-lg-2.text-center = link_to(root_path, title: title) do - =image_tag("labels/#{Whitelabel[:label_id]}.png", title: title, id: :logo) + = image_tag(label_logo_asset, title: title, id: :logo) .col-lg-10 = link_to(root_path, id: :title, title: title) do diff --git a/app/views/application/_nav.slim b/app/views/application/_nav.slim index 4fc9c9243..0d56833f1 100644 --- a/app/views/application/_nav.slim +++ b/app/views/application/_nav.slim @@ -1,7 +1,7 @@ nav.navbar.sticky-top.navbar-expand-lg.navbar-light.bg-light#nav .label.navbar-brand = link_to(root_path(anchor: :on_ruby), class: 'navbar-brand ps-4') do - = image_tag("labels/#{Whitelabel[:label_id]}.png", title: title, class: "d-inline-block align-bottom label") + = image_tag(label_logo_asset, title: title, class: "d-inline-block align-bottom label") .d-none.d-sm-inline.d-md-inline.d-lg-none= I18n.tw('name') button.navbar-toggler.border-0(type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation") diff --git a/app/views/labels/index.slim b/app/views/labels/index.slim index 57fa61fbe..2c6ecbdad 100644 --- a/app/views/labels/index.slim +++ b/app/views/labels/index.slim @@ -9,7 +9,7 @@ section.mt-4 - subtitle = t("label.#{label.label_id}.subtitle") .card .text-center - span.image= image_tag("labels/#{label.label_id}.png", alt: name, style: "max-height:100px;padding-top:10px") + span.image= image_tag(label_logo_asset(label.label_id), alt: name, style: "max-height:100px;padding-top:10px") .card-body h5.card-title= title diff --git a/config/application.rb b/config/application.rb index acb6fadf5..721605f35 100644 --- a/config/application.rb +++ b/config/application.rb @@ -20,7 +20,7 @@ module OnRuby class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. - config.load_defaults 5.0 + config.load_defaults 6.1 # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers diff --git a/config/initializers/new_framework_defaults_5_2.rb b/config/initializers/new_framework_defaults_5_2.rb deleted file mode 100644 index c383d072b..000000000 --- a/config/initializers/new_framework_defaults_5_2.rb +++ /dev/null @@ -1,38 +0,0 @@ -# Be sure to restart your server when you modify this file. -# -# This file contains migration options to ease your Rails 5.2 upgrade. -# -# Once upgraded flip defaults one by one to migrate to the new default. -# -# Read the Guide for Upgrading Ruby on Rails for more info on each option. - -# Make Active Record use stable #cache_key alongside new #cache_version method. -# This is needed for recyclable cache keys. -# Rails.application.config.active_record.cache_versioning = true - -# Use AES-256-GCM authenticated encryption for encrypted cookies. -# Also, embed cookie expiry in signed or encrypted cookies for increased security. -# -# This option is not backwards compatible with earlier Rails versions. -# It's best enabled when your entire app is migrated and stable on 5.2. -# -# Existing cookies will be converted on read then written with the new scheme. -# Rails.application.config.action_dispatch.use_authenticated_cookie_encryption = true - -# Use AES-256-GCM authenticated encryption as default cipher for encrypting messages -# instead of AES-256-CBC, when use_authenticated_message_encryption is set to true. -# Rails.application.config.active_support.use_authenticated_message_encryption = true - -# Add default protection from forgery to ActionController::Base instead of in -# ApplicationController. -# Rails.application.config.action_controller.default_protect_from_forgery = true - -# Store boolean values are in sqlite3 databases as 1 and 0 instead of 't' and -# 'f' after migrating old data. -# Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true - -# Use SHA-1 instead of MD5 to generate non-sensitive digests, such as the ETag header. -# Rails.application.config.active_support.use_sha1_digests = true - -# Make `form_with` generate id attributes for any generated HTML tags. -# Rails.application.config.action_view.form_with_generates_ids = true diff --git a/config/initializers/new_framework_defaults_6_0.rb b/config/initializers/new_framework_defaults_6_0.rb deleted file mode 100644 index 92240ef5f..000000000 --- a/config/initializers/new_framework_defaults_6_0.rb +++ /dev/null @@ -1,45 +0,0 @@ -# Be sure to restart your server when you modify this file. -# -# This file contains migration options to ease your Rails 6.0 upgrade. -# -# Once upgraded flip defaults one by one to migrate to the new default. -# -# Read the Guide for Upgrading Ruby on Rails for more info on each option. - -# Don't force requests from old versions of IE to be UTF-8 encoded. -# Rails.application.config.action_view.default_enforce_utf8 = false - -# Embed purpose and expiry metadata inside signed and encrypted -# cookies for increased security. -# -# This option is not backwards compatible with earlier Rails versions. -# It's best enabled when your entire app is migrated and stable on 6.0. -# Rails.application.config.action_dispatch.use_cookies_with_metadata = true - -# Change the return value of `ActionDispatch::Response#content_type` to Content-Type header without modification. -# Rails.application.config.action_dispatch.return_only_media_type_on_content_type = false - -# Return false instead of self when enqueuing is aborted from a callback. -# Rails.application.config.active_job.return_false_on_aborted_enqueue = true - -# Send Active Storage analysis and purge jobs to dedicated queues. -# Rails.application.config.active_storage.queues.analysis = :active_storage_analysis -# Rails.application.config.active_storage.queues.purge = :active_storage_purge - -# When assigning to a collection of attachments declared via `has_many_attached`, replace existing -# attachments instead of appending. Use #attach to add new attachments without replacing existing ones. -# Rails.application.config.active_storage.replace_on_assign_to_many = true - -# Use ActionMailer::MailDeliveryJob for sending parameterized and normal mail. -# -# The default delivery jobs (ActionMailer::Parameterized::DeliveryJob, ActionMailer::DeliveryJob), -# will be removed in Rails 6.1. This setting is not backwards compatible with earlier Rails versions. -# If you send mail in the background, job workers need to have a copy of -# MailDeliveryJob to ensure all delivery jobs are processed properly. -# Make sure your entire app is migrated and stable on 6.0 before using this setting. -# Rails.application.config.action_mailer.delivery_job = "ActionMailer::MailDeliveryJob" - -# Enable the same cache key to be reused when the object being cached of type -# `ActiveRecord::Relation` changes by moving the volatile information (max updated at and count) -# of the relation's cache key into the cache version to support recycling cache key. -# Rails.application.config.active_record.collection_cache_versioning = true diff --git a/config/initializers/new_framework_defaults_6_1.rb b/config/initializers/new_framework_defaults_6_1.rb deleted file mode 100644 index 9526b835a..000000000 --- a/config/initializers/new_framework_defaults_6_1.rb +++ /dev/null @@ -1,67 +0,0 @@ -# Be sure to restart your server when you modify this file. -# -# This file contains migration options to ease your Rails 6.1 upgrade. -# -# Once upgraded flip defaults one by one to migrate to the new default. -# -# Read the Guide for Upgrading Ruby on Rails for more info on each option. - -# Support for inversing belongs_to -> has_many Active Record associations. -# Rails.application.config.active_record.has_many_inversing = true - -# Track Active Storage variants in the database. -# Rails.application.config.active_storage.track_variants = true - -# Apply random variation to the delay when retrying failed jobs. -# Rails.application.config.active_job.retry_jitter = 0.15 - -# Stop executing `after_enqueue`/`after_perform` callbacks if -# `before_enqueue`/`before_perform` respectively halts with `throw :abort`. -# Rails.application.config.active_job.skip_after_callbacks_if_terminated = true - -# Specify cookies SameSite protection level: either :none, :lax, or :strict. -# -# This change is not backwards compatible with earlier Rails versions. -# It's best enabled when your entire app is migrated and stable on 6.1. -# Rails.application.config.action_dispatch.cookies_same_site_protection = :lax - -# Generate CSRF tokens that are encoded in URL-safe Base64. -# -# This change is not backwards compatible with earlier Rails versions. -# It's best enabled when your entire app is migrated and stable on 6.1. -# Rails.application.config.action_controller.urlsafe_csrf_tokens = true - -# Specify whether `ActiveSupport::TimeZone.utc_to_local` returns a time with an -# UTC offset or a UTC time. -# ActiveSupport.utc_to_local_returns_utc_offset_times = true - -# Change the default HTTP status code to `308` when redirecting non-GET/HEAD -# requests to HTTPS in `ActionDispatch::SSL` middleware. -# Rails.application.config.action_dispatch.ssl_default_redirect_status = 308 - -# Use new connection handling API. For most applications this won't have any -# effect. For applications using multiple databases, this new API provides -# support for granular connection swapping. -# Rails.application.config.active_record.legacy_connection_handling = false - -# Make `form_with` generate non-remote forms by default. -# Rails.application.config.action_view.form_with_generates_remote_forms = false - -# Set the default queue name for the analysis job to the queue adapter default. -# Rails.application.config.active_storage.queues.analysis = nil - -# Set the default queue name for the purge job to the queue adapter default. -# Rails.application.config.active_storage.queues.purge = nil - -# Set the default queue name for the incineration job to the queue adapter default. -# Rails.application.config.action_mailbox.queues.incineration = nil - -# Set the default queue name for the routing job to the queue adapter default. -# Rails.application.config.action_mailbox.queues.routing = nil - -# Set the default queue name for the mail deliver job to the queue adapter default. -# Rails.application.config.action_mailer.deliver_later_queue_name = nil - -# Generate a `Link` header that gives a hint to modern browsers about -# preloading assets when using `javascript_include_tag` and `stylesheet_link_tag`. -# Rails.application.config.action_view.preload_links_header = true diff --git a/spec/controllers/sessions_controller_spec.rb b/spec/controllers/sessions_controller_spec.rb index 5cb69a294..e9ee1194a 100644 --- a/spec/controllers/sessions_controller_spec.rb +++ b/spec/controllers/sessions_controller_spec.rb @@ -45,20 +45,20 @@ context 'POST :email_login' do it 'sends the email and redirect to index', :aggregate_failures do expect { post :email_login, params: { email: 'user@example.org' } } - .to have_enqueued_job(ActionMailer::DeliveryJob) + .to have_enqueued_job(ActionMailer::MailDeliveryJob) expect(response).to redirect_to(root_path) end it 'does not send the email if param missing', :aggregate_failures do expect { post :email_login } - .not_to have_enqueued_job(ActionMailer::DeliveryJob) + .not_to have_enqueued_job(ActionMailer::MailDeliveryJob) expect(response).to have_http_status(:unprocessable_entity) end it 'does not send the email looks bad', :aggregate_failures do expect { post :email_login, params: { email: 'user@org' } } - .not_to have_enqueued_job(ActionMailer::DeliveryJob) + .not_to have_enqueued_job(ActionMailer::MailDeliveryJob) expect(response).to have_http_status(:unprocessable_entity) end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index 837b3131b..6b0827982 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -15,4 +15,30 @@ .to include('<script>alert("xss");</script>') end end + + describe '#browser_icon' do + it 'falls back to the default favicon when the whitelabel icon is missing' do + allow(Whitelabel).to receive(:label).and_return(instance_double(Whitelabel)) + allow(Whitelabel).to receive(:[]).with(:label_id).and_return('tokio') + + expect(helper.browser_icon).to include('href="/assets/favicon') + end + + it 'uses the whitelabel favicon when it exists' do + allow(Whitelabel).to receive(:label).and_return(instance_double(Whitelabel)) + allow(Whitelabel).to receive(:[]).with(:label_id).and_return('berlin') + + expect(helper.browser_icon).to include('labels/berlin') + end + end + + describe '#label_logo_asset' do + it 'falls back to the default logo when the whitelabel logo is missing' do + expect(helper.label_logo_asset('tokio')).to eq('logo.png') + end + + it 'uses the whitelabel logo when it exists' do + expect(helper.label_logo_asset('berlin')).to eq('labels/berlin.png') + end + end end