From 86cf674d0c91cd58749e2d5cc9664cfe8ed5e281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 2 Mar 2024 01:22:19 +0100 Subject: [PATCH 1/2] Add Rails/DeprecatedActiveModelErrorsMethods rule Even though we're already applying this rule since commit 08b12a78f, it's very useful to have it so we don't accidentally introduce code that won't work with Rails 7. After upgrading to Rails 7, this rule will no longer be necessary, since the code using the deprecated syntax will not work and so we'll notice it immediately. --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index 3d64964eb..488cd99fe 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -377,6 +377,9 @@ Rails/CreateTableWithTimestamps: Rails/Date: Enabled: true +Rails/DeprecatedActiveModelErrorsMethods: # TODO: Remove after upgrading to Rails 7 + Enabled: true + Rails/DurationArithmetic: Enabled: true From d0fae3377e87d74f9f8be8f071cc894c4f3d9998 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sat, 2 Mar 2024 01:29:45 +0100 Subject: [PATCH 2/2] Add and apply Rails/WhereMissing rubocop rule So now we know where to use the `where.missing` method which was introduced in Rails 6.1. Note this rule didn't detect all cases where the new method can be used. --- .rubocop.yml | 3 +++ app/models/concerns/sdg/relatable.rb | 2 +- app/models/poll/question/answer.rb | 6 +++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 488cd99fe..11a007c5d 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -510,6 +510,9 @@ Rails/Validation: Rails/WhereEquals: Enabled: true +Rails/WhereMissing: + Enabled: true + Rails/WhereNot: Enabled: true diff --git a/app/models/concerns/sdg/relatable.rb b/app/models/concerns/sdg/relatable.rb index 63ce917ef..56d3cd86e 100644 --- a/app/models/concerns/sdg/relatable.rb +++ b/app/models/concerns/sdg/relatable.rb @@ -44,7 +44,7 @@ module SDG::Relatable end def pending_sdg_review - left_joins(:sdg_review).merge(SDG::Review.where(id: nil)) + where.missing(:sdg_review) end end diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 57aba5609..a41f9e4a9 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -17,9 +17,9 @@ class Poll::Question::Answer < ApplicationRecord scope :with_content, -> { where.not(id: without_content) } scope :without_content, -> do where(description: "") - .left_joins(:images).where(images: { id: nil }) - .left_joins(:documents).where(documents: { id: nil }) - .left_joins(:videos).where(poll_question_answer_videos: { id: nil }) + .where.missing(:images) + .where.missing(:documents) + .where.missing(:videos) end def self.order_answers(ordered_array)