From f8716585108d3b3893e12af2e21d9b153dbf89f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 30 Sep 2022 04:01:20 +0200 Subject: [PATCH] Use default_url_options to generate absolute URLs In the dev seeds, we were using `Setting["url"]/proposals`, but we can use `proposals_url` instead, similar to what we do in the rest of the application. We can do a similar thing in the sitemap. This way the sitemap will also work on installations who haven't manually set the "url" setting. Since we aren't using `Setting["url"]` anywhere anymore, we're removing it. This setting was mainly redundant, since we already had the `server_name` in the secrets. Furthermore, `server_name` is automatically configured when running the installer, while `Setting["url"]` had to be manually set in the admin section the application was installed. Note we're using `ActionMailer::Base` setting to generate URLs. Sounds a bit strange, but it's a standard way Rails provides to generate URLs outside the context of a request. --- app/models/setting.rb | 1 - config/locales/en/settings.yml | 2 -- config/locales/es/settings.yml | 2 -- config/sitemap.rb | 2 +- db/dev_seeds/admin_notifications.rb | 5 ++++- db/dev_seeds/settings.rb | 1 - 6 files changed, 5 insertions(+), 8 deletions(-) diff --git a/app/models/setting.rb b/app/models/setting.rb index 9fa1e7d68..58cb8bb06 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -159,7 +159,6 @@ class Setting < ApplicationRecord "twitter_handle": nil, "twitter_hashtag": nil, "youtube_handle": nil, - "url": "http://example.com", # Public-facing URL of the app. # CONSUL installation's organization name "org_name": "CONSUL", "meta_title": nil, diff --git a/config/locales/en/settings.yml b/config/locales/en/settings.yml index fa9287be3..22e600c0b 100644 --- a/config/locales/en/settings.yml +++ b/config/locales/en/settings.yml @@ -40,8 +40,6 @@ en: telegram_handle_description: "If filled in it will appear in the footer" instagram_handle: "Instagram handle" instagram_handle_description: "If filled in it will appear in the footer" - url: "Main URL" - url_description: "Main URL of your website" org_name: "Site name" org_name_description: "This name will appear on mailers subject, help pages..." related_content_score_threshold: "Related content score threshold" diff --git a/config/locales/es/settings.yml b/config/locales/es/settings.yml index 146213632..3aea75100 100644 --- a/config/locales/es/settings.yml +++ b/config/locales/es/settings.yml @@ -40,8 +40,6 @@ es: telegram_handle_description: "Si está rellenado aparecerá en el pie de página" instagram_handle: "Usuario de Instagram" instagram_handle_description: "Si está rellenado aparecerá en el pie de página" - url: "URL general de la web" - url_description: "URL principal de tu web" org_name: "Nombre del sitio" org_name_description: "Este nombre aparecerá en el asunto de emails, páginas de ayuda..." related_content_score_threshold: "Umbral de puntuación de contenido relacionado" diff --git a/config/sitemap.rb b/config/sitemap.rb index 24b0e8ce3..3a2664e55 100644 --- a/config/sitemap.rb +++ b/config/sitemap.rb @@ -9,7 +9,7 @@ SitemapGenerator::Sitemap.namer = SitemapGenerator::SimpleNamer.new(:sitemap, ex # default host SitemapGenerator::Sitemap.verbose = false if Rails.env.test? -SitemapGenerator::Sitemap.default_host = Setting["url"] +SitemapGenerator::Sitemap.default_host = root_url(ActionMailer::Base.default_url_options) # sitemap generator SitemapGenerator::Sitemap.create do diff --git a/db/dev_seeds/admin_notifications.rb b/db/dev_seeds/admin_notifications.rb index 47e2a8702..e75139c7d 100644 --- a/db/dev_seeds/admin_notifications.rb +++ b/db/dev_seeds/admin_notifications.rb @@ -4,7 +4,10 @@ section "Creating Admin Notifications & Templates" do %i[title body].index_with do |attribute| -> { I18n.t("seeds.admin_notifications.proposal.#{attribute}") } end - ).merge(link: "#{Setting["url"]}/proposals", segment_recipient: "administrators") + ).merge( + link: Rails.application.routes.url_helpers.proposals_url(ActionMailer::Base.default_url_options), + segment_recipient: "administrators" + ) ).deliver AdminNotification.create!( diff --git a/db/dev_seeds/settings.rb b/db/dev_seeds/settings.rb index 364773ab6..280e56c63 100644 --- a/db/dev_seeds/settings.rb +++ b/db/dev_seeds/settings.rb @@ -18,7 +18,6 @@ section "Creating Settings" do "telegram_handle": "CONSUL", "twitter_handle": "@consul_dev", "twitter_hashtag": "#consul_dev", - "url": "http://localhost:3000", "votes_for_proposal_success": "100", "youtube_handle": "CONSUL" }.each do |name, value|