Files
nairobi/app/components/budgets/investments/votes_component.rb
Javi Martín 0214184b2d Remove investment supports query optimizations
In the previous commit I mentioned:

> 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.

Now let's discuss the case when there are several investments, like in
the investments index:

* There are 10 investments per page by default
* Each query takes less than a millisecond
* We still make a query per investment to check whether the current user
  voted in a different group
* AFAIK, there have been no performance tests showing these
  optimizations make the request to the investments index significantly
  faster
* These optimizations make the code way more complex than it is without
  them

Considering all these points, I'm removing the optimizations. I'm fine
with adding `includes` calls to preload records and avoid N+1 queries
even if there are no performance tests showing they make the application
faster because the effect on the code complexity is negligible. But
that's not the case here.

Note we're using `defined?` instead of the `||=` operator because the
`||=` operator will not serve its purpose when the result of the
operation returns `false`.
2021-06-14 14:41:57 +02:00

48 lines
1.3 KiB
Ruby

class Budgets::Investments::VotesComponent < ApplicationComponent
attr_reader :investment
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 = investment
end
def vote_path
case namespace
when "management"
vote_management_budget_investment_path(investment.budget, investment, value: "yes")
else
vote_budget_investment_path(investment.budget, investment, value: "yes")
end
end
private
def reason
@reason ||= investment.reason_for_not_being_selectable_by(current_user)
end
def voting_allowed?
reason != :not_voting_allowed
end
def user_voted_for?
@user_voted_for = current_user&.voted_for?(investment) unless defined?(@user_voted_for)
@user_voted_for
end
def display_support_alert?
current_user &&
!current_user.voted_in_group?(investment.group) &&
investment.group.headings.count > 1
end
def confirm_vote_message
t("budgets.investments.investment.confirm_group", count: investment.group.max_votable_headings)
end
def support_aria_label
t("budgets.investments.investment.support_label", investment: investment.title)
end
end