Remove votes query optimizations
Just like we did in commit0214184b2dfor investments, we're removing some possible optimizations (we don't have any benchmarks proving they affect performance at all) in order to simplify the code. The investement votes component `delegate` code was accidentally left but isn't used since commit0214184b2, so we're removing it now that we're removing the `voted_for?` helper method.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class Budgets::Investments::VotesComponent < ApplicationComponent
|
class Budgets::Investments::VotesComponent < ApplicationComponent
|
||||||
attr_reader :investment
|
attr_reader :investment
|
||||||
delegate :namespace, :current_user, :voted_for?, :image_absolute_url,
|
delegate :namespace, :current_user, :image_absolute_url,
|
||||||
:link_to_verify_account, :link_to_signin, :link_to_signup, to: :helpers
|
:link_to_verify_account, :link_to_signin, :link_to_signup, to: :helpers
|
||||||
|
|
||||||
def initialize(investment)
|
def initialize(investment)
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<% voted_classes = css_classes_for_vote(debate_votes, debate) %>
|
<% voted_classes = css_classes_for_vote(debate) %>
|
||||||
<div class="votes">
|
<div class="votes">
|
||||||
<div class="in-favor inline-block">
|
<div class="in-favor inline-block">
|
||||||
<% if user_signed_in? %>
|
<% if user_signed_in? %>
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
class Debates::VotesComponent < ApplicationComponent
|
class Debates::VotesComponent < ApplicationComponent
|
||||||
attr_reader :debate, :debate_votes
|
attr_reader :debate
|
||||||
delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :votes_percentage, to: :helpers
|
delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :votes_percentage, to: :helpers
|
||||||
|
|
||||||
def initialize(debate, debate_votes:)
|
def initialize(debate)
|
||||||
@debate = debate
|
@debate = debate
|
||||||
@debate_votes = debate_votes
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
<% voted_classes = css_classes_for_vote(legislation_proposal_votes, proposal) %>
|
<% voted_classes = css_classes_for_vote(proposal) %>
|
||||||
<div class="votes">
|
<div class="votes">
|
||||||
<% if proposal.process.proposals_phase.open? %>
|
<% if proposal.process.proposals_phase.open? %>
|
||||||
<div class="in-favor inline-block">
|
<div class="in-favor inline-block">
|
||||||
@@ -64,7 +64,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if voted_for?(legislation_proposal_votes, proposal) && setting["twitter_handle"] %>
|
<% if current_user&.voted_as_when_voted_for(proposal) && setting["twitter_handle"] %>
|
||||||
<div class="share-supported">
|
<div class="share-supported">
|
||||||
<%= render "shared/social_share",
|
<%= render "shared/social_share",
|
||||||
title: proposal.title,
|
title: proposal.title,
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
class Legislation::Proposals::VotesComponent < ApplicationComponent
|
class Legislation::Proposals::VotesComponent < ApplicationComponent
|
||||||
attr_reader :proposal, :legislation_proposal_votes
|
attr_reader :proposal
|
||||||
delegate :css_classes_for_vote, :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, :votes_percentage, to: :helpers
|
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
|
@proposal = proposal
|
||||||
@legislation_proposal_votes = legislation_proposal_votes
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
<%= render "proposals/supports", proposal: proposal %>
|
<%= render "proposals/supports", proposal: proposal %>
|
||||||
|
|
||||||
<div class="in-favor">
|
<div class="in-favor">
|
||||||
<% if voted_for?(proposal_votes, proposal) %>
|
<% if current_user&.voted_for?(proposal) %>
|
||||||
<div class="supported callout success">
|
<div class="supported callout success">
|
||||||
<%= t("proposals.proposal.already_supported") %>
|
<%= t("proposals.proposal.already_supported") %>
|
||||||
</div>
|
</div>
|
||||||
@@ -39,7 +39,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if voted_for?(proposal_votes, proposal) && setting["twitter_handle"] %>
|
<% if current_user&.voted_for?(proposal) && setting["twitter_handle"] %>
|
||||||
<div class="share-supported">
|
<div class="share-supported">
|
||||||
<%= render "proposals/social_share", proposal: proposal, share_title: false %>
|
<%= render "proposals/social_share", proposal: proposal, share_title: false %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
class Proposals::VotesComponent < ApplicationComponent
|
class Proposals::VotesComponent < ApplicationComponent
|
||||||
attr_reader :proposal, :vote_url, :proposal_votes
|
attr_reader :proposal, :vote_url
|
||||||
delegate :current_user, :link_to_verify_account, :user_signed_in?, :voted_for?, to: :helpers
|
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
|
@proposal = proposal
|
||||||
@vote_url = vote_url
|
@vote_url = vote_url
|
||||||
@proposal_votes = proposal_votes
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -68,14 +68,6 @@ class ApplicationController < ActionController::Base
|
|||||||
end
|
end
|
||||||
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)
|
def set_comment_flags(comments)
|
||||||
@comment_flags = current_user ? current_user.comment_flags(comments) : {}
|
@comment_flags = current_user ? current_user.comment_flags(comments) : {}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -17,14 +17,11 @@ module CommentableActions
|
|||||||
|
|
||||||
@tag_cloud = tag_cloud
|
@tag_cloud = tag_cloud
|
||||||
|
|
||||||
set_resource_votes(@resources)
|
|
||||||
|
|
||||||
set_resources_instance
|
set_resources_instance
|
||||||
@remote_translations = detect_remote_translations(@resources, featured_proposals)
|
@remote_translations = detect_remote_translations(@resources, featured_proposals)
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
set_resource_votes(resource)
|
|
||||||
@commentable = resource
|
@commentable = resource
|
||||||
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
|
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
|
||||||
set_comment_flags(@comment_tree.comments)
|
set_comment_flags(@comment_tree.comments)
|
||||||
@@ -99,10 +96,6 @@ module CommentableActions
|
|||||||
@categories = Tag.category.order(:name)
|
@categories = Tag.category.order(:name)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_resource_votes(instance)
|
|
||||||
send("set_#{resource_name}_votes", instance)
|
|
||||||
end
|
|
||||||
|
|
||||||
def index_customization
|
def index_customization
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -30,7 +30,6 @@ class DebatesController < ApplicationController
|
|||||||
|
|
||||||
def vote
|
def vote
|
||||||
@debate.register_vote(current_user, params[:value])
|
@debate.register_vote(current_user, params[:value])
|
||||||
set_debate_votes(@debate)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def unmark_featured
|
def unmark_featured
|
||||||
|
|||||||
@@ -2,8 +2,4 @@ class Legislation::BaseController < ApplicationController
|
|||||||
include FeatureFlags
|
include FeatureFlags
|
||||||
|
|
||||||
feature_flag :legislation
|
feature_flag :legislation
|
||||||
|
|
||||||
def legislation_proposal_votes(proposals)
|
|
||||||
@legislation_proposal_votes = current_user ? current_user.legislation_proposal_votes(proposals) : {}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -124,7 +124,6 @@ class Legislation::ProcessesController < Legislation::BaseController
|
|||||||
end
|
end
|
||||||
|
|
||||||
if @process.proposals_phase.started? || current_user&.administrator?
|
if @process.proposals_phase.started? || current_user&.administrator?
|
||||||
legislation_proposal_votes(@proposals)
|
|
||||||
render :proposals
|
render :proposals
|
||||||
else
|
else
|
||||||
render :phase_not_open
|
render :phase_not_open
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ class Legislation::ProposalsController < Legislation::BaseController
|
|||||||
|
|
||||||
def show
|
def show
|
||||||
super
|
super
|
||||||
legislation_proposal_votes(@process.proposals)
|
|
||||||
@document = Document.new(documentable: @proposal)
|
@document = Document.new(documentable: @proposal)
|
||||||
if request.path != legislation_process_proposal_path(params[:process_id], @proposal)
|
if request.path != legislation_process_proposal_path(params[:process_id], @proposal)
|
||||||
redirect_to 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
|
def vote
|
||||||
@proposal.register_vote(current_user, params[:value])
|
@proposal.register_vote(current_user, params[:value])
|
||||||
legislation_proposal_votes(@proposal)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@@ -37,12 +37,10 @@ class Management::ProposalsController < Management::BaseController
|
|||||||
def vote
|
def vote
|
||||||
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
||||||
@proposal.register_vote(managed_user, "yes")
|
@proposal.register_vote(managed_user, "yes")
|
||||||
set_proposal_votes(@proposal)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def print
|
def print
|
||||||
@proposals = Proposal.send("sort_by_#{@current_order}").for_render.limit(5)
|
@proposals = Proposal.send("sort_by_#{@current_order}").for_render.limit(5)
|
||||||
set_proposal_votes(@proposal)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@@ -66,10 +64,6 @@ class Management::ProposalsController < Management::BaseController
|
|||||||
check_verified_user t("management.proposals.alert.unverified_user")
|
check_verified_user t("management.proposals.alert.unverified_user")
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_proposal_votes(proposals)
|
|
||||||
@proposal_votes = managed_user ? managed_user.proposal_votes(proposals) : {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def set_comment_flags(comments)
|
def set_comment_flags(comments)
|
||||||
@comment_flags = managed_user ? managed_user.comment_flags(comments) : {}
|
@comment_flags = managed_user ? managed_user.comment_flags(comments) : {}
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -58,7 +58,6 @@ class ProposalsController < ApplicationController
|
|||||||
def vote
|
def vote
|
||||||
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
||||||
@proposal.register_vote(current_user, "yes")
|
@proposal.register_vote(current_user, "yes")
|
||||||
set_proposal_votes(@proposal)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def retire
|
def retire
|
||||||
@@ -75,7 +74,6 @@ class ProposalsController < ApplicationController
|
|||||||
def vote_featured
|
def vote_featured
|
||||||
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
@follow = Follow.find_or_create_by!(user: current_user, followable: @proposal)
|
||||||
@proposal.register_vote(current_user, "yes")
|
@proposal.register_vote(current_user, "yes")
|
||||||
set_featured_proposal_votes(@proposal)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def summary
|
def summary
|
||||||
@@ -118,10 +116,6 @@ class ProposalsController < ApplicationController
|
|||||||
Proposal
|
Proposal
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_featured_proposal_votes(proposals)
|
|
||||||
@featured_proposals_votes = current_user ? current_user.proposal_votes(proposals) : {}
|
|
||||||
end
|
|
||||||
|
|
||||||
def discard_draft
|
def discard_draft
|
||||||
@resources = @resources.published
|
@resources = @resources.published
|
||||||
end
|
end
|
||||||
@@ -156,7 +150,6 @@ class ProposalsController < ApplicationController
|
|||||||
@featured_proposals = Proposal.not_archived.unsuccessful
|
@featured_proposals = Proposal.not_archived.unsuccessful
|
||||||
.sort_by_confidence_score.limit(Setting["featured_proposals_number"])
|
.sort_by_confidence_score.limit(Setting["featured_proposals_number"])
|
||||||
if @featured_proposals.present?
|
if @featured_proposals.present?
|
||||||
set_featured_proposal_votes(@featured_proposals)
|
|
||||||
@resources = @resources.where.not(id: @featured_proposals)
|
@resources = @resources.where.not(id: @featured_proposals)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -13,8 +13,8 @@ module VotesHelper
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def css_classes_for_vote(votes, votable)
|
def css_classes_for_vote(votable)
|
||||||
case votes[votable.id]
|
case current_user&.voted_as_when_voted_for(votable)
|
||||||
when true
|
when true
|
||||||
{ in_favor: "voted", against: "no-voted" }
|
{ in_favor: "voted", against: "no-voted" }
|
||||||
when false
|
when false
|
||||||
@@ -23,8 +23,4 @@ module VotesHelper
|
|||||||
{ in_favor: "", against: "" }
|
{ in_favor: "", against: "" }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def voted_for?(votes, votable)
|
|
||||||
votes[votable.id]
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -147,21 +147,6 @@ class User < ApplicationRecord
|
|||||||
organization? ? organization.name : username
|
organization? ? organization.name : username
|
||||||
end
|
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)
|
def comment_flags(comments)
|
||||||
comment_flags = flags.for_comments(comments)
|
comment_flags = flags.for_comments(comments)
|
||||||
comment_flags.each_with_object({}) { |f, h| h[f.flaggable_id] = true }
|
comment_flags.each_with_object({}) { |f, h| h[f.flaggable_id] = true }
|
||||||
|
|||||||
@@ -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 %>
|
||||||
<div id="<%= dom_id(debate) %>" class="debate clear" data-type="debate">
|
<div id="<%= dom_id(debate) %>" class="debate clear" data-type="debate">
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
<div class="small-12 column">
|
<div class="small-12 column">
|
||||||
<div class="debate-content">
|
<div class="debate-content">
|
||||||
<% cache [locale_and_user_status,
|
<% 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 %>
|
||||||
<h3><%= link_to debate.title, debate %></h3>
|
<h3><%= link_to debate.title, debate %></h3>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<%= render Debates::VotesComponent.new(debate, debate_votes: @debate_votes) %>
|
<%= render Debates::VotesComponent.new(debate) %>
|
||||||
|
|||||||
@@ -3,7 +3,11 @@
|
|||||||
<%= render "shared/canonical", href: debate_url(@debate) %>
|
<%= render "shared/canonical", href: debate_url(@debate) %>
|
||||||
<% end %>
|
<% 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 %>
|
||||||
<div class="debate-show">
|
<div class="debate-show">
|
||||||
<div id="<%= dom_id(@debate) %>" class="row">
|
<div id="<%= dom_id(@debate) %>" class="row">
|
||||||
<div class="small-12 medium-9 column">
|
<div class="small-12 medium-9 column">
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<%= render Legislation::Proposals::VotesComponent.new(proposal, legislation_proposal_votes: @legislation_proposal_votes) %>
|
<%= render Legislation::Proposals::VotesComponent.new(proposal) %>
|
||||||
|
|||||||
@@ -21,7 +21,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% 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 %>
|
||||||
<div class="proposal-show legislation-proposal-show">
|
<div class="proposal-show legislation-proposal-show">
|
||||||
<div id="<%= dom_id(@proposal) %>" class="row">
|
<div id="<%= dom_id(@proposal) %>" class="row">
|
||||||
<div class="small-12 medium-9 column">
|
<div class="small-12 medium-9 column">
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<div class="supports text-center">
|
<div class="supports text-center">
|
||||||
<div class="in-favor">
|
<div class="in-favor">
|
||||||
<% if voted_for?(@featured_proposals_votes, proposal) %>
|
<% if current_user&.voted_for?(proposal) %>
|
||||||
<div class="supported">
|
<div class="supported">
|
||||||
<%= t("proposals.proposal.already_supported") %>
|
<%= t("proposals.proposal.already_supported") %>
|
||||||
</div>
|
</div>
|
||||||
@@ -29,7 +29,7 @@
|
|||||||
<%= render "shared/login_to_vote" %>
|
<%= render "shared/login_to_vote" %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
||||||
<% if voted_for?(@featured_proposals_votes, proposal) %>
|
<% if current_user&.voted_for?(proposal) %>
|
||||||
<% if setting["twitter_handle"] %>
|
<% if setting["twitter_handle"] %>
|
||||||
<div class="share-supported">
|
<div class="share-supported">
|
||||||
<%= render "shared/social_share",
|
<%= render "shared/social_share",
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
<%= render Proposals::VotesComponent.new(proposal, vote_url: vote_url, proposal_votes: @proposal_votes) %>
|
<%= render Proposals::VotesComponent.new(proposal, vote_url: vote_url) %>
|
||||||
|
|||||||
@@ -9,7 +9,11 @@
|
|||||||
<%= render "shared/canonical", href: proposal_url(@proposal) %>
|
<%= render "shared/canonical", href: proposal_url(@proposal) %>
|
||||||
<% end %>
|
<% 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 %>
|
||||||
<div id="<%= dom_id(@proposal) %>" class="row">
|
<div id="<%= dom_id(@proposal) %>" class="row">
|
||||||
<div class="small-12 medium-8 column">
|
<div class="small-12 medium-8 column">
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
@proposal.author,
|
@proposal.author,
|
||||||
Flag.flagged?(current_user, @proposal),
|
Flag.flagged?(current_user, @proposal),
|
||||||
@proposal.followed_by?(current_user),
|
@proposal.followed_by?(current_user),
|
||||||
@proposal_votes] do %>
|
current_user&.voted_for?(@proposal)] do %>
|
||||||
<div class="proposal-show">
|
<div class="proposal-show">
|
||||||
<div id="<%= dom_id(@proposal) %>" class="row">
|
<div id="<%= dom_id(@proposal) %>" class="row">
|
||||||
<div class="small-12 medium-9 column">
|
<div class="small-12 medium-9 column">
|
||||||
|
|||||||
@@ -1,22 +1,6 @@
|
|||||||
require "rails_helper"
|
require "rails_helper"
|
||||||
|
|
||||||
describe VotesHelper do
|
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
|
describe "#votes_percentage" do
|
||||||
it "alwayses sum 100%" do
|
it "alwayses sum 100%" do
|
||||||
debate = create(:debate)
|
debate = create(:debate)
|
||||||
|
|||||||
@@ -33,28 +33,6 @@ describe User do
|
|||||||
end
|
end
|
||||||
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
|
describe "#comment_flags" do
|
||||||
let(:user) { create(:user) }
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user