From c25ab8e4a6b3fda0d14014ad2fc698c59c8debe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 5 Apr 2022 22:28:20 +0200 Subject: [PATCH 1/4] Remove unused vote extensions methods These methods aren't used since commit b98244afd. --- config/initializers/vote_extensions.rb | 12 ------------ spec/models/vote_spec.rb | 26 -------------------------- 2 files changed, 38 deletions(-) diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index fb65d84a1..8a7921a48 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -13,18 +13,6 @@ ActsAsVotable::Vote.class_eval do Comment.public_for_api.pluck(:id)) end - def self.for_debates(debates) - where(votable_type: "Debate", votable_id: debates) - end - - def self.for_proposals(proposals) - where(votable_type: "Proposal", votable_id: proposals) - end - - def self.for_legislation_proposals(proposals) - where(votable_type: "Legislation::Proposal", votable_id: proposals) - end - def self.for_budget_investments(budget_investments = Budget::Investment.all) where(votable_type: "Budget::Investment", votable_id: budget_investments) end diff --git a/spec/models/vote_spec.rb b/spec/models/vote_spec.rb index c22d47fd4..c4f6919d8 100644 --- a/spec/models/vote_spec.rb +++ b/spec/models/vote_spec.rb @@ -1,32 +1,6 @@ require "rails_helper" describe Vote do - describe "#for_debates" do - it "does not returns votes for other votables" do - debate = create(:debate) - create(:vote, votable: create(:comment)) - - expect(Vote.for_debates(debate).count).to eq(0) - end - - it "returns votes only for debates in parameters" do - debate1 = create(:debate, voters: [create(:user)]) - debate2 = create(:debate) - - expect(Vote.for_debates(debate1).count).to eq(1) - expect(Vote.for_debates(debate2).count).to eq(0) - end - - it "accepts more than 1 debate" do - debate1 = create(:debate, voters: [create(:user)]) - debate2 = create(:debate) - debate3 = create(:debate, voters: [create(:user)]) - - expect(Vote.for_debates([debate1, debate2]).count).to eq(1) - expect(Vote.for_debates([debate1, debate3]).count).to eq(2) - end - end - describe "#value" do it "returns vote flag" do vote = create(:vote, vote_flag: true) From 0a3c86b92e9d483256ad6e7f08cf83e98c8318ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 5 Apr 2022 22:44:30 +0200 Subject: [PATCH 2/4] Remove method to get votes for budget investments After commit 0214184b2, this method was only used in two places and was only useful in one of them. IMHO it isn't worth it add a monkey-patch for such a minor usage. --- app/models/user.rb | 4 ++-- config/initializers/vote_extensions.rb | 4 ---- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index afb45baf5..290e47180 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -153,7 +153,7 @@ class User < ApplicationRecord end def voted_in_group?(group) - votes.for_budget_investments(Budget::Investment.where(group: group)).exists? + votes.where(votable: Budget::Investment.where(group: group)).exists? end def headings_voted_within_group(group) @@ -161,7 +161,7 @@ class User < ApplicationRecord end def voted_investments - Budget::Investment.where(id: votes.for_budget_investments.pluck(:votable_id)) + Budget::Investment.where(id: votes.where(votable: Budget::Investment.all).pluck(:votable_id)) end def administrator? diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index 8a7921a48..c7962b47a 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -13,10 +13,6 @@ ActsAsVotable::Vote.class_eval do Comment.public_for_api.pluck(:id)) end - def self.for_budget_investments(budget_investments = Budget::Investment.all) - where(votable_type: "Budget::Investment", votable_id: budget_investments) - end - def value vote_flag end From 5977f2ec3db76bcc47de873cab6ee6bc94b19bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 5 Apr 2022 22:48:42 +0200 Subject: [PATCH 3/4] Simplify methods to get API-public records Just like we did to tags in commit 2e863fdc5. --- app/models/comment.rb | 7 +------ app/models/proposal_notification.rb | 2 +- config/initializers/vote_extensions.rb | 7 +------ 3 files changed, 3 insertions(+), 13 deletions(-) diff --git a/app/models/comment.rb b/app/models/comment.rb index e6f9a0af3..8e7c886a5 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -37,12 +37,7 @@ class Comment < ApplicationRecord scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :public_for_api, -> do not_valuations - .where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or - (comments.commentable_type = 'Proposal' and comments.commentable_id in (?)) or - (comments.commentable_type = 'Poll' and comments.commentable_id in (?))}, - Debate.public_for_api.pluck(:id), - Proposal.public_for_api.pluck(:id), - Poll.public_for_api.pluck(:id)) + .where(commentable: [Debate.public_for_api, Proposal.public_for_api, Poll.public_for_api]) end scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) } diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb index ffed3ab9f..a49dfe9a2 100644 --- a/app/models/proposal_notification.rb +++ b/app/models/proposal_notification.rb @@ -10,7 +10,7 @@ class ProposalNotification < ApplicationRecord validates :proposal, presence: true validate :minimum_interval - scope :public_for_api, -> { where(proposal_id: Proposal.public_for_api.pluck(:id)) } + scope :public_for_api, -> { where(proposal: Proposal.public_for_api) } scope :sort_by_created_at, -> { reorder(created_at: :desc) } scope :sort_by_moderated, -> { reorder(moderated: :desc) } diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index c7962b47a..372c3789f 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -5,12 +5,7 @@ ActsAsVotable::Vote.class_eval do belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment" scope :public_for_api, -> do - where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or - (votes.votable_type = 'Proposal' and votes.votable_id in (?)) or - (votes.votable_type = 'Comment' and votes.votable_id in (?))}, - Debate.public_for_api.pluck(:id), - Proposal.public_for_api.pluck(:id), - Comment.public_for_api.pluck(:id)) + where(votable: [Debate.public_for_api, Proposal.public_for_api, Comment.public_for_api]) end def value From 60579f7e16879a3de77c68294a99f2276218943e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Tue, 5 Apr 2022 23:05:39 +0200 Subject: [PATCH 4/4] Fix typos in user public API methods We were returning an (empty) association of users instead of empty associations of proposals, debates or comments. The code worked because in the end it returned an empty array, but looked weird nevertheless. --- app/models/user.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 290e47180..a54f3874a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -368,15 +368,15 @@ class User < ApplicationRecord delegate :can?, :cannot?, to: :ability def public_proposals - public_activity? ? proposals : User.none + public_activity? ? proposals : proposals.none end def public_debates - public_activity? ? debates : User.none + public_activity? ? debates : debates.none end def public_comments - public_activity? ? comments : User.none + public_activity? ? comments : comments.none end # overwritting of Devise method to allow login using email OR username