Skip to content

Commit

Permalink
Merge pull request consuldemocracy#94 from AyuntamientoMadrid/vote_co…
Browse files Browse the repository at this point in the history
…mments-25

refactors voting
  • Loading branch information
xuanxu committed Aug 5, 2015
2 parents cf1b72b + 062ef36 commit c18bc06
Show file tree
Hide file tree
Showing 17 changed files with 263 additions and 110 deletions.
8 changes: 7 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,6 +10,12 @@ def create
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)
Expand Down
8 changes: 6 additions & 2 deletions app/controllers/debates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class DebatesController < ApplicationController
include RecaptchaHelper
before_action :set_debate, only: [:show, :edit, :update]
before_action :authenticate_user!, except: [:show, :index]
before_action :set_debate, only: [:show, :edit, :update, :vote]
before_action :authenticate_user!, except: [:index, :show]
before_action :validate_ownership, only: [:edit, :update]

def index
Expand Down Expand Up @@ -38,6 +38,10 @@ def update
respond_with @debate
end

def vote
@debate.vote_by(voter: current_user, vote: params[:value])
end


private
def set_debate
Expand Down
21 changes: 0 additions & 21 deletions app/controllers/votes_controller.rb

This file was deleted.

1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Comment < ActiveRecord::Base
acts_as_nested_set scope: [:commentable_id, :commentable_type]
acts_as_votable

validates :body, presence: true
validates :user, presence: true
Expand Down
5 changes: 5 additions & 0 deletions app/views/comments/_comment.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
11 changes: 11 additions & 0 deletions app/views/comments/_votes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<span class="in_favor">
<%= link_to "up", vote_comment_path(comment, value: 'yes'),
method: "post", remote: true %>
<%= comment.get_likes.size %>
</span>

<span class="against">
<%= link_to "down", vote_comment_path(comment, value: 'no'),
method: "post", remote: true %>
<%= comment.get_dislikes.size %>
</span>
1 change: 1 addition & 0 deletions app/views/comments/vote.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#<%= dom_id(@comment) %>_votes").html('<%= j render("comments/votes", comment: @comment) %>');
20 changes: 3 additions & 17 deletions app/views/debates/_debate.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,11 @@
<%= render "shared/tags", debate: debate %>
</div>
</div>
<div class="small-12 medium-3 column">
<div class="text-center votes">
<%= link_to debate_votes_path(debate, value: 'yes'), class: "like inline-block", title: t('votes.agree'), method: "post" do %>
<i class="icon-like"></i>
<span><%= percentage('likes', debate) %></span>
<% end %>

<span class="divider"></span>

<%= link_to debate_votes_path(debate, value: 'no'), class: "unlike inline-block", title: t('votes.disagree'), method: "post" do %>
<i class="icon-unlike"></i>
<span><%= percentage('dislikes', debate) %></span>
<% end %>
<br>
<span class="total-votes">
<%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %>
</span>
</div>
<div id="<%= dom_id(debate) %>_votes" class="small-12 medium-3 column">
<%= render 'debates/votes_min', debate: debate %>
</div>

</div>
</div>
</div>
Expand Down
18 changes: 2 additions & 16 deletions app/views/debates/_featured_debate.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,8 @@
</div>

<div class="row votes">
<div class="small-12 column">
<%= link_to debate_votes_path(featured_debate, value: "yes"), class: "like", title: t('votes.agree'), method: "post" do %>
<i class="icon-like"></i>
<span><%= percentage('likes', featured_debate) %></span>
<% end %>

<span class="divider"></span>

<%= link_to debate_votes_path(featured_debate, value: "no"), class: "unlike", title: t('votes.disagree'), method: "post" do %>
<i class="icon-unlike"></i>
<span><%= percentage('dislikes', featured_debate) %></span>
<% end %>

<span class="total-votes right">
<%= pluralize(featured_debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %>
</span>
<div id="<%= dom_id(featured_debate) %>_votes" class="small-12 column">
<%= render 'debates/featured_debate_votes', debate: featured_debate %>
</div>
</div>
</div>
Expand Down
21 changes: 21 additions & 0 deletions app/views/debates/_featured_debate_votes.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<span id="in_favor">
<%= link_to vote_debate_path(debate, value: 'yes', partial: 'featured_debate_votes'),
class: "like", title: t('votes.agree'), method: "post", remote: true do %>
<i class="icon-like"></i>
<span><%= percentage('likes', debate) %></span>
<% end %>
</span>

<span class="divider"></span>

<span id="against">
<%= link_to vote_debate_path(debate, value: 'no', partial: 'featured_debate_votes'),
class: "unlike", title: t('votes.disagree'), method: "post", remote: true do %>
<i class="icon-unlike"></i>
<span><%= percentage('dislikes', debate) %></span>
<% end %>
</span>

<span class="total-votes right">
<%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %>
</span>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

<div class="text-center">
<div id="in_favor" class="inline-block">
<%= link_to debate_votes_path(@debate, value: 'yes'), class: "like", title: t('votes.agree'), method: "post", remote: true do %>
<%= link_to vote_debate_path(@debate, value: 'yes', partial: 'votes'),
class: "like", title: t('votes.agree'), method: "post", remote: true do %>
<i class="icon-like"></i>
<span><%= percentage('likes', @debate) %></span>
<% end %>
Expand All @@ -16,7 +17,8 @@
<span class="divider"></span>

<div id="against" class="inline-block">
<%= link_to debate_votes_path(@debate, value: 'no'), class: "unlike", title: t('votes.disagree'), method: "post", remote: true do %>
<%= link_to vote_debate_path(@debate, value: 'no', partial: 'votes'),
class: "unlike", title: t('votes.disagree'), method: "post", remote: true do %>
<i class="icon-unlike"></i>
<span><%= percentage('dislikes', @debate) %></span>
<% end %>
Expand Down
22 changes: 22 additions & 0 deletions app/views/debates/_votes_min.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<div class="text-center votes">
<span id="in_favor">
<%= link_to vote_debate_path(debate, value: 'yes', partial: 'votes_min'),
class: "like inline-block", title: t('votes.agree'), method: "post", remote: true do %>
<i class="icon-like"></i>
<span><%= percentage('likes', debate) %></span>
<% end %>
</span>
<span class="divider"></span>

<span id="against">
<%= link_to vote_debate_path(debate, value: 'no', partial: 'votes_min'),
class: "unlike inline-block", title: t('votes.disagree'), method: "post", remote: true do %>
<i class="icon-unlike"></i>
<span><%= percentage('dislikes', debate) %></span>
<% end %>
</span>
<br>
<span class="total-votes">
<%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %>
</span>
</div>
4 changes: 2 additions & 2 deletions app/views/debates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<%= @debate.description %>
<p><%= render 'shared/tags', debate: @debate %></p>
</div>
<div id="votes" class="small-12 medium-3 column votes">
<%= render 'votes/votes' %>
<div id="<%= dom_id(@debate) %>_votes" class="votes small-12 medium-3 column">
<%= render 'debates/votes' %>
<div class="text-center">
<%= link_to t("debates.show.leave_comment"), "#comments", class: "leave-comment" %>
</div>
Expand Down
1 change: 1 addition & 0 deletions app/views/debates/vote.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$("#<%= dom_id(@debate) %>_votes").html('<%= j render("debates/#{params[:partial]}", debate: @debate) %>');
1 change: 0 additions & 1 deletion app/views/votes/create.js.erb

This file was deleted.

13 changes: 11 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@

# You can have the root of your site routed with "root"
root 'debates#index'

resources :debates do
resources :votes, only: :create
resources :comments, only: :create
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]
Expand Down
Loading

0 comments on commit c18bc06

Please sign in to comment.