diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 00fd20653..409bb7c9b 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -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) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 2e3a07e3e..8ccf33f6a 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -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 @@ -38,6 +38,10 @@ class DebatesController < ApplicationController respond_with @debate end + def vote + @debate.vote_by(voter: current_user, vote: params[:value]) + end + private def set_debate diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb deleted file mode 100644 index e93968f35..000000000 --- a/app/controllers/votes_controller.rb +++ /dev/null @@ -1,21 +0,0 @@ -class VotesController < ApplicationController - before_action :set_debate - 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_debate - @debate = Debate.find(params[:debate_id]) - end - - def register_vote - @debate.vote_by voter: current_user, vote: params[:value] - end -end diff --git a/app/models/comment.rb b/app/models/comment.rb index 91d7447c0..e99ef15e3 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -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 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..2f92ff246 --- /dev/null +++ b/app/views/comments/_votes.html.erb @@ -0,0 +1,11 @@ + + <%= link_to "up", vote_comment_path(comment, value: 'yes'), + method: "post", remote: true %> + <%= comment.get_likes.size %> + + + + <%= link_to "down", vote_comment_path(comment, value: 'no'), + method: "post", remote: true %> + <%= comment.get_dislikes.size %> + \ No newline at end of file diff --git a/app/views/comments/vote.js.erb b/app/views/comments/vote.js.erb new file mode 100644 index 000000000..11f0ad905 --- /dev/null +++ b/app/views/comments/vote.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@comment) %>_votes").html('<%= j render("comments/votes", comment: @comment) %>'); \ No newline at end of file diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index f37f1c5bc..86f58f9e0 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -14,25 +14,11 @@ <%= render "shared/tags", debate: debate %> -
-
- <%= link_to debate_votes_path(debate, value: 'yes'), class: "like inline-block", title: t('votes.agree'), method: "post" do %> - - <%= percentage('likes', debate) %> - <% end %> - - - <%= link_to debate_votes_path(debate, value: 'no'), class: "unlike inline-block", title: t('votes.disagree'), method: "post" do %> - - <%= percentage('dislikes', debate) %> - <% end %> -
- - <%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %> - -
+
+ <%= render 'debates/votes_min', debate: debate %>
+
diff --git a/app/views/debates/_featured_debate.html.erb b/app/views/debates/_featured_debate.html.erb index 6f74b0ed7..e4f8cb3ad 100644 --- a/app/views/debates/_featured_debate.html.erb +++ b/app/views/debates/_featured_debate.html.erb @@ -16,22 +16,8 @@
-
- <%= link_to debate_votes_path(featured_debate, value: "yes"), class: "like", title: t('votes.agree'), method: "post" do %> - - <%= percentage('likes', featured_debate) %> - <% end %> - - - - <%= link_to debate_votes_path(featured_debate, value: "no"), class: "unlike", title: t('votes.disagree'), method: "post" do %> - - <%= percentage('dislikes', featured_debate) %> - <% end %> - - - <%= pluralize(featured_debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %> - +
+ <%= render 'debates/featured_debate_votes', debate: featured_debate %>
diff --git a/app/views/debates/_featured_debate_votes.html.erb b/app/views/debates/_featured_debate_votes.html.erb new file mode 100644 index 000000000..f216aad77 --- /dev/null +++ b/app/views/debates/_featured_debate_votes.html.erb @@ -0,0 +1,21 @@ + + <%= link_to vote_debate_path(debate, value: 'yes', partial: 'featured_debate_votes'), + class: "like", title: t('votes.agree'), method: "post", remote: true do %> + + <%= percentage('likes', debate) %> + <% end %> + + + + + + <%= link_to vote_debate_path(debate, value: 'no', partial: 'featured_debate_votes'), + class: "unlike", title: t('votes.disagree'), method: "post", remote: true do %> + + <%= percentage('dislikes', debate) %> + <% end %> + + + + <%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %> + \ No newline at end of file diff --git a/app/views/votes/_votes.html.erb b/app/views/debates/_votes.html.erb similarity index 63% rename from app/views/votes/_votes.html.erb rename to app/views/debates/_votes.html.erb index b88de32a9..c4339572a 100644 --- a/app/views/votes/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -7,7 +7,8 @@
- <%= 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 %> <%= percentage('likes', @debate) %> <% end %> @@ -16,7 +17,8 @@
- <%= 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 %> <%= percentage('dislikes', @debate) %> <% end %> diff --git a/app/views/debates/_votes_min.html.erb b/app/views/debates/_votes_min.html.erb new file mode 100644 index 000000000..204890293 --- /dev/null +++ b/app/views/debates/_votes_min.html.erb @@ -0,0 +1,22 @@ +
+ + <%= link_to vote_debate_path(debate, value: 'yes', partial: 'votes_min'), + class: "like inline-block", title: t('votes.agree'), method: "post", remote: true do %> + + <%= percentage('likes', debate) %> + <% end %> + + + + + <%= link_to vote_debate_path(debate, value: 'no', partial: 'votes_min'), + class: "unlike inline-block", title: t('votes.disagree'), method: "post", remote: true do %> + + <%= percentage('dislikes', debate) %> + <% end %> + +
+ + <%= pluralize(debate.total_votes, t("debates.debate.vote"), t("debates.debate.votes")) %> + +
\ No newline at end of file diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 765ede640..3fa88bdda 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -10,8 +10,8 @@ <%= @debate.description %>

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

-
- <%= render 'votes/votes' %> +
+ <%= render 'debates/votes' %>
<%= link_to t("debates.show.leave_comment"), "#comments", class: "leave-comment" %>
diff --git a/app/views/debates/vote.js.erb b/app/views/debates/vote.js.erb new file mode 100644 index 000000000..e98725bb2 --- /dev/null +++ b/app/views/debates/vote.js.erb @@ -0,0 +1 @@ +$("#<%= dom_id(@debate) %>_votes").html('<%= j render("debates/#{params[:partial]}", debate: @debate) %>'); \ No newline at end of file diff --git a/app/views/votes/create.js.erb b/app/views/votes/create.js.erb deleted file mode 100644 index 5e855e40e..000000000 --- a/app/views/votes/create.js.erb +++ /dev/null @@ -1 +0,0 @@ -$("#votes").html("<%= j render('votes') %>"); \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cd95b8461..73a77f60e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,9 +6,18 @@ Rails.application.routes.draw do # 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] diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index d1aabd84a..3e44f4f0e 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -2,74 +2,194 @@ require 'rails_helper' feature 'Votes' do - background do - @manuela = create(:user) - @pablo = create(:user) - @debate = create(:debate) + feature 'Debates' do + + background do + @manuela = create(:user) + @pablo = create(:user) + @debate = create(:debate) + + login_as(@manuela) + visit debate_path(@debate) + end + + scenario 'Show' do + vote = create(:vote, voter: @manuela, votable: @debate, vote_flag: true) + vote = create(:vote, voter: @pablo, votable: @debate, vote_flag: false) + + visit debate_path(@debate) + + expect(page).to have_content "2 votes" + + within('#in_favor') do + expect(page).to have_content "50%" + end + + within('#against') do + expect(page).to have_content "50%" + end + end + + scenario 'Create from debate show', :js do + find('#in_favor a').click + + within('#in_favor') do + expect(page).to have_content "100%" + end + + within('#against') do + expect(page).to have_content "0%" + end + + expect(page).to have_content "1 vote" + end + + scenario 'Create from debate featured', :js do + visit debates_path + + within("#featured-debates") do + find('#in_favor a').click + + within('#in_favor') do + expect(page).to have_content "100%" + end + + within('#against') do + expect(page).to have_content "0%" + end + + expect(page).to have_content "1 vote" + end + expect(URI.parse(current_url).path).to eq(debates_path) + end + + scenario 'Create from debate index', :js do + 3.times { create(:debate) } + visit debates_path + + within("#debates") do + expect(page).to have_css(".debate", count: 1) + + find('#in_favor a').click + + within('#in_favor') do + expect(page).to have_content "100%" + end + + within('#against') do + expect(page).to have_content "0%" + end + + expect(page).to have_content "1 vote" + end + expect(URI.parse(current_url).path).to eq(debates_path) + end + + scenario 'Update', :js do + find('#in_favor a').click + find('#against a').click + + within('#in_favor') do + expect(page).to have_content "0%" + end + + within('#against') do + expect(page).to have_content "100%" + end + + expect(page).to have_content "1 vote" + end + + scenario 'Trying to vote multiple times', :js do + find('#in_favor a').click + find('#in_favor a').click + + within('#in_favor') do + expect(page).to have_content "100%" + end + + within('#against') do + expect(page).to have_content "0%" + end + + expect(page).to have_content "1 vote" + end - login_as(@manuela) - visit debate_path(@debate) end - scenario 'Show' do - vote = create(:vote, voter: @manuela, votable: @debate, vote_flag: true) - vote = create(:vote, voter: @pablo, votable: @debate, vote_flag: false) - visit debate_path(@debate) + feature 'Comments' do - expect(page).to have_content "2 votes" + background do + @manuela = create(:user) + @pablo = create(:user) + @debate = create(:debate) + @comment = create(:comment, commentable: @debate) - within('#in_favor') do - expect(page).to have_content "50%" + login_as(@manuela) + visit debate_path(@debate) end - within('#against') do - expect(page).to have_content "50%" + scenario 'Show' do + vote = create(:vote, voter: @manuela, votable: @comment, vote_flag: true) + vote = create(:vote, voter: @pablo, votable: @comment, vote_flag: false) + + visit debate_path(@debate) + + within("#comment_#{@comment.id}_votes") do + within(".in_favor") do + expect(page).to have_content "1" + end + + within(".against") do + expect(page).to have_content "1" + end + end end + + scenario 'Create', :js do + within("#comment_#{@comment.id}_votes") do + find(".in_favor a").click + + within(".in_favor") do + expect(page).to have_content "1" + end + + within(".against") do + expect(page).to have_content "0" + end + end + end + + scenario 'Update', :js do + within("#comment_#{@comment.id}_votes") do + find('.in_favor a').click + find('.against a').click + + within('.in_favor') do + expect(page).to have_content "0" + end + + within('.against') do + expect(page).to have_content "1" + end + end + end + + scenario 'Trying to vote multiple times', :js do + within("#comment_#{@comment.id}_votes") do + find('.in_favor a').click + find('.in_favor a').click + + within('.in_favor') do + expect(page).to have_content "1" + end + + within('.against') do + expect(page).to have_content "0" + end + end + end + end - - scenario 'Create', :js do - find('#in_favor a').click - - within('#in_favor') do - expect(page).to have_content "100%" - end - - within('#against') do - expect(page).to have_content "0%" - end - - expect(page).to have_content "1 vote" - end - - scenario 'Update', :js do - find('#in_favor a').click - find('#against a').click - - within('#in_favor') do - expect(page).to have_content "0%" - end - - within('#against') do - expect(page).to have_content "100%" - end - - expect(page).to have_content "1 vote" - end - - scenario 'Trying to vote multiple times', :js do - find('#in_favor a').click - find('#in_favor a').click - - within('#in_favor') do - expect(page).to have_content "100%" - end - - within('#against') do - expect(page).to have_content "0%" - end - - expect(page).to have_content "1 vote" - end - end \ No newline at end of file