From e74eff217bf4f782155d39715399cfc7c75d49d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 20 Jul 2023 02:05:56 +0200 Subject: [PATCH 1/5] Upgrade Ruby to version 3.1.4 Note we updated the `mail` gem in commit 103742847, which is necesary for Ruby 3.1 because it adds the net-smtp dependency. The net-smtp library was removed from Ruby in Ruby 3.1, and if we don't include it, we get an error: ``` cannot load such file -- net/smtp (LoadError) ``` We're also updating the Bundler version in the Gemfile.lock so it's the one included in Ruby 3.1. Without updating it, we get a warning: ``` Calling `DidYouMean::SPELL_CHECKERS.merge!(error_name => spell_checker)' has been deprecated. Please call `DidYouMean.correct_error(error_nam e, spell_checker)' instead. ``` Finally, in order to make Capistrano work, we need to add a couple more changes: * Make the net-ssh gem compatible with SSL 3.0; done in commit b2eec088b * Explicitly allow aliases in the `deploy-secrets.yml` file because Psych 4.x (included in Ruby 3.1) doesn't load aliases without this option --- .gitlab-ci.yml | 4 ++-- .ruby-version | 2 +- Dockerfile | 2 +- Gemfile.lock | 2 +- README.md | 2 +- README_ES.md | 2 +- config/deploy.rb | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 91d0e3c9a..f0253b5ec 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -3,7 +3,7 @@ stages: - lint tests: - image: "ruby:3.0.6" + image: "ruby:3.1.4" stage: test services: - postgres:10.10 @@ -34,7 +34,7 @@ tests: # PRONTO_GITLAB_API_PRIVATE_TOKEN to your repository CI/CD settings # giving it the value of the Personal Access Token linters: - image: "ruby:3.0.6" + image: "ruby:3.1.4" stage: lint cache: key: consul diff --git a/.ruby-version b/.ruby-version index 818bd47ab..0aec50e6e 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -3.0.6 +3.1.4 diff --git a/Dockerfile b/Dockerfile index dee67825a..497ec81fa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM ruby:3.0.6-buster +FROM ruby:3.1.4-buster ENV DEBIAN_FRONTEND noninteractive diff --git a/Gemfile.lock b/Gemfile.lock index ec7840abd..6cc8e2611 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -784,4 +784,4 @@ DEPENDENCIES wkhtmltopdf-binary (~> 0.12.6) BUNDLED WITH - 2.2.33 + 2.3.26 diff --git a/README.md b/README.md index 9dd0f3403..adbdc625b 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ You can access the main website of the project at [http://consuldemocracy.org](h **NOTE**: For more detailed instructions check the [docs](https://docs.consuldemocracy.org) -Prerequisites: install git, Ruby 3.0.6, CMake, pkg-config, shared-mime-info, Node.js and PostgreSQL (>=9.5). +Prerequisites: install git, Ruby 3.1.4, CMake, pkg-config, shared-mime-info, Node.js and PostgreSQL (>=9.5). ```bash git clone https://github.com/consuldemocracy/consuldemocracy.git diff --git a/README_ES.md b/README_ES.md index 5f48d6da0..b82284b0e 100644 --- a/README_ES.md +++ b/README_ES.md @@ -36,7 +36,7 @@ Puedes acceder a la página principal del proyecto en [http://consuldemocracy.or **NOTA**: para unas instrucciones más detalladas consulta la [documentación](https://docs.consuldemocracy.org) -Prerequisitos: tener instalado git, Ruby 3.0.6, CMake, pkg-config, shared-mime-info, Node.js y PostgreSQL (9.5 o superior). +Prerequisitos: tener instalado git, Ruby 3.1.4, CMake, pkg-config, shared-mime-info, Node.js y PostgreSQL (9.5 o superior). ```bash git clone https://github.com/consuldemocracy/consuldemocracy.git diff --git a/config/deploy.rb b/config/deploy.rb index fc60ff594..b2e89a59a 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -2,7 +2,7 @@ lock "~> 3.17.1" def deploysecret(key, default: "") - @deploy_secrets_yml ||= YAML.load_file("config/deploy-secrets.yml")[fetch(:stage).to_s] + @deploy_secrets_yml ||= YAML.load_file("config/deploy-secrets.yml", aliases: true)[fetch(:stage).to_s] @deploy_secrets_yml.fetch(key.to_s, default) end From fd9169e0d6febd7695154a2316cd3f2e54e500d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 20 Jul 2023 03:24:02 +0200 Subject: [PATCH 2/5] Update Style/HashSyntax Rubocop rule Ruby 3.1 adds the option for hash shortcuts, so it's possible to write `{ user: , poll: }` instead of `{ user: user, poll: poll }`. By default, Rubocop expects the new syntax in Ruby 3.1. While right now I absolutely hate this new syntax, we're allowing both the old and the new styles because we might start adopting it once we get used to it. --- .rubocop.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop.yml b/.rubocop.yml index 2dce69504..8e8b05794 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -646,6 +646,7 @@ Style/HashExcept: Style/HashSyntax: Enabled: true + EnforcedShorthandSyntax: either Style/HashTransformKeys: Enabled: true From f87d4b589dc3fb0b3f3e938b1db3f41d1a58520f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 20 Jul 2023 03:42:07 +0200 Subject: [PATCH 3/5] Add and apply Naming/BlockForwarding rubocop rule This syntax has been added in Ruby 3.1. Not using a variable name might not be very descriptive, but it's just as descriptive as using "block" as a variable name. Using just `&` we get the same amount of information than using `&block`: that we're passing a block. We're still using `&action` in `around_action` methods because here we aren't using a generic name for the variable, so (at least for now) we aren't running this cop on controllers using `around_action`. --- .rubocop.yml | 7 +++++++ app/controllers/stats_controller.rb | 4 ++-- app/graphql/types/base_object.rb | 6 +++--- app/helpers/globalize_helper.rb | 4 ++-- app/helpers/translatable_form_helper.rb | 12 ++++++------ app/mailers/mailer.rb | 4 ++-- app/models/budget/stats.rb | 4 ++-- app/models/poll/stats.rb | 4 ++-- app/models/tenant.rb | 4 ++-- .../microsoft/available_locales.rb | 4 ++-- spec/sessions_helper.rb | 4 ++-- 11 files changed, 32 insertions(+), 25 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 8e8b05794..db163bee7 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -319,6 +319,13 @@ Lint/UselessAssignment: Lint/Void: Enabled: true +Naming/BlockForwarding: + Enabled: true + Exclude: + - "app/controllers/application_controller.rb" + - "app/controllers/management/base_controller.rb" + - "app/controllers/subscriptions_controller.rb" + Naming/VariableName: Enabled: true diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 53b19a05e..15cfd7bb8 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -23,7 +23,7 @@ class StatsController < ApplicationController private - def daily_cache(key, &block) - Rails.cache.fetch("public_stats/#{Time.current.strftime("%Y-%m-%d")}/#{key}", &block) + def daily_cache(key, &) + Rails.cache.fetch("public_stats/#{Time.current.strftime("%Y-%m-%d")}/#{key}", &) end end diff --git a/app/graphql/types/base_object.rb b/app/graphql/types/base_object.rb index a86f4ff40..96765f0c9 100644 --- a/app/graphql/types/base_object.rb +++ b/app/graphql/types/base_object.rb @@ -1,7 +1,7 @@ module Types class BaseObject < GraphQL::Schema::Object - def self.field(*args, **kwargs, &block) - super(*args, **kwargs, &block) + def self.field(*args, **kwargs, &) + super(*args, **kwargs, &) # The old api contained non-camelized fields # We want to support these for now, but throw a deprecation warning @@ -14,7 +14,7 @@ module Types if field_name.to_s.include?("_") reason = "Snake case fields are deprecated. Please use #{field_name.to_s.camelize(:lower)}." kwargs = kwargs.merge({ camelize: false, deprecation_reason: reason }) - super(*args, **kwargs, &block) + super(*args, **kwargs, &) end # Make sure associations only return public records diff --git a/app/helpers/globalize_helper.rb b/app/helpers/globalize_helper.rb index 6487c5c7b..b18762550 100644 --- a/app/helpers/globalize_helper.rb +++ b/app/helpers/globalize_helper.rb @@ -130,7 +130,7 @@ module GlobalizeHelper hidden_field_tag("enabled_translations[#{locale}]", (enabled ? 1 : 0)) end - def globalize(locale, &block) - Globalize.with_locale(locale, &block) + def globalize(locale, &) + Globalize.with_locale(locale, &) end end diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb index 1ca593e1d..388ec5a20 100644 --- a/app/helpers/translatable_form_helper.rb +++ b/app/helpers/translatable_form_helper.rb @@ -1,6 +1,6 @@ module TranslatableFormHelper - def translatable_form_for(record, options = {}, &block) - form_for(record, options.merge(builder: TranslatableFormBuilder), &block) + def translatable_form_for(record, options = {}, &) + form_for(record, options.merge(builder: TranslatableFormBuilder), &) end def translations_interface_enabled? @@ -18,13 +18,13 @@ module TranslatableFormHelper class TranslatableFormBuilder < ConsulFormBuilder attr_accessor :translations - def translatable_fields(&block) + def translatable_fields(&) @translations = {} visible_locales.map do |locale| @translations[locale] = translation_for(locale) end safe_join(visible_locales.map do |locale| - Globalize.with_locale(locale) { fields_for_locale(locale, &block) } + Globalize.with_locale(locale) { fields_for_locale(locale, &) } end) end @@ -46,8 +46,8 @@ module TranslatableFormHelper end end - def fields_for_translation(translation, &block) - fields_for(:translations, translation, builder: TranslationsFieldsBuilder, &block) + def fields_for_translation(translation, &) + fields_for(:translations, translation, builder: TranslationsFieldsBuilder, &) end def translation_for(locale) diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 2ac040969..8398bbf6b 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -158,8 +158,8 @@ class Mailer < ApplicationMailer private - def with_user(user, &block) - I18n.with_locale(user.locale, &block) + def with_user(user, &) + I18n.with_locale(user.locale, &) end def prevent_delivery_to_users_without_email diff --git a/app/models/budget/stats.rb b/app/models/budget/stats.rb index 3837bb0f8..7db855aa9 100644 --- a/app/models/budget/stats.rb +++ b/app/models/budget/stats.rb @@ -180,7 +180,7 @@ class Budget::Stats stats_cache(*stats_methods) - def stats_cache(key, &block) - Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &block) + def stats_cache(key, &) + Rails.cache.fetch("budgets_stats/#{budget.id}/#{phases.join}/#{key}/#{version}", &) end end diff --git a/app/models/poll/stats.rb b/app/models/poll/stats.rb index 6b687e3a3..facb27b6d 100644 --- a/app/models/poll/stats.rb +++ b/app/models/poll/stats.rb @@ -117,7 +117,7 @@ class Poll::Stats stats_cache(*stats_methods) - def stats_cache(key, &block) - Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &block) + def stats_cache(key, &) + Rails.cache.fetch("polls_stats/#{poll.id}/#{key}/#{version}", &) end end diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 08757bb57..52803ed9c 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -139,9 +139,9 @@ class Tenant < ApplicationRecord Apartment::Tenant.switch(...) end - def self.run_on_each(&block) + def self.run_on_each(&) ["public"].union(Apartment.tenant_names).each do |schema| - switch(schema, &block) + switch(schema, &) end end diff --git a/lib/remote_translations/microsoft/available_locales.rb b/lib/remote_translations/microsoft/available_locales.rb index 6ddcc8f7a..755af8dbc 100644 --- a/lib/remote_translations/microsoft/available_locales.rb +++ b/lib/remote_translations/microsoft/available_locales.rb @@ -50,7 +50,7 @@ class RemoteTranslations::Microsoft::AvailableLocales JSON.parse(result)["translation"].map(&:first) end - def self.daily_cache(key, &block) - Rails.cache.fetch("remote_available_locales/#{Time.current.strftime("%Y-%m-%d")}/#{key}", &block) + def self.daily_cache(key, &) + Rails.cache.fetch("remote_available_locales/#{Time.current.strftime("%Y-%m-%d")}/#{key}", &) end end diff --git a/spec/sessions_helper.rb b/spec/sessions_helper.rb index 13c9504d0..062b51693 100644 --- a/spec/sessions_helper.rb +++ b/spec/sessions_helper.rb @@ -1,3 +1,3 @@ -def in_browser(name, &block) - Capybara.using_session(name, &block) +def in_browser(name, &) + Capybara.using_session(name, &) end From 584176adfa2a9fa9f39191a33c3b1a9125a18113 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 6 Sep 2023 12:54:01 +0200 Subject: [PATCH 4/5] Add and apply Style/ArrayIntersect rubocop rule The `intersect?` method has been added in Ruby 3.1, and it's more readable than `(a & b).any?`. --- .rubocop.yml | 3 +++ app/helpers/translatable_form_helper.rb | 2 +- app/models/sdg/process_enabled.rb | 2 +- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index db163bee7..ac0daad61 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -627,6 +627,9 @@ Style/AndOr: Style/ArrayCoercion: Enabled: true +Style/ArrayIntersect: + Enabled: true + Style/BlockDelimiters: Enabled: true diff --git a/app/helpers/translatable_form_helper.rb b/app/helpers/translatable_form_helper.rb index 388ec5a20..f77ca0b00 100644 --- a/app/helpers/translatable_form_helper.rb +++ b/app/helpers/translatable_form_helper.rb @@ -8,7 +8,7 @@ module TranslatableFormHelper end def backend_translations_enabled? - (controller.class.module_parents & [Admin, Management, Valuation, SDGManagement]).any? + controller.class.module_parents.intersect?([Admin, Management, Valuation, SDGManagement]) end def highlight_translation_html_class diff --git a/app/models/sdg/process_enabled.rb b/app/models/sdg/process_enabled.rb index 29b567bcf..0378155e2 100644 --- a/app/models/sdg/process_enabled.rb +++ b/app/models/sdg/process_enabled.rb @@ -43,6 +43,6 @@ class SDG::ProcessEnabled def relatable? return true if controller_path_name? - (SDG::Related::RELATABLE_TYPES & [record_or_name.class.name, record_or_name]).any? + SDG::Related::RELATABLE_TYPES.intersect?([record_or_name.class.name, record_or_name]) end end From dcb6450b2390d2beb3d5c47655a2a6fd7e5154a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 8 Sep 2023 22:36:30 +0200 Subject: [PATCH 5/5] Remove no longer needed --keep-file-descriptors option We added this option in commit d17b2523c, but Bundler now keeps descriptors by default. While this behavior was backported to Ruby 3.0.x, we're changing it now because, we've only noticed it now that we're upgrading to Ruby 3.1.x, since it was first developed for that version [1]. [1] https://github.com/rubygems/rubygems/pull/4812/commits/88b7a3e7e2 --- config/puma/defaults.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/config/puma/defaults.rb b/config/puma/defaults.rb index 4f2ad945e..5c1d52ea8 100644 --- a/config/puma/defaults.rb +++ b/config/puma/defaults.rb @@ -18,7 +18,6 @@ threads 0, 16 workers 2 preload_app! -restart_command "bundle exec --keep-file-descriptors puma" plugin :tmp_restart on_restart do