Merge pull request #4807 from consul/api_votes

Remove and simplify API and votes-related code
This commit is contained in:
Javi Martín
2022-05-02 18:24:54 +02:00
committed by GitHub
5 changed files with 8 additions and 60 deletions

View File

@@ -37,12 +37,7 @@ class Comment < ApplicationRecord
scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) } scope :sort_by_flags, -> { order(flags_count: :desc, updated_at: :desc) }
scope :public_for_api, -> do scope :public_for_api, -> do
not_valuations not_valuations
.where(%{(comments.commentable_type = 'Debate' and comments.commentable_id in (?)) or .where(commentable: [Debate.public_for_api, Proposal.public_for_api, Poll.public_for_api])
(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))
end end
scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) } scope :sort_by_most_voted, -> { order(confidence_score: :desc, created_at: :desc) }

View File

@@ -10,7 +10,7 @@ class ProposalNotification < ApplicationRecord
validates :proposal, presence: true validates :proposal, presence: true
validate :minimum_interval 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_created_at, -> { reorder(created_at: :desc) }
scope :sort_by_moderated, -> { reorder(moderated: :desc) } scope :sort_by_moderated, -> { reorder(moderated: :desc) }

View File

@@ -153,7 +153,7 @@ class User < ApplicationRecord
end end
def voted_in_group?(group) 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 end
def headings_voted_within_group(group) def headings_voted_within_group(group)
@@ -161,7 +161,7 @@ class User < ApplicationRecord
end end
def voted_investments 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 end
def administrator? def administrator?
@@ -368,15 +368,15 @@ class User < ApplicationRecord
delegate :can?, :cannot?, to: :ability delegate :can?, :cannot?, to: :ability
def public_proposals def public_proposals
public_activity? ? proposals : User.none public_activity? ? proposals : proposals.none
end end
def public_debates def public_debates
public_activity? ? debates : User.none public_activity? ? debates : debates.none
end end
def public_comments def public_comments
public_activity? ? comments : User.none public_activity? ? comments : comments.none
end end
# overwritting of Devise method to allow login using email OR username # overwritting of Devise method to allow login using email OR username

View File

@@ -5,28 +5,7 @@ ActsAsVotable::Vote.class_eval do
belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment" belongs_to :budget_investment, foreign_key: "votable_id", class_name: "Budget::Investment"
scope :public_for_api, -> do scope :public_for_api, -> do
where(%{(votes.votable_type = 'Debate' and votes.votable_id in (?)) or where(votable: [Debate.public_for_api, Proposal.public_for_api, Comment.public_for_api])
(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))
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 end
def value def value

View File

@@ -1,32 +1,6 @@
require "rails_helper" require "rails_helper"
describe Vote do 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 describe "#value" do
it "returns vote flag" do it "returns vote flag" do
vote = create(:vote, vote_flag: true) vote = create(:vote, vote_flag: true)