From 7f749bb9bb8eefa5113c0c44f9211bfb7f386a16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Fri, 31 Oct 2025 14:31:35 +0100 Subject: [PATCH] Add and apply Style/CollectionQuerying rubocop rule This rule was added in rubocop 1.77. We were following it most of the time. It makes the code more readable in my humble opinion. --- .rubocop.yml | 3 +++ app/components/layout/notification_item_component.rb | 2 +- app/helpers/debates_helper.rb | 2 +- app/lib/poll_option_finder.rb | 4 ++-- app/models/budget/ballot.rb | 2 +- app/models/concerns/globalizable.rb | 2 +- app/models/verification/management/email.rb | 4 ++-- db/seeds.rb | 2 +- lib/tasks/polls.rake | 4 ++-- 9 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 68c7f0077..1f679491b 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -747,6 +747,9 @@ Style/ClassVars: Style/CollectionMethods: Enabled: true +Style/CollectionQuerying: + Enabled: true + Style/ComparableBetween: Enabled: true diff --git a/app/components/layout/notification_item_component.rb b/app/components/layout/notification_item_component.rb index 6b30f4ab4..e408d804a 100644 --- a/app/components/layout/notification_item_component.rb +++ b/app/components/layout/notification_item_component.rb @@ -16,7 +16,7 @@ class Layout::NotificationItemComponent < ApplicationComponent end def notifications_class - if unread_notifications.count > 0 + if unread_notifications.any? "unread-notifications" else "no-notifications" diff --git a/app/helpers/debates_helper.rb b/app/helpers/debates_helper.rb index 4059263c6..f1ee139a5 100644 --- a/app/helpers/debates_helper.rb +++ b/app/helpers/debates_helper.rb @@ -1,6 +1,6 @@ module DebatesHelper def has_featured? - Debate.all.featured.count > 0 + Debate.all.featured.any? end def empty_recommended_debates_message_text(user) diff --git a/app/lib/poll_option_finder.rb b/app/lib/poll_option_finder.rb index 37544926c..cfb7b011e 100644 --- a/app/lib/poll_option_finder.rb +++ b/app/lib/poll_option_finder.rb @@ -6,11 +6,11 @@ class PollOptionFinder end def manageable_choices - choices_map.select { |choice, ids| ids.count == 1 } + choices_map.select { |choice, ids| ids.one? } end def unmanageable_choices - choices_map.reject { |choice, ids| ids.count == 1 } + choices_map.reject { |choice, ids| ids.one? } end private diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index 22ba6b64b..2c8e23e9d 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -37,7 +37,7 @@ class Budget end def has_lines_with_no_heading? - investments.no_heading.count > 0 + investments.no_heading.any? end def has_lines_with_heading? diff --git a/app/models/concerns/globalizable.rb b/app/models/concerns/globalizable.rb index 0ab046c1b..7971c8ae3 100644 --- a/app/models/concerns/globalizable.rb +++ b/app/models/concerns/globalizable.rb @@ -84,7 +84,7 @@ module Globalizable length: options[:length], if: lambda { |translation| translation.locale == Setting.default_locale } end - if options.count > 1 + if options.many? translation_class.instance_eval do validates method, options.reject { |key| key == :length } end diff --git a/app/models/verification/management/email.rb b/app/models/verification/management/email.rb index 311ff1a6d..f1dfe3e9f 100644 --- a/app/models/verification/management/email.rb +++ b/app/models/verification/management/email.rb @@ -42,7 +42,7 @@ class Verification::Management::Email private def validate_user - return if errors.count > 0 + return if errors.any? if !user? errors.add(:email, I18n.t("errors.messages.user_not_found")) @@ -52,7 +52,7 @@ class Verification::Management::Email end def validate_document_number - return if errors.count > 0 + return if errors.any? if document_number_mismatch? errors.add( diff --git a/db/seeds.rb b/db/seeds.rb index a10330b7c..3999f5d5a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,5 @@ # Default admin user (change password after first deploy to a server!) -if Administrator.count == 0 && (!Rails.env.test? || !Tenant.default?) +if Administrator.none? && (!Rails.env.test? || !Tenant.default?) admin = User.create!(username: "admin", email: "admin@consul.dev", password: "12345678", password_confirmation: "12345678", confirmed_at: Time.current, terms_of_service: "1") diff --git a/lib/tasks/polls.rake b/lib/tasks/polls.rake index 2fb3a5adf..3623e8e5e 100644 --- a/lib/tasks/polls.rake +++ b/lib/tasks/polls.rake @@ -94,7 +94,7 @@ namespace :polls do option_finder.unmanageable_choices.each do |choice, ids| tenant_info = " on tenant #{Tenant.current_schema}" unless Tenant.default? - if ids.count == 0 + if ids.none? logger.warn "Skipping poll_answers with the text \"#{choice}\" for the poll_question " \ "with ID #{question.id}. This question has no poll_question_answers " \ "containing the text \"#{choice}\"" + tenant_info.to_s @@ -185,7 +185,7 @@ namespace :polls do option_finder.unmanageable_choices.each do |choice, ids| tenant_info = " on tenant #{Tenant.current_schema}" unless Tenant.default? - if ids.count == 0 + if ids.none? logger.warn "Skipping poll_partial_results with the text \"#{choice}\" for the poll_question " \ "with ID #{question.id}. This question has no poll_question_answers " \ "containing the text \"#{choice}\"" + tenant_info.to_s