refactors voting implementation [#25]
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class CommentsController < ApplicationController
|
class CommentsController < ApplicationController
|
||||||
before_action :authenticate_user!
|
before_action :authenticate_user!
|
||||||
before_action :set_debate, :set_parent
|
before_action :set_debate, :set_parent, only: :create
|
||||||
respond_to :html, :js
|
respond_to :html, :js
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@@ -10,6 +10,12 @@ class CommentsController < ApplicationController
|
|||||||
respond_with @comment
|
respond_with @comment
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def vote
|
||||||
|
@comment = Comment.find(params[:id])
|
||||||
|
@comment.vote_by(voter: current_user, vote: params[:value])
|
||||||
|
respond_with @comment
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def comment_params
|
def comment_params
|
||||||
params.require(:comments).permit(:commentable_type, :commentable_id, :body)
|
params.require(:comments).permit(:commentable_type, :commentable_id, :body)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
class DebatesController < ApplicationController
|
class DebatesController < ApplicationController
|
||||||
include RecaptchaHelper
|
include RecaptchaHelper
|
||||||
before_action :set_debate, only: [:show, :edit, :update]
|
before_action :set_debate, only: [:show, :edit, :update, :vote]
|
||||||
before_action :authenticate_user!, except: [:show, :index]
|
before_action :authenticate_user!, except: [:index, :show, :vote]
|
||||||
before_action :validate_ownership, only: [:edit, :update]
|
before_action :validate_ownership, only: [:edit, :update]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@@ -37,6 +37,10 @@ class DebatesController < ApplicationController
|
|||||||
respond_with @debate
|
respond_with @debate
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def vote
|
||||||
|
@debate.vote_by(voter: current_user, vote: params[:value])
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
private
|
private
|
||||||
def set_debate
|
def set_debate
|
||||||
|
|||||||
@@ -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
|
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
<span class="in_favor">
|
<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 %>
|
method: "post", remote: true %>
|
||||||
<%= comment.get_likes.size %>
|
<%= comment.get_likes.size %>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
<span class="against">
|
<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 %>
|
method: "post", remote: true %>
|
||||||
<%= comment.get_dislikes.size %>
|
<%= comment.get_dislikes.size %>
|
||||||
</span>
|
</span>
|
||||||
1
app/views/comments/vote.js.erb
Normal file
1
app/views/comments/vote.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("#<%= dom_id(@comment) %>_votes").html('<%= j render("comments/votes", comment: @comment) %>');
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
<div class="small-12 column text-center">
|
<div class="small-12 column text-center">
|
||||||
<div id="in_favor" class="inline-block">
|
<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>
|
<i class="fi-like"></i>
|
||||||
<span><%= percentage('likes', @debate) %></span>
|
<span><%= percentage('likes', @debate) %></span>
|
||||||
<% end %>
|
<% end %>
|
||||||
@@ -9,7 +10,8 @@
|
|||||||
<span class="divider"></span>
|
<span class="divider"></span>
|
||||||
|
|
||||||
<div id="against" class="inline-block">
|
<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>
|
<i class="fi-dislike"></i>
|
||||||
<span><%= percentage('dislikes', @debate) %></span>
|
<span><%= percentage('dislikes', @debate) %></span>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|||||||
1
app/views/debates/vote.js.erb
Normal file
1
app/views/debates/vote.js.erb
Normal file
@@ -0,0 +1 @@
|
|||||||
|
$("#<%= dom_id(@debate) %>_votes").html('<%= j render("debates/#{params[:partial]}", debate: @debate) %>');
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
$("#<%= dom_id(@resource) %>_votes").
|
|
||||||
html('<%= j render("#{@resource_name.pluralize}/votes", comment: @resource) %>');
|
|
||||||
@@ -7,16 +7,17 @@ Rails.application.routes.draw do
|
|||||||
# You can have the root of your site routed with "root"
|
# You can have the root of your site routed with "root"
|
||||||
root 'debates#index'
|
root 'debates#index'
|
||||||
|
|
||||||
concern :votable do |options|
|
|
||||||
resources :votes, options.merge(only: :create)
|
|
||||||
end
|
|
||||||
|
|
||||||
resources :debates do
|
resources :debates do
|
||||||
concerns :votable, votable_type: 'debate'
|
member do
|
||||||
|
post :vote
|
||||||
resources :comments, only: :create do
|
|
||||||
concerns :votable, votable_type: 'comment'
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
resources :comments, only: :create, shallow: true do
|
||||||
|
member do
|
||||||
|
post :vote
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
resource :account, controller: "account", only: [:show, :update]
|
resource :account, controller: "account", only: [:show, :update]
|
||||||
|
|||||||
Reference in New Issue
Block a user