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?