From 2a3086010ec14992bcc0b4fbc92934df90193495 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sun, 2 Aug 2015 13:37:37 +0200 Subject: [PATCH] adds voting for comments functionality [#25] --- app/controllers/votes_controller.rb | 23 +++++++++++++++++--- app/views/comments/_comment.html.erb | 5 +++++ app/views/comments/_votes.html.erb | 11 ++++++++++ app/views/{votes => debates}/_votes.html.erb | 0 app/views/debates/show.html.erb | 4 ++-- app/views/votes/create.js.erb | 3 ++- 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 app/views/comments/_votes.html.erb rename app/views/{votes => debates}/_votes.html.erb (100%) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index e93968f35..c42935ddf 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -1,21 +1,38 @@ class VotesController < ApplicationController before_action :set_debate + before_action :set_resource before_action :authenticate_user! respond_to :html, :js - + def create register_vote - notice = @debate.vote_registered? ? I18n.t("votes.notice_thanks") : I18n.t("votes.notice_already_registered") 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 - @debate.vote_by voter: current_user, vote: params[:value] + @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/_comment.html.erb b/app/views/comments/_comment.html.erb index 0d152d928..50c033670 100644 --- a/app/views/comments/_comment.html.erb +++ b/app/views/comments/_comment.html.erb @@ -8,6 +8,11 @@ <%= comment.user.name %> • <%= time_ago_in_words(comment.created_at) %>

<%= comment.body %>

+ + + <%= render 'comments/votes', comment: comment %> + +

<%= render 'comments/form', parent: comment %>

diff --git a/app/views/comments/_votes.html.erb b/app/views/comments/_votes.html.erb new file mode 100644 index 000000000..24b7dd869 --- /dev/null +++ b/app/views/comments/_votes.html.erb @@ -0,0 +1,11 @@ + + <%= link_to "up", debate_comment_votes_path(@debate, comment, value: 'yes'), + method: "post", remote: true %> + <%= comment.get_likes.size %> + + + + <%= link_to "down", debate_comment_votes_path(@debate, comment, value: 'no'), + method: "post", remote: true %> + <%= comment.get_dislikes.size %> + \ No newline at end of file diff --git a/app/views/votes/_votes.html.erb b/app/views/debates/_votes.html.erb similarity index 100% rename from app/views/votes/_votes.html.erb rename to app/views/debates/_votes.html.erb diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 3ae0cace4..8ea945eee 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -17,8 +17,8 @@

<%= render 'shared/tags', debate: @debate %>

-
- <%= render 'votes/votes' %> +
+ <%= render 'debates/votes' %>
diff --git a/app/views/votes/create.js.erb b/app/views/votes/create.js.erb index 5e855e40e..2953d8a25 100644 --- a/app/views/votes/create.js.erb +++ b/app/views/votes/create.js.erb @@ -1 +1,2 @@ -$("#votes").html("<%= j render('votes') %>"); \ No newline at end of file +$("#<%= dom_id(@resource) %>_votes"). + html('<%= j render("#{@resource_name.pluralize}/votes", comment: @resource) %>'); \ No newline at end of file