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).
This commit is contained in:
taitus
2023-09-20 15:06:45 +02:00
parent c96e3b027f
commit fd5fa2da79
22 changed files with 173 additions and 59 deletions

View File

@@ -28,10 +28,6 @@ class DebatesController < ApplicationController
redirect_to debate_path(@debate), status: :moved_permanently if request.path != debate_path(@debate)
end
def vote
@debate.register_vote(current_user, params[:value])
end
def unmark_featured
@debate.update!(featured_at: nil)
redirect_to debates_path