Files
grecia/app/components/shared/in_favor_against_component.rb
taitus fd5fa2da79 Refactoring: Move 'vote' action to Votes Controllers
As far as possible I think the code is clearer if we use CRUD actions
rather than custom actions. This will make it easier to add the action
to remove votes in the next commit.

Note that we are adding this line as we need to validate it that a vote
can be created on a debate by the current user:

```authorize! :create, Vote.new(voter: current_user, votable: @debate)```

We have done it this way and not with the following code as you might
expect, as this way two votes are created instead of one.

```load_and_authorize_resource through: :debate, through_association: :votes_for```

This line tries to load the resource @debate and through the association
"votes_for" it tries to create a new vote associated to that debate.
Therefore a vote is created when trying to authorise the resource and
then another one in the create action, when calling @debate.vote_by (which
is called by @debate.register_vote).
2023-10-09 07:21:49 +02:00

38 lines
842 B
Ruby

class Shared::InFavorAgainstComponent < ApplicationComponent
attr_reader :votable
delegate :current_user, :votes_percentage, to: :helpers
def initialize(votable)
@votable = votable
end
private
def agree_aria_label
t("votes.agree_label", title: votable.title)
end
def disagree_aria_label
t("votes.disagree_label", title: votable.title)
end
def pressed?(value)
case current_user&.voted_as_when_voted_for(votable)
when true
value == "yes"
when false
value == "no"
else
false
end
end
def vote_in_favor_against_path(value)
if votable.class.name == "Debate"
debate_votes_path(votable, value: value)
else
legislation_process_proposal_votes_path(votable.process, votable, value: value)
end
end
end