From 0d11f2c66a81876814d5326564bb3f3a7a808ac7 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 24 Aug 2022 22:30:21 +0000 Subject: [PATCH 1/5] Bump rubocop from 1.18.4 to 1.35.1 Bumps [rubocop](https://github.com/rubocop/rubocop) from 1.18.4 to 1.35.1. - [Release notes](https://github.com/rubocop/rubocop/releases) - [Changelog](https://github.com/rubocop/rubocop/blob/master/CHANGELOG.md) - [Commits](https://github.com/rubocop/rubocop/compare/v1.18.4...v1.35.1) --- updated-dependencies: - dependency-name: rubocop dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] --- Gemfile | 2 +- Gemfile.lock | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/Gemfile b/Gemfile index 9dd37828e..9d247a509 100644 --- a/Gemfile +++ b/Gemfile @@ -106,7 +106,7 @@ group :development do gem "pronto-eslint", "~> 0.11.0", require: false gem "pronto-rubocop", "~> 0.11.2", require: false gem "pronto-scss", "~> 0.11.0", require: false - gem "rubocop", "~> 1.18.4", require: false + gem "rubocop", "~> 1.35.1", require: false gem "rubocop-performance", "~> 1.11.4", require: false gem "rubocop-rails", "~> 2.15.2", require: false gem "rubocop-rspec", "~> 2.4.0", require: false diff --git a/Gemfile.lock b/Gemfile.lock index 4d2271ec8..248a01c84 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -551,13 +551,14 @@ GEM rspec-mocks (~> 3.10) rspec-support (~> 3.10) rspec-support (3.11.0) - rubocop (1.18.4) + rubocop (1.35.1) + json (~> 2.3) parallel (~> 1.10) - parser (>= 3.0.0.0) + parser (>= 3.1.2.1) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) - rexml - rubocop-ast (>= 1.8.0, < 2.0) + rexml (>= 3.2.5, < 4.0) + rubocop-ast (>= 1.20.1, < 2.0) ruby-progressbar (~> 1.7) unicode-display_width (>= 1.4.0, < 3.0) rubocop-ast (1.21.0) @@ -784,7 +785,7 @@ DEPENDENCIES responders (~> 3.0.1) rinku (~> 2.0.6) rspec-rails (~> 5.1.2) - rubocop (~> 1.18.4) + rubocop (~> 1.35.1) rubocop-performance (~> 1.11.4) rubocop-rails (~> 2.15.2) rubocop-rspec (~> 2.4.0) From f800a02a429782bfcff0192268b6f8f63d86cdd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Aug 2022 21:06:12 +0200 Subject: [PATCH 2/5] Add Layout/LineEndStringConcatenationIndentation rule This rule was added in Rubocop 1.18.0, but we didn't add it back then. Since we're applying it most of the time, we might as well be consistent and apply it everywhere. --- .rubocop.yml | 3 +++ db/dev_seeds/geozones.rb | 20 +++++++++---------- spec/system/admin/administrators_spec.rb | 2 +- .../system/admin/local_census_records_spec.rb | 2 +- spec/system/budgets/investments_spec.rb | 4 ++-- .../sdg_management/local_targets_spec.rb | 2 +- 6 files changed, 18 insertions(+), 15 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 71f0bec9d..94b2b1a4e 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -66,6 +66,9 @@ Layout/IndentationWidth: Layout/LeadingEmptyLines: Enabled: true +Layout/LineEndStringConcatenationIndentation: + Enabled: true + Layout/LineLength: Max: 110 Severity: refactor diff --git a/db/dev_seeds/geozones.rb b/db/dev_seeds/geozones.rb index e8addbe35..98d75d137 100644 --- a/db/dev_seeds/geozones.rb +++ b/db/dev_seeds/geozones.rb @@ -2,23 +2,23 @@ section "Creating Geozones" do Geozone.create!(name: I18n.t("seeds.geozones.north_district"), external_code: "001", census_code: "01", html_map_coordinates: "30,139,45,153,77,148,107,165,138,201,146,218,186,198,216,"\ - "196,233,203,240,215,283,194,329,185,377,184,388,165,369,126,333,113,334,84,320,"\ - "66,286,73,258,65,265,57,249,47,207,58,159,84,108,85,72,101,51,114") + "196,233,203,240,215,283,194,329,185,377,184,388,165,369,126,333,113,334,84,320,"\ + "66,286,73,258,65,265,57,249,47,207,58,159,84,108,85,72,101,51,114") Geozone.create!(name: I18n.t("seeds.geozones.west_district"), external_code: "002", census_code: "02", html_map_coordinates: "42,153,31,176,24,202,20,221,44,235,59,249,55,320,30,354,"\ - "31,372,52,396,64,432,89,453,116,432,149,419,162,412,165,377,172,357,189,352,228,"\ - "327,246,313,262,297,234,291,210,284,193,284,176,294,158,303,154,310,146,289,140,"\ - "268,138,246,135,236,139,222,151,214,136,197,120,179,99,159,85,149,65,149,56,149") + "31,372,52,396,64,432,89,453,116,432,149,419,162,412,165,377,172,357,189,352,228,"\ + "327,246,313,262,297,234,291,210,284,193,284,176,294,158,303,154,310,146,289,140,"\ + "268,138,246,135,236,139,222,151,214,136,197,120,179,99,159,85,149,65,149,56,149") Geozone.create!(name: I18n.t("seeds.geozones.east_district"), external_code: "003", census_code: "03", html_map_coordinates: "175,353,162,378,161,407,153,416,167,432,184,447,225,426,"\ - "250,409,283,390,298,369,344,363,351,334,356,296,361,267,376,245,378,185,327,188,"\ - "281,195,239,216,245,221,245,232,261,244,281,238,300,242,304,251,285,262,278,277,"\ - "267,294,249,312,219,333,198,346,184,353") + "250,409,283,390,298,369,344,363,351,334,356,296,361,267,376,245,378,185,327,188,"\ + "281,195,239,216,245,221,245,232,261,244,281,238,300,242,304,251,285,262,278,277,"\ + "267,294,249,312,219,333,198,346,184,353") Geozone.create!(name: I18n.t("seeds.geozones.central_district"), external_code: "004", census_code: "04", html_map_coordinates: "152,308,137,258,133,235,147,216,152,214,186,194,210,196,"\ - "228,202,240,216,241,232,263,243,293,241,301,245,302,254,286,265,274,278,267,296,"\ - "243,293,226,289,209,285,195,283,177,297") + "228,202,240,216,241,232,263,243,293,241,301,245,302,254,286,265,274,278,267,296,"\ + "243,293,226,289,209,285,195,283,177,297") end diff --git a/spec/system/admin/administrators_spec.rb b/spec/system/admin/administrators_spec.rb index 3bca1971f..10f355938 100644 --- a/spec/system/admin/administrators_spec.rb +++ b/spec/system/admin/administrators_spec.rb @@ -36,7 +36,7 @@ describe "Admin administrators" do visit admin_administrators_path confirmation = "Are you sure? This action will delete "\ - "\"#{user_administrator.name}\" and can't be undone." + "\"#{user_administrator.name}\" and can't be undone." within "#administrator_#{user_administrator.id}" do accept_confirm(confirmation) { click_button "Delete" } diff --git a/spec/system/admin/local_census_records_spec.rb b/spec/system/admin/local_census_records_spec.rb index 1cc24feaf..cf30393b6 100644 --- a/spec/system/admin/local_census_records_spec.rb +++ b/spec/system/admin/local_census_records_spec.rb @@ -140,7 +140,7 @@ describe "Admin local census records", :admin do expect(page).to have_content deleted_document_number confirmation = "Are you sure? This action will delete "\ - "\"#{local_census_record.title}\" and can't be undone." + "\"#{local_census_record.title}\" and can't be undone." accept_confirm(confirmation) { click_on "Delete" } diff --git a/spec/system/budgets/investments_spec.rb b/spec/system/budgets/investments_spec.rb index ba3a20e22..0935d3d73 100644 --- a/spec/system/budgets/investments_spec.rb +++ b/spec/system/budgets/investments_spec.rb @@ -604,7 +604,7 @@ describe "Budget Investments" do fill_in_ckeditor "Description", with: "I want to live in a high tower over the clouds" fill_in "Location additional info", with: "City center" fill_in "If you are proposing in the name of a collective/organization, "\ - "or on behalf of more people, write its name", with: "T.I.A." + "or on behalf of more people, write its name", with: "T.I.A." fill_in "Tags", with: "Towers" check "I agree to the Privacy Policy and the Terms and conditions of use" @@ -669,7 +669,7 @@ describe "Budget Investments" do fill_in_ckeditor "Description", with: "I want to live in a high tower over the clouds" fill_in "Location additional info", with: "City center" fill_in "If you are proposing in the name of a collective/organization, "\ - "or on behalf of more people, write its name", with: "T.I.A." + "or on behalf of more people, write its name", with: "T.I.A." fill_in "Tags", with: "Towers" check "I agree to the Privacy Policy and the Terms and conditions of use" diff --git a/spec/system/sdg_management/local_targets_spec.rb b/spec/system/sdg_management/local_targets_spec.rb index 789f9c810..77d5e6065 100644 --- a/spec/system/sdg_management/local_targets_spec.rb +++ b/spec/system/sdg_management/local_targets_spec.rb @@ -90,7 +90,7 @@ describe "Local Targets" do visit sdg_management_local_targets_path confirmation = "Are you sure? This action will delete "\ - "\"#{sdg_local_target.title}\" and can't be undone." + "\"#{sdg_local_target.title}\" and can't be undone." accept_confirm(confirmation) { click_button "Delete" } From 5ec7f4a3391505d9e1192e7f4c9a83bdd6e08442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Aug 2022 22:15:53 +0200 Subject: [PATCH 3/5] Add and apply FileRead and FileWrite rubocop rules They were added in Rubocop 1.24.0. Even if we were already applying FileRead everywhere, this is something we've manually fixed in the past. Another reason to add it is that these rules are deeply related. --- .rubocop.yml | 6 ++++++ app/models/concerns/json_exporter.rb | 2 +- spec/system/admin/machine_learning_spec.rb | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 94b2b1a4e..d8b0e5bc2 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -485,6 +485,12 @@ Style/ClassVars: Style/CollectionMethods: Enabled: true +Style/FileRead: + Enabled: true + +Style/FileWrite: + Enabled: true + Style/HashConversion: Enabled: true diff --git a/app/models/concerns/json_exporter.rb b/app/models/concerns/json_exporter.rb index b06ce42d2..171200fc6 100644 --- a/app/models/concerns/json_exporter.rb +++ b/app/models/concerns/json_exporter.rb @@ -2,7 +2,7 @@ module JsonExporter def to_json_file(filename) data = [] model.find_each { |record| data << json_values(record) } - File.open(filename, "w") { |file| file.write(data.to_json) } + File.write(filename, data.to_json) end private diff --git a/spec/system/admin/machine_learning_spec.rb b/spec/system/admin/machine_learning_spec.rb index 32a59f281..03a476026 100644 --- a/spec/system/admin/machine_learning_spec.rb +++ b/spec/system/admin/machine_learning_spec.rb @@ -216,9 +216,9 @@ describe "Machine learning" do kind: "comments_summary", updated_at: 2.minutes.from_now) comments_file = MachineLearning::DATA_FOLDER.join(MachineLearning.comments_filename) - File.open(comments_file, "w") { |file| file.write([].to_json) } + File.write(comments_file, [].to_json) proposals_comments_summary_file = MachineLearning::DATA_FOLDER.join(MachineLearning.proposals_comments_summary_filename) - File.open(proposals_comments_summary_file, "w") { |file| file.write([].to_json) } + File.write(proposals_comments_summary_file, [].to_json) end visit admin_machine_learning_path From 4a851c0d820160ed4f498a4e7ea3ea833f92b7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Aug 2022 22:24:36 +0200 Subject: [PATCH 4/5] Add and apply Style/MapToHash rubocop rule This rule was added in Rubocop 1.24.0. Applying it slightly simplifies the code. --- .rubocop.yml | 3 +++ app/helpers/settings_helper.rb | 2 +- app/models/concerns/statisticable.rb | 8 ++++---- app/models/i18n_content.rb | 4 ++-- lib/user_segments.rb | 4 ++-- spec/system/admin/translatable_spec.rb | 4 ++-- spec/system/translatable_spec.rb | 4 ++-- 7 files changed, 16 insertions(+), 13 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index d8b0e5bc2..7e9941d1d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -509,6 +509,9 @@ Style/IdenticalConditionalBranches: Style/IfWithBooleanLiteralBranches: Enabled: true +Style/MapToHash: + Enabled: true + Style/MethodDefParentheses: Enabled: true diff --git a/app/helpers/settings_helper.rb b/app/helpers/settings_helper.rb index e82330676..a37d9299e 100644 --- a/app/helpers/settings_helper.rb +++ b/app/helpers/settings_helper.rb @@ -13,7 +13,7 @@ module SettingsHelper end def setting - @all_settings ||= Setting.all.map { |s| [s.key, s.value.presence] }.to_h + @all_settings ||= Setting.all.to_h { |s| [s.key, s.value.presence] } end def display_setting_name(setting_name) diff --git a/app/models/concerns/statisticable.rb b/app/models/concerns/statisticable.rb index 83ab583f8..790b01b3e 100644 --- a/app/models/concerns/statisticable.rb +++ b/app/models/concerns/statisticable.rb @@ -95,7 +95,7 @@ module Statisticable end def participants_by_age - age_groups.map do |start, finish| + age_groups.to_h do |start, finish| count = participants.between_ages(start, finish).count [ @@ -106,11 +106,11 @@ module Statisticable percentage: calculate_percentage(count, total_participants) } ] - end.to_h + end end def participants_by_geozone - geozone_stats.map do |stats| + geozone_stats.to_h do |stats| [ stats.name, { @@ -118,7 +118,7 @@ module Statisticable percentage: stats.percentage } ] - end.to_h + end end def calculate_percentage(fraction, total) diff --git a/app/models/i18n_content.rb b/app/models/i18n_content.rb index de4edc172..c17a7dee2 100644 --- a/app/models/i18n_content.rb +++ b/app/models/i18n_content.rb @@ -113,9 +113,9 @@ class I18nContent < ApplicationRecord def self.translations_hash(locale) Rails.cache.fetch(translation_class.where(locale: locale)) do - all.map do |content| + all.to_h do |content| [content.key, translation_class.find_by(i18n_content_id: content, locale: locale)&.value] - end.to_h + end end end diff --git a/lib/user_segments.rb b/lib/user_segments.rb index 7e4e8a154..55fab74be 100644 --- a/lib/user_segments.rb +++ b/lib/user_segments.rb @@ -84,9 +84,9 @@ class UserSegments end def self.geozones - Geozone.order(:name).map do |geozone| + Geozone.order(:name).to_h do |geozone| [geozone.name.gsub(/./) { |char| character_approximation(char) }.underscore.tr(" ", "_"), geozone] - end.to_h + end end def self.character_approximation(char) diff --git a/spec/system/admin/translatable_spec.rb b/spec/system/admin/translatable_spec.rb index ca81e8e39..389e0116b 100644 --- a/spec/system/admin/translatable_spec.rb +++ b/spec/system/admin/translatable_spec.rb @@ -9,9 +9,9 @@ describe "Admin edit translatable records", :admin do let(:fields) { translatable.translated_attribute_names } let(:attributes) do - fields.product(%i[en es]).map do |field, locale| + fields.product(%i[en es]).to_h do |field, locale| [:"#{field}_#{locale}", text_for(field, locale)] - end.to_h + end end context "Add a translation" do diff --git a/spec/system/translatable_spec.rb b/spec/system/translatable_spec.rb index 52ecdfddb..d502fbd4a 100644 --- a/spec/system/translatable_spec.rb +++ b/spec/system/translatable_spec.rb @@ -182,9 +182,9 @@ describe "Public area translatable records" do before { translatable.update(attributes.merge(author: user)) } let(:attributes) do - translatable.translated_attribute_names.product(%i[en es]).map do |field, locale| + translatable.translated_attribute_names.product(%i[en es]).to_h do |field, locale| [:"#{field}_#{locale}", text_for(field, locale)] - end.to_h + end end context "Update a translation" do From 0d4a032f525b082e656891c598c3bacfa302789f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 25 Aug 2022 23:02:33 +0200 Subject: [PATCH 5/5] Add and apply Lint/NonAtomicFileOperation rule This rule was added in Rubocop 1.31.0; it follows the principles mentioned in the Ruby Style Guide [1]. https://rubystyle.guide/#atomic-file-operations --- .rubocop.yml | 3 +++ spec/lib/tasks/sitemap_spec.rb | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.rubocop.yml b/.rubocop.yml index 7e9941d1d..d44255479 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -179,6 +179,9 @@ Lint/EmptyFile: Lint/LiteralAsCondition: Enabled: true +Lint/NonAtomicFileOperation: + Enabled: true + Lint/ParenthesesAsGroupedExpression: Enabled: true diff --git a/spec/lib/tasks/sitemap_spec.rb b/spec/lib/tasks/sitemap_spec.rb index d72bb9061..dc2fa7edd 100644 --- a/spec/lib/tasks/sitemap_spec.rb +++ b/spec/lib/tasks/sitemap_spec.rb @@ -4,7 +4,7 @@ describe "rake sitemap:create", type: :system do let(:file) { Rails.root.join("public", "sitemap.xml") } before do - File.delete(file) if File.exist?(file) + FileUtils.rm_f(file) Rake::Task["sitemap:create"].reenable end