adds voting for comments functionality [#25]

This commit is contained in:
rgarcia
2015-08-02 13:37:37 +02:00
parent 44ae7078ea
commit 2a3086010e
6 changed files with 40 additions and 6 deletions

View File

@@ -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

View File

@@ -8,6 +8,11 @@
<%= comment.user.name %>&nbsp;&bullet;&nbsp;<%= time_ago_in_words(comment.created_at) %>
</span>
<p><%= comment.body %></p>
<span id="<%= dom_id(comment) %>_votes">
<%= render 'comments/votes', comment: comment %>
</span>
<p class="reply"><%= render 'comments/form', parent: comment %></p>
</div>

View File

@@ -0,0 +1,11 @@
<span class="in_favor">
<%= link_to "up", debate_comment_votes_path(@debate, comment, value: 'yes'),
method: "post", remote: true %>
<%= comment.get_likes.size %>
</span>
<span class="against">
<%= link_to "down", debate_comment_votes_path(@debate, comment, value: 'no'),
method: "post", remote: true %>
<%= comment.get_dislikes.size %>
</span>

View File

@@ -17,8 +17,8 @@
<p><%= render 'shared/tags', debate: @debate %></p>
</div>
<div id="votes" class="votes small-12 medium-3 column">
<%= render 'votes/votes' %>
<div id="<%= dom_id(@debate) %>_votes" class="votes small-12 medium-3 column">
<%= render 'debates/votes' %>
</div>
</div>
</section>

View File

@@ -1 +1,2 @@
$("#votes").html("<%= j render('votes') %>");
$("#<%= dom_id(@resource) %>_votes").
html('<%= j render("#{@resource_name.pluralize}/votes", comment: @resource) %>');