<% end %>
- <% if voted_for?(legislation_proposal_votes, proposal) && setting["twitter_handle"] %>
+ <% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %>
<%= render "shared/social_share",
title: proposal.title,
diff --git a/app/components/legislation/proposals/votes_component.rb b/app/components/legislation/proposals/votes_component.rb
index 4c5663e29..0dabb29ec 100644
--- a/app/components/legislation/proposals/votes_component.rb
+++ b/app/components/legislation/proposals/votes_component.rb
@@ -1,9 +1,8 @@
class Legislation::Proposals::VotesComponent < ApplicationComponent
- attr_reader :proposal, :legislation_proposal_votes
- delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, :votes_percentage, to: :helpers
+ attr_reader :proposal
+ delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :votes_percentage, to: :helpers
- def initialize(proposal, legislation_proposal_votes:)
+ def initialize(proposal)
@proposal = proposal
- @legislation_proposal_votes = legislation_proposal_votes
end
end
diff --git a/app/components/proposals/votes_component.html.erb b/app/components/proposals/votes_component.html.erb
index e38091ff5..1834cc094 100644
--- a/app/components/proposals/votes_component.html.erb
+++ b/app/components/proposals/votes_component.html.erb
@@ -2,7 +2,7 @@
<%= render "proposals/supports", proposal: proposal %>
- <% if voted_for?(proposal_votes, proposal) %>
+ <% if current_user&.voted_for?(proposal) %>
<%= t("proposals.proposal.already_supported") %>
@@ -39,7 +39,7 @@
<% end %>
- <% if voted_for?(proposal_votes, proposal) && setting["twitter_handle"] %>
+ <% if current_user&.voted_for?(proposal) && setting["twitter_handle"] %>
<%= render "proposals/social_share", proposal: proposal, share_title: false %>
diff --git a/app/components/proposals/votes_component.rb b/app/components/proposals/votes_component.rb
index 12fa65dad..5f74d362c 100644
--- a/app/components/proposals/votes_component.rb
+++ b/app/components/proposals/votes_component.rb
@@ -1,10 +1,9 @@
class Proposals::VotesComponent < ApplicationComponent
- attr_reader :proposal, :vote_url, :proposal_votes
- delegate :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, to: :helpers
+ attr_reader :proposal, :vote_url
+ delegate :current_user, :link_to_verify_account, :user_signed_in?, to: :helpers
- def initialize(proposal, vote_url:, proposal_votes:)
+ def initialize(proposal, vote_url:)
@proposal = proposal
@vote_url = vote_url
- @proposal_votes = proposal_votes
end
end
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 71089a97a..caee4ec18 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -68,14 +68,6 @@ class ApplicationController < ActionController::Base
end
end
- def set_debate_votes(debates)
- @debate_votes = current_user ? current_user.debate_votes(debates) : {}
- end
-
- def set_proposal_votes(proposals)
- @proposal_votes = current_user ? current_user.proposal_votes(proposals) : {}
- end
-
def set_comment_flags(comments)
@comment_flags = current_user ? current_user.comment_flags(comments) : {}
end
diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb
index 1d1b647f9..dd8611366 100644
--- a/app/controllers/concerns/commentable_actions.rb
+++ b/app/controllers/concerns/commentable_actions.rb
@@ -17,14 +17,11 @@ module CommentableActions
@tag_cloud = tag_cloud
- set_resource_votes(@resources)
-
set_resources_instance
@remote_translations = detect_remote_translations(@resources, featured_proposals)
end
def show
- set_resource_votes(resource)
@commentable = resource
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
set_comment_flags(@comment_tree.comments)
@@ -99,10 +96,6 @@ module CommentableActions
@categories = Tag.category.order(:name)
end
- def set_resource_votes(instance)
- send("set_#{resource_name}_votes", instance)
- end
-
def index_customization
nil
end
diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb
index 3dd2f1a7b..df92c3a5e 100644
--- a/app/controllers/debates_controller.rb
+++ b/app/controllers/debates_controller.rb
@@ -30,7 +30,6 @@ class DebatesController < ApplicationController
def vote
@debate.register_vote(current_user, params[:value])
- set_debate_votes(@debate)
end
def unmark_featured
diff --git a/app/controllers/legislation/base_controller.rb b/app/controllers/legislation/base_controller.rb
index b1d01a676..ca609ecba 100644
--- a/app/controllers/legislation/base_controller.rb
+++ b/app/controllers/legislation/base_controller.rb
@@ -2,8 +2,4 @@ class Legislation::BaseController < ApplicationController
include FeatureFlags
feature_flag :legislation
-
- def legislation_proposal_votes(proposals)
- @legislation_proposal_votes = current_user ? current_user.legislation_proposal_votes(proposals) : {}
- end
end
diff --git a/app/controllers/legislation/processes_controller.rb b/app/controllers/legislation/processes_controller.rb
index 9de10b5bc..3ceff8392 100644
--- a/app/controllers/legislation/processes_controller.rb
+++ b/app/controllers/legislation/processes_controller.rb
@@ -124,7 +124,6 @@ class Legislation::ProcessesController < Legislation::BaseController
end
if @process.proposals_phase.started? || current_user&.administrator?
- legislation_proposal_votes(@proposals)
render :proposals
else
render :phase_not_open
diff --git a/app/controllers/legislation/proposals_controller.rb b/app/controllers/legislation/proposals_controller.rb
index d924f43ec..8a9d2f24c 100644
--- a/app/controllers/legislation/proposals_controller.rb
+++ b/app/controllers/legislation/proposals_controller.rb
@@ -19,7 +19,6 @@ class Legislation::ProposalsController < Legislation::BaseController
def show
super
- legislation_proposal_votes(@process.proposals)
@document = Document.new(documentable: @proposal)
if request.path != legislation_process_proposal_path(params[:process_id], @proposal)
redirect_to legislation_process_proposal_path(params[:process_id], @proposal),
@@ -39,7 +38,6 @@ class Legislation::ProposalsController < Legislation::BaseController
def vote
@proposal.register_vote(current_user, params[:value])
- legislation_proposal_votes(@proposal)
end
private
diff --git a/app/controllers/management/proposals_controller.rb b/app/controllers/management/proposals_controller.rb
index d15020ad5..2e28aa2d3 100644
--- a/app/controllers/management/proposals_controller.rb
+++ b/app/controllers/management/proposals_controller.rb
@@ -37,12 +37,10 @@ class Management::ProposalsController < Management::BaseController
def vote
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
@proposal.register_vote(managed_user, "yes")
- set_proposal_votes(@proposal)
end
def print
@proposals = Proposal.send("sort_by_#{@current_order}").for_render.limit(5)
- set_proposal_votes(@proposal)
end
private
@@ -66,10 +64,6 @@ class Management::ProposalsController < Management::BaseController
check_verified_user t("management.proposals.alert.unverified_user")
end
- def set_proposal_votes(proposals)
- @proposal_votes = managed_user ? managed_user.proposal_votes(proposals) : {}
- end
-
def set_comment_flags(comments)
@comment_flags = managed_user ? managed_user.comment_flags(comments) : {}
end
diff --git a/app/controllers/proposals_controller.rb b/app/controllers/proposals_controller.rb
index 25e00f12b..d9857837b 100644
--- a/app/controllers/proposals_controller.rb
+++ b/app/controllers/proposals_controller.rb
@@ -58,7 +58,6 @@ class ProposalsController < ApplicationController
def vote
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
@proposal.register_vote(current_user, "yes")
- set_proposal_votes(@proposal)
end
def retire
@@ -75,7 +74,6 @@ class ProposalsController < ApplicationController
def vote_featured
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
@proposal.register_vote(current_user, "yes")
- set_featured_proposal_votes(@proposal)
end
def summary
@@ -118,10 +116,6 @@ class ProposalsController < ApplicationController
Proposal
end
- def set_featured_proposal_votes(proposals)
- @featured_proposals_votes = current_user ? current_user.proposal_votes(proposals) : {}
- end
-
def discard_draft
@resources = @resources.published
end
@@ -156,7 +150,6 @@ class ProposalsController < ApplicationController
@featured_proposals = Proposal.not_archived.unsuccessful
.sort_by_confidence_score.limit(Setting["featured_proposals_number"])
if @featured_proposals.present?
- set_featured_proposal_votes(@featured_proposals)
@resources = @resources.where.not(id: @featured_proposals)
end
end
diff --git a/app/helpers/votes_helper.rb b/app/helpers/votes_helper.rb
index 8739c2323..e4b4f6a3c 100644
--- a/app/helpers/votes_helper.rb
+++ b/app/helpers/votes_helper.rb
@@ -13,8 +13,8 @@ module VotesHelper
end
end
- def css_classes_for_vote(votes, votable)
- case votes[votable.id]
+ def css_classes_for_vote(votable)
+ case current_user&.voted_as_when_voted_for(votable)
when true
{ in_favor: "voted", against: "no-voted" }
when false
@@ -23,8 +23,4 @@ module VotesHelper
{ in_favor: "", against: "" }
end
end
-
- def voted_for?(votes, votable)
- votes[votable.id]
- end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 0dde87ca7..afb45baf5 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -147,21 +147,6 @@ class User < ApplicationRecord
organization? ? organization.name : username
end
- def debate_votes(debates)
- voted = votes.for_debates(Array(debates).map(&:id))
- voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
- end
-
- def proposal_votes(proposals)
- voted = votes.for_proposals(Array(proposals).map(&:id))
- voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
- end
-
- def legislation_proposal_votes(proposals)
- voted = votes.for_legislation_proposals(proposals)
- voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value }
- end
-
def comment_flags(comments)
comment_flags = flags.for_comments(comments)
comment_flags.each_with_object({}) { |f, h| h[f.flaggable_id] = true }
diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb
index 72bc717ac..83f865023 100644
--- a/app/views/debates/_debate.html.erb
+++ b/app/views/debates/_debate.html.erb
@@ -1,4 +1,4 @@
-<% cache [locale_and_user_status, debate, @debate_votes[debate.id]] do %>
+<% cache [locale_and_user_status, debate, current_user&.voted_as_when_voted_for(debate)] do %>
diff --git a/app/views/debates/_debate_minimal.html.erb b/app/views/debates/_debate_minimal.html.erb
index be640cb4f..e0156d95a 100644
--- a/app/views/debates/_debate_minimal.html.erb
+++ b/app/views/debates/_debate_minimal.html.erb
@@ -4,7 +4,7 @@
<% cache [locale_and_user_status,
- "index_minimal", debate, @debate_votes[debate.id]] do %>
+ "index_minimal", debate, current_user&.voted_as_when_voted_for(debate)] do %>
<%= link_to debate.title, debate %>
<% end %>
diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb
index dfdd0f629..01a1f2b3f 100644
--- a/app/views/debates/_votes.html.erb
+++ b/app/views/debates/_votes.html.erb
@@ -1 +1 @@
-<%= render Debates::VotesComponent.new(debate, debate_votes: @debate_votes) %>
+<%= render Debates::VotesComponent.new(debate) %>
diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb
index deebcaf54..72fb918e7 100644
--- a/app/views/debates/show.html.erb
+++ b/app/views/debates/show.html.erb
@@ -3,7 +3,11 @@
<%= render "shared/canonical", href: debate_url(@debate) %>
<% end %>
-<% cache [locale_and_user_status(@debate), @debate, @debate.author, Flag.flagged?(current_user, @debate), @debate_votes] do %>
+<% cache [locale_and_user_status(@debate),
+ @debate,
+ @debate.author,
+ Flag.flagged?(current_user, @debate),
+ current_user&.voted_as_when_voted_for(@debate)] do %>
diff --git a/app/views/legislation/proposals/_votes.html.erb b/app/views/legislation/proposals/_votes.html.erb
index 669ea4ca6..175527233 100644
--- a/app/views/legislation/proposals/_votes.html.erb
+++ b/app/views/legislation/proposals/_votes.html.erb
@@ -1 +1 @@
-<%= render Legislation::Proposals::VotesComponent.new(proposal, legislation_proposal_votes: @legislation_proposal_votes) %>
+<%= render Legislation::Proposals::VotesComponent.new(proposal) %>
diff --git a/app/views/legislation/proposals/show.html.erb b/app/views/legislation/proposals/show.html.erb
index 6d0baea0e..91a210d01 100644
--- a/app/views/legislation/proposals/show.html.erb
+++ b/app/views/legislation/proposals/show.html.erb
@@ -21,7 +21,11 @@
-<% cache [locale_and_user_status(@proposal), @proposal, @proposal.author, Flag.flagged?(current_user, @proposal), @legislation_proposal_votes] do %>
+<% cache [locale_and_user_status(@proposal),
+ @proposal,
+ @proposal.author,
+ Flag.flagged?(current_user, @proposal),
+ current_user&.voted_as_when_voted_for(@proposal)] do %>
diff --git a/app/views/proposals/_featured_votes.html.erb b/app/views/proposals/_featured_votes.html.erb
index 4c42406b0..96021e5bd 100644
--- a/app/views/proposals/_featured_votes.html.erb
+++ b/app/views/proposals/_featured_votes.html.erb
@@ -1,6 +1,6 @@
- <% if voted_for?(@featured_proposals_votes, proposal) %>
+ <% if current_user&.voted_for?(proposal) %>
<%= t("proposals.proposal.already_supported") %>
@@ -29,7 +29,7 @@
<%= render "shared/login_to_vote" %>
<% end %>
- <% if voted_for?(@featured_proposals_votes, proposal) %>
+ <% if current_user&.voted_for?(proposal) %>
<% if setting["twitter_handle"] %>
<%= render "shared/social_share",
diff --git a/app/views/proposals/_votes.html.erb b/app/views/proposals/_votes.html.erb
index 9f3f3b4a6..9e8784e57 100644
--- a/app/views/proposals/_votes.html.erb
+++ b/app/views/proposals/_votes.html.erb
@@ -1 +1 @@
-<%= render Proposals::VotesComponent.new(proposal, vote_url: vote_url, proposal_votes: @proposal_votes) %>
+<%= render Proposals::VotesComponent.new(proposal, vote_url: vote_url) %>
diff --git a/app/views/proposals/share.html.erb b/app/views/proposals/share.html.erb
index 0cb387809..1ee1938de 100644
--- a/app/views/proposals/share.html.erb
+++ b/app/views/proposals/share.html.erb
@@ -9,7 +9,11 @@
<%= render "shared/canonical", href: proposal_url(@proposal) %>
<% end %>
-<% cache [locale_and_user_status(@proposal), @proposal, @proposal.author, Flag.flagged?(current_user, @proposal), @proposal_votes] do %>
+<% cache [locale_and_user_status(@proposal),
+ @proposal,
+ @proposal.author,
+ Flag.flagged?(current_user, @proposal),
+ current_user&.voted_for?(@proposal)] do %>
diff --git a/app/views/proposals/show.html.erb b/app/views/proposals/show.html.erb
index a5a05e050..15f52d8c7 100644
--- a/app/views/proposals/show.html.erb
+++ b/app/views/proposals/show.html.erb
@@ -18,7 +18,7 @@
@proposal.author,
Flag.flagged?(current_user, @proposal),
@proposal.followed_by?(current_user),
- @proposal_votes] do %>
+ current_user&.voted_for?(@proposal)] do %>
diff --git a/spec/helpers/votes_helper_spec.rb b/spec/helpers/votes_helper_spec.rb
index 2ed294d4b..79bd51e42 100644
--- a/spec/helpers/votes_helper_spec.rb
+++ b/spec/helpers/votes_helper_spec.rb
@@ -1,22 +1,6 @@
require "rails_helper"
describe VotesHelper do
- describe "#voted_for?" do
- it "returns true if voted for a proposal" do
- proposal = create(:proposal)
- votes = { proposal.id => true }
-
- expect(voted_for?(votes, proposal)).to eq(true)
- end
-
- it "returns false if not voted for a proposals" do
- proposal = create(:proposal)
- votes = { proposal.id => nil }
-
- expect(voted_for?(votes, proposal)).to eq(nil)
- end
- end
-
describe "#votes_percentage" do
it "alwayses sum 100%" do
debate = create(:debate)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 72acf4f50..e8532c1f8 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -33,28 +33,6 @@ describe User do
end
end
- describe "#debate_votes" do
- let(:user) { create(:user) }
-
- it "returns {} if no debate" do
- expect(user.debate_votes([])).to eq({})
- end
-
- it "returns a hash of debates ids and votes" do
- debate1 = create(:debate)
- debate2 = create(:debate)
- debate3 = create(:debate)
- create(:vote, voter: user, votable: debate1, vote_flag: true)
- create(:vote, voter: user, votable: debate3, vote_flag: false)
-
- voted = user.debate_votes([debate1, debate2, debate3])
-
- expect(voted[debate1.id]).to eq(true)
- expect(voted[debate2.id]).to eq(nil)
- expect(voted[debate3.id]).to eq(false)
- end
- end
-
describe "#comment_flags" do
let(:user) { create(:user) }