Files
nairobi/app/components/budgets/investments/votes_component.rb
decabeza a851048d56 Allow users to remove their support on investments
Note we don't cast negative votes when users remove their support. That
way we provide compatibility for institutions who have implemented real
negative votes (in case there are / will be any), and we also keep the
database meaningful: it's not that users downvoted something; they
simply removed their upvote.

Co-Authored-By: Javi Martín <javim@elretirao.net>
Co-Authored-By: Julian Nicolas Herrero <microweb10@gmail.com>
2021-06-14 14:46:54 +02:00

63 lines
1.7 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 support_path
case namespace
when "management"
management_budget_investment_votes_path(investment.budget, investment)
else
budget_investment_votes_path(investment.budget, investment)
end
end
def remove_support_path
vote = investment.votes_for.find_by!(voter: current_user)
case namespace
when "management"
management_budget_investment_vote_path(investment.budget, investment, vote)
else
budget_investment_vote_path(investment.budget, investment, vote)
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.votes.confirm_group", count: investment.group.max_votable_headings)
end
def support_aria_label
t("budgets.investments.votes.support_label", investment: investment.title)
end
def remove_support_aria_label
t("budgets.investments.votes.remove_support_label", investment: investment.title)
end
end