Extract method to get vote counts by votable type

This commit is contained in:
Javi Martín
2023-07-01 22:08:05 +02:00
parent 0d35bddf9e
commit 9491b20314
4 changed files with 21 additions and 7 deletions

View File

@@ -7,9 +7,9 @@ class Admin::StatsController < Admin::BaseController
@proposals = Proposal.with_hidden.count
@comments = Comment.not_valuations.with_hidden.count
@debate_votes = Vote.where(votable_type: "Debate").count
@proposal_votes = Vote.where(votable_type: "Proposal").count
@comment_votes = Vote.where(votable_type: "Comment").count
@debate_votes = Vote.count_for("Debate")
@proposal_votes = Vote.count_for("Proposal")
@comment_votes = Vote.count_for("Comment")
@votes = Vote.count

View File

@@ -11,10 +11,10 @@ class StatsController < ApplicationController
@proposals = daily_cache("proposals") { Proposal.with_hidden.count }
@comments = daily_cache("comments") { Comment.not_valuations.with_hidden.count }
@debate_votes = daily_cache("debate_votes") { Vote.where(votable_type: "Debate").count }
@proposal_votes = daily_cache("proposal_votes") { Vote.where(votable_type: "Proposal").count }
@comment_votes = daily_cache("comment_votes") { Vote.where(votable_type: "Comment").count }
@investment_votes = daily_cache("budget_investment_votes") { Vote.where(votable_type: "Budget::Investment").count }
@debate_votes = daily_cache("debate_votes") { Vote.count_for("Debate") }
@proposal_votes = daily_cache("proposal_votes") { Vote.count_for("Proposal") }
@comment_votes = daily_cache("comment_votes") { Vote.count_for("Comment") }
@investment_votes = daily_cache("budget_investment_votes") { Vote.count_for("Budget::Investment") }
@votes = daily_cache("votes") { Vote.count }
@verified_users = daily_cache("verified_users") { User.with_hidden.level_two_or_three_verified.count }

View File

@@ -8,6 +8,10 @@ ActsAsVotable::Vote.class_eval do
where(votable: [Debate.public_for_api, Proposal.public_for_api, Comment.public_for_api])
end
def self.count_for(votable_type)
where(votable_type: votable_type).count
end
def value
vote_flag
end

View File

@@ -1,6 +1,16 @@
require "rails_helper"
describe Vote do
describe ".count_for" do
it "returns the number of records for a votable type" do
3.times { create(:vote, votable: create(:debate)) }
2.times { create(:vote, votable: create(:proposal)) }
expect(Vote.count_for("Debate")).to eq 3
expect(Vote.count_for("Proposal")).to eq 2
end
end
describe "#value" do
it "returns vote flag" do
vote = create(:vote, vote_flag: true)