Simplify managing investment votes

If I'm right, the `investment_votes` instance variable only exists to
avoid several database queries to get whether the current user has
supported each of the investments.

However, that doesn't make much sense when only one investment is shown.
In this case, the number of queries stays the same, and so we can
simplify the code by rendering the component with an optional parameter.
This commit is contained in:
Javi Martín
2021-06-13 02:55:09 +02:00
parent d5f4313f59
commit 5fab843184
11 changed files with 16 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ class Budgets::Investments::VotesComponent < ApplicationComponent
delegate :namespace, :current_user, :voted_for?, :image_absolute_url,
:link_to_verify_account, :link_to_signin, :link_to_signup, to: :helpers
def initialize(investment, investment_votes:)
def initialize(investment, investment_votes: nil)
@investment = investment
@investment_votes = investment_votes
end
@@ -28,7 +28,11 @@ class Budgets::Investments::VotesComponent < ApplicationComponent
end
def user_voted_for?
@user_voted_for ||= voted_for?(investment_votes, investment)
@user_voted_for ||= if investment_votes
voted_for?(investment_votes, investment)
else
current_user&.voted_for?(investment)
end
end
def display_support_alert?

View File

@@ -60,7 +60,6 @@ module Budgets
@comment_tree = CommentTree.new(@commentable, params[:page], @current_order)
@related_contents = Kaminari.paginate_array(@investment.relationed_contents).page(params[:page]).per(5)
set_comment_flags(@comment_tree.comments)
load_investment_votes(@investment)
@investment_ids = [@investment.id]
@remote_translations = detect_remote_translations([@investment], @comment_tree.comments)
end
@@ -94,7 +93,6 @@ module Budgets
def vote
@investment.register_selection(current_user)
load_investment_votes(@investment)
respond_to do |format|
format.html { redirect_to budget_investments_path(heading_id: @investment.heading.id) }
format.js

View File

@@ -37,12 +37,10 @@ class Management::Budgets::InvestmentsController < Management::BaseController
end
def show
load_investment_votes(@investment)
end
def vote
@investment.register_selection(managed_user)
load_investment_votes(@investment)
respond_to do |format|
format.html { redirect_to management_budget_investments_path(heading_id: @investment.heading.id) }
format.js

View File

@@ -38,9 +38,8 @@
<div id="<%= dom_id(investment) %>_votes"
class="small-12 medium-3 column text-center"
<%= "data-equalizer-watch" if feature?(:allow_images) && investment.image.present? %>>
<%= render "/budgets/investments/votes",
investment: investment,
investment_votes: investment_votes %>
<%= render Budgets::Investments::VotesComponent.new(investment,
investment_votes: investment_votes) %>
</div>
<% elsif investment.should_show_vote_count? %>
<div id="<%= dom_id(investment) %>_votes"

View File

@@ -13,7 +13,7 @@
investment.author,
Flag.flagged?(current_user, investment),
investment.followed_by?(current_user),
@investment_votes] do %>
current_user&.voted_for?(investment)] do %>
<section class="budget-investment-show" id="<%= dom_id(investment) %>">
<div class="row">
@@ -32,9 +32,7 @@
<h2><%= t("budgets.investments.show.supports") %></h2>
<div class="text-center">
<div id="<%= dom_id(investment) %>_votes">
<%= render "/budgets/investments/votes",
investment: investment,
investment_votes: investment_votes %>
<%= render Budgets::Investments::VotesComponent.new(investment) %>
</div>
</div>
<% elsif investment.should_show_vote_count? %>

View File

@@ -1,4 +0,0 @@
<%= render Budgets::Investments::VotesComponent.new(
investment,
investment_votes: investment_votes
) %>

View File

@@ -6,7 +6,6 @@
<%= render "/budgets/investments/investment_show",
investment: @investment,
investment_ids: @investment_ids,
investment_votes: @investment_votes,
ballot: @ballot %>
<div class="row">

View File

@@ -1,3 +1,2 @@
$("#<%= dom_id(@investment) %>_votes").html("<%= j render("/budgets/investments/votes",
investment: @investment,
investment_votes: @investment_votes) %>");
$("#<%= dom_id(@investment) %>_votes")
.html("<%= j render Budgets::Investments::VotesComponent.new(@investment) %>");

View File

@@ -2,6 +2,4 @@
<%= render "/shared/print" %>
<%= render "/budgets/investments/investment_show",
investment: @investment,
investment_votes: @investment_votes %>
<%= render "/budgets/investments/investment_show", investment: @investment %>

View File

@@ -1,3 +1,2 @@
$("#<%= dom_id(@investment) %>_votes").html("<%= j render("/budgets/investments/votes",
investment: @investment,
investment_votes: @investment_votes) %>");
$("#<%= dom_id(@investment) %>_votes")
.html("<%= j render Budgets::Investments::VotesComponent.new(@investment) %>");

View File

@@ -4,7 +4,7 @@ describe Budgets::Investments::VotesComponent, type: :component do
describe "vote link" do
context "when investment shows votes" do
let(:investment) { create(:budget_investment, title: "Renovate sidewalks in Main Street") }
let(:component) { Budgets::Investments::VotesComponent.new(investment, investment_votes: []) }
let(:component) { Budgets::Investments::VotesComponent.new(investment) }
before { allow(investment).to receive(:should_show_votes?).and_return(true) }