app/controllers/decidim/proposals/proposal_votes_controller.rb in decidim-proposals-0.2.0 vs app/controllers/decidim/proposals/proposal_votes_controller.rb in decidim-proposals-0.3.0
- old
+ new
@@ -10,23 +10,33 @@
before_action :authenticate_user!
def create
authorize! :vote, proposal
-
- proposal.votes.create!(author: current_user)
@from_proposals_list = params[:from_proposals_list] == "true"
- render :update_buttons_and_counters
+
+ VoteProposal.call(proposal, current_user) do
+ on(:ok) do
+ proposal.reload
+ render :update_buttons_and_counters
+ end
+
+ on(:invalid) do
+ render json: { error: I18n.t("proposal_votes.create.error", scope: "decidim.proposals") }, status: 422
+ end
+ end
end
def destroy
authorize! :unvote, proposal
-
- proposal.votes.where(author: current_user).destroy_all
- proposal.reload
-
@from_proposals_list = params[:from_proposals_list] == "true"
- render :update_buttons_and_counters
+
+ UnvoteProposal.call(proposal, current_user) do
+ on(:ok) do
+ proposal.reload
+ render :update_buttons_and_counters
+ end
+ end
end
private
def proposal