From 69dda19af73e318c43a3ab69a6d8ef6a1c0cbf53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 9 Aug 2021 22:32:00 +0200 Subject: [PATCH] Add and apply Rails/PluckId rubocop rule --- .rubocop.yml | 3 +++ app/controllers/admin/stats_controller.rb | 2 +- app/controllers/budgets/investments_controller.rb | 2 +- app/helpers/proposals_dashboard_helper.rb | 2 +- app/models/budget/investment.rb | 12 ++++++------ app/models/concerns/randomizable.rb | 2 +- app/models/dashboard/action.rb | 5 +---- app/models/legislation/question.rb | 4 ++-- app/models/proposal.rb | 2 +- app/models/user.rb | 2 +- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index faedb60ce..74d2640d8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -270,6 +270,9 @@ Rails/OutputSafety: Exclude: - app/helpers/text_with_links_helper.rb +Rails/PluckId: + Enabled: true + Rails/PluralizationGrammar: Enabled: true diff --git a/app/controllers/admin/stats_controller.rb b/app/controllers/admin/stats_controller.rb index 577d90398..5f91ce61f 100644 --- a/app/controllers/admin/stats_controller.rb +++ b/app/controllers/admin/stats_controller.rb @@ -24,7 +24,7 @@ class Admin::StatsController < Admin::BaseController .count(:voter_id) @user_ids_who_didnt_vote_proposals = @verified_users - @user_ids_who_voted_proposals - budgets_ids = Budget.where.not(phase: "finished").pluck(:id) + budgets_ids = Budget.where.not(phase: "finished").ids @budgets = budgets_ids.size @investments = Budget::Investment.where(budget_id: budgets_ids).count end diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index a06e68d1e..7c4101dad 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -44,7 +44,7 @@ module Budgets def index @investments = investments.page(params[:page]).per(PER_PAGE).for_render - @investment_ids = @investments.pluck(:id) + @investment_ids = @investments.ids @investments_map_coordinates = MapLocation.where(investment: investments).map(&:json_data) @tag_cloud = tag_cloud diff --git a/app/helpers/proposals_dashboard_helper.rb b/app/helpers/proposals_dashboard_helper.rb index e96b2a3dd..a73b6d0ff 100644 --- a/app/helpers/proposals_dashboard_helper.rb +++ b/app/helpers/proposals_dashboard_helper.rb @@ -117,7 +117,7 @@ module ProposalsDashboardHelper def new_resources_since_last_login?(resources, new_actions_since_last_login) if resources.present? - resources.pluck(:id).any? { |id| new_actions_since_last_login.include?(id) } + resources.ids.any? { |id| new_actions_since_last_login.include?(id) } end end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index d19ced91a..06d10b5a7 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -164,10 +164,10 @@ class Budget results = results.winners if params[:advanced_filters].include?("winners") ids = [] - ids += results.valuation_finished_feasible.pluck(:id) if params[:advanced_filters].include?("feasible") - ids += results.where(selected: true).pluck(:id) if params[:advanced_filters].include?("selected") - ids += results.undecided.pluck(:id) if params[:advanced_filters].include?("undecided") - ids += results.unfeasible.pluck(:id) if params[:advanced_filters].include?("unfeasible") + ids += results.valuation_finished_feasible.ids if params[:advanced_filters].include?("feasible") + ids += results.where(selected: true).ids if params[:advanced_filters].include?("selected") + ids += results.undecided.ids if params[:advanced_filters].include?("undecided") + ids += results.unfeasible.ids if params[:advanced_filters].include?("unfeasible") results = results.where(id: ids) if ids.any? results end @@ -191,8 +191,8 @@ class Budget return results if max_per_heading <= 0 ids = [] - budget.headings.pluck(:id).each do |hid| - ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).pluck(:id) + budget.headings.ids.each do |hid| + ids += Investment.where(heading_id: hid).order(confidence_score: :desc).limit(max_per_heading).ids end results.where(id: ids) diff --git a/app/models/concerns/randomizable.rb b/app/models/concerns/randomizable.rb index 98a86b426..6b8299368 100644 --- a/app/models/concerns/randomizable.rb +++ b/app/models/concerns/randomizable.rb @@ -3,7 +3,7 @@ module Randomizable class_methods do def sort_by_random(seed = rand(10_000_000)) - ids = order(:id).pluck(:id).shuffle(random: Random.new(seed)) + ids = order(:id).ids.shuffle(random: Random.new(seed)) return all if ids.empty? diff --git a/app/models/dashboard/action.rb b/app/models/dashboard/action.rb index 72340f0b8..f3734c0d0 100644 --- a/app/models/dashboard/action.rb +++ b/app/models/dashboard/action.rb @@ -86,10 +86,7 @@ class Dashboard::Action < ApplicationRecord actions_for_today = get_actions_for_today(proposal) actions_for_date = get_actions_for_date(proposal, date) - actions_for_today_ids = actions_for_today.pluck(:id) - actions_for_date_ids = actions_for_date.pluck(:id) - - actions_for_today_ids - actions_for_date_ids + actions_for_today.ids - actions_for_date.ids end private diff --git a/app/models/legislation/question.rb b/app/models/legislation/question.rb index 73f1cc946..89515d943 100644 --- a/app/models/legislation/question.rb +++ b/app/models/legislation/question.rb @@ -22,11 +22,11 @@ class Legislation::Question < ApplicationRecord scope :sorted, -> { order("id ASC") } def next_question_id - @next_question_id ||= process.questions.where("id > ?", id).sorted.limit(1).pluck(:id).first + @next_question_id ||= process.questions.where("id > ?", id).sorted.limit(1).ids.first end def first_question_id - @first_question_id ||= process.questions.sorted.limit(1).pluck(:id).first + @first_question_id ||= process.questions.sorted.limit(1).ids.first end def answer_for_user(user) diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 9fe469915..18109bbaa 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -114,7 +114,7 @@ class Proposal < ApplicationRecord end def self.not_followed_by_user(user) - where.not(id: followed_by_user(user).pluck(:id)) + where.not(id: followed_by_user(user).ids) end def to_param diff --git a/app/models/user.rb b/app/models/user.rb index 75da22c3b..918667840 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -243,7 +243,7 @@ class User < ApplicationRecord Comment.hide_all comment_ids Proposal.hide_all proposal_ids Budget::Investment.hide_all budget_investment_ids - ProposalNotification.hide_all ProposalNotification.where(author_id: id).pluck(:id) + ProposalNotification.hide_all ProposalNotification.where(author_id: id).ids end def full_restore