refactors voting implementation [#25]

This commit is contained in:
rgarcia
2015-08-05 12:12:56 +02:00
parent cce6ca2fb9
commit 91b52ad8d2
9 changed files with 31 additions and 56 deletions

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,11 @@
<span class="in_favor">
<%= 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 %>
</span>
<span class="against">
<%= 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 %>
</span>

View File

@@ -0,0 +1 @@
$("#<%= dom_id(@comment) %>_votes").html('<%= j render("comments/votes", comment: @comment) %>');

View File

@@ -1,6 +1,7 @@
<div class="small-12 column text-center">
<div id="in_favor" class="inline-block">
<%= link_to debate_votes_path(@debate, value: 'yes'), class: 'in-favor', method: "post", remote: true do %>
<%= link_to vote_debate_path(@debate, value: 'yes', partial: 'votes'),
class: 'in-favor', method: "post", remote: true do %>
<i class="fi-like"></i>
<span><%= percentage('likes', @debate) %></span>
<% end %>
@@ -9,7 +10,8 @@
<span class="divider"></span>
<div id="against" class="inline-block">
<%= link_to debate_votes_path(@debate, value: 'no'), class: 'against', method: "post", remote: true do %>
<%= link_to vote_debate_path(@debate, value: 'no', partial: 'votes'),
class: 'against', method: "post", remote: true do %>
<i class="fi-dislike"></i>
<span><%= percentage('dislikes', @debate) %></span>
<% end %>

View File

@@ -0,0 +1 @@
$("#<%= dom_id(@debate) %>_votes").html('<%= j render("debates/#{params[:partial]}", debate: @debate) %>');

View File

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

View File

@@ -7,16 +7,17 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root"
root 'debates#index'
concern :votable do |options|
resources :votes, options.merge(only: :create)
end
resources :debates do
concerns :votable, votable_type: 'debate'
resources :comments, only: :create do
concerns :votable, votable_type: 'comment'
member do
post :vote
end
resources :comments, only: :create, shallow: true do
member do
post :vote
end
end
end
resource :account, controller: "account", only: [:show, :update]