diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 00fd20653..409bb7c9b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,6 +1,6 @@ class CommentsController < ApplicationController before_action :authenticate_user! - before_action :set_debate, :set_parent + before_action :set_debate, :set_parent, only: :create respond_to :html, :js def create @@ -10,6 +10,12 @@ class CommentsController < ApplicationController respond_with @comment end + def vote + @comment = Comment.find(params[:id]) + @comment.vote_by(voter: current_user, vote: params[:value]) + respond_with @comment + end + private def comment_params params.require(:comments).permit(:commentable_type, :commentable_id, :body) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 672f41d20..fe572abe4 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,7 +1,7 @@ class DebatesController < ApplicationController - include RecaptchaHelper - before_action :set_debate, only: [:show, :edit, :update] - before_action :authenticate_user!, except: [:show, :index] + include RecaptchaHelper + before_action :set_debate, only: [:show, :edit, :update, :vote] + before_action :authenticate_user!, except: [:index, :show, :vote] before_action :validate_ownership, only: [:edit, :update] def index @@ -37,6 +37,10 @@ class DebatesController < ApplicationController respond_with @debate end + def vote + @debate.vote_by(voter: current_user, vote: params[:value]) + end + private def set_debate diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb deleted file mode 100644 index c42935ddf..000000000 --- a/app/controllers/votes_controller.rb +++ /dev/null @@ -1,38 +0,0 @@ -class VotesController < ApplicationController - before_action :set_debate - before_action :set_resource - before_action :authenticate_user! - respond_to :html, :js - - def create - register_vote - respond_with @debate - end - - private - - def set_resource - @resource = resource_model.find(params["#{resource_name + "_id"}"]) - end - - def resource_name - @resource_name ||= params[:votable_type] - end - - def resource_model - resource_name.capitalize.constantize - end - - def set_debate - @debate = Debate.find(params[:debate_id]) - end - - def register_vote - @resource.vote_by voter: current_user, vote: params[:value] - end - - def notice - @resource.vote_registered? ? I18n.t("votes.notice_thanks") : I18n.t("votes.notice_already_registered") - end - -end diff --git a/app/views/comments/_votes.html.erb b/app/views/comments/_votes.html.erb index 24b7dd869..2f92ff246 100644 --- a/app/views/comments/_votes.html.erb +++ b/app/views/comments/_votes.html.erb @@ -1,11 +1,11 @@ - <%= link_to "up", debate_comment_votes_path(@debate, comment, value: 'yes'), + <%= link_to "up", vote_comment_path(comment, value: 'yes'), method: "post", remote: true %> <%= comment.get_likes.size %> - <%= link_to "down", debate_comment_votes_path(@debate, comment, value: 'no'), + <%= link_to "down", vote_comment_path(comment, value: 'no'), method: "post", remote: true %> <%= comment.get_dislikes.size %> \ No newline at end of file diff --git a/app/views/comments/vote.js.erb b/app/views/comments/vote.js.erb new file mode 100644 index 000000000..11f0ad905 --- /dev/null +++ b/app/views/comments/vote.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment) %>_votes").html('<%= j render("comments/votes", comment: @comment) %>'); \ No newline at end of file diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index fd2704ed4..1ed675ef1 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -1,6 +1,7 @@