From 6e7832957a486087f77a4a46d4842c6f27d7f6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 6 Aug 2015 17:23:11 +0200 Subject: [PATCH 1/5] adds method to get user votes on specific debates --- app/models/user.rb | 10 ++++++++++ spec/models/user_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index da1c3ab8d..55f70bb2e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,7 +2,17 @@ class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable, :trackable, :validatable + acts_as_voter + def name "#{first_name} #{last_name}" end + + def votes_on_debates(debates_ids = []) + debates_ids = debates_ids.flatten.compact.uniq + return {} if debates_ids.empty? + + voted = votes.where("votable_type = ? AND votable_id IN (?)", "Debate", debates_ids) + voted.each_with_object({}){ |v,_| _[v.votable_id] = v.vote_flag } + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 80c91c2d4..77b6755dc 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,4 +1,30 @@ require 'rails_helper' describe User do + + before(:each) do + @user = create(:user) + end + + describe "#votes_on_debates" do + it "should return {} if no debate" do + expect(@user.votes_on_debates()).to eq({}) + expect(@user.votes_on_debates([])).to eq({}) + expect(@user.votes_on_debates([nil, nil])).to eq({}) + end + + it "should return a hash of debates ids and votes" do + debate1 = create(:debate) + debate2 = create(:debate) + debate3 = create(:debate) + create(:vote, voter: @user, votable: debate1, vote_flag: true) + create(:vote, voter: @user, votable: debate3, vote_flag: false) + + voted = @user.votes_on_debates([debate1.id, debate2.id, debate3.id]) + + expect(voted[debate1.id]).to eq(true) + expect(voted[debate2.id]).to eq(nil) + expect(voted[debate3.id]).to eq(false) + end + end end From 33489e7b0cae5bc658bc258f666b137ca5f441d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 6 Aug 2015 18:53:05 +0200 Subject: [PATCH 2/5] adds css classes to voted options in debates for logged users. Closes #88 --- app/controllers/debates_controller.rb | 8 ++++ app/views/debates/_votes.html.erb | 13 ++++++- spec/features/votes_spec.rb | 53 +++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 713e8b97d..a395f9c5e 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -7,13 +7,16 @@ class DebatesController < ApplicationController def index if params[:tag] @debates = Debate.tagged_with(params[:tag]).order("created_at DESC") + set_voted_values @debates.map(&:id) else @debates = Debate.all.order("created_at DESC") + set_voted_values @debates.map(&:id) @featured_debates = @debates.to_a.shift(3) end end def show + set_voted_values [@debate.id] end def new @@ -40,6 +43,7 @@ class DebatesController < ApplicationController def vote @debate.vote_by(voter: current_user, vote: params[:value]) + set_voted_values [@debate.id] end @@ -55,4 +59,8 @@ class DebatesController < ApplicationController def validate_ownership raise ActiveRecord::RecordNotFound unless @debate.editable_by?(current_user) end + + def set_voted_values(debates_ids) + @voted_values = current_user ? current_user.votes_on_debates(debates_ids) : {} + end end diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index dadea65b2..ff664f725 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -1,7 +1,16 @@ +<% voted_classes = case @voted_values[debate.id] + when true + {in_favor: "voted", against: "no-voted"} + when false + {in_favor: "no-voted", against: "voted"} + else + {in_favor: "", against: ""} + end %> +
<%= link_to vote_debate_path(debate, value: 'yes'), - class: "like", title: t('votes.agree'), method: "post", remote: true do %> + class: "like #{voted_classes[:in_favor]}", title: t('votes.agree'), method: "post", remote: true do %> <%= percentage('likes', debate) %> <% end %> @@ -10,7 +19,7 @@
- <%= link_to vote_debate_path(debate, value: 'no'), class: "unlike", title: t('votes.disagree'), method: "post", remote: true do %> + <%= link_to vote_debate_path(debate, value: 'no'), class: "unlike #{voted_classes[:against]}", title: t('votes.disagree'), method: "post", remote: true do %> <%= percentage('dislikes', debate) %> <% end %> diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb index befec31af..f4b61d3e0 100644 --- a/spec/features/votes_spec.rb +++ b/spec/features/votes_spec.rb @@ -13,6 +13,53 @@ feature 'Votes' do visit debate_path(@debate) end + scenario "Index show user votes on debates" do + debate1 = create(:debate) + debate2 = create(:debate) + debate3 = create(:debate) + vote = create(:vote, voter: @manuela, votable: debate1, vote_flag: true) + vote = create(:vote, voter: @manuela, votable: debate3, vote_flag: false) + + visit debates_path + + within("#debate_#{debate1.id}_votes") do + within(".in-favor") do + expect(page).to have_css("a.voted") + expect(page).to_not have_css("a.no-voted") + end + + within(".against") do + expect(page).to have_css("a.no-voted") + expect(page).to_not have_css("a.voted") + end + end + + within("#debate_#{debate2.id}_votes") do + within(".in-favor") do + expect(page).to_not have_css("a.voted") + expect(page).to_not have_css("a.no-voted") + end + + within(".against") do + expect(page).to_not have_css("a.no-voted") + expect(page).to_not have_css("a.voted") + end + end + + within("#debate_#{debate3.id}_votes") do + within(".in-favor") do + expect(page).to have_css("a.no-voted") + expect(page).to_not have_css("a.voted") + end + + within(".against") do + expect(page).to have_css("a.voted") + expect(page).to_not have_css("a.no-voted") + end + end + + end + scenario 'Show no votes' do visit debate_path(@debate) @@ -20,10 +67,14 @@ feature 'Votes' do within('.in-favor') do expect(page).to have_content "0%" + expect(page).to_not have_css("a.voted") + expect(page).to_not have_css("a.no-voted") end within('.against') do expect(page).to have_content "0%" + expect(page).to_not have_css("a.voted") + expect(page).to_not have_css("a.no-voted") end end @@ -37,10 +88,12 @@ feature 'Votes' do within('.in-favor') do expect(page).to have_content "50%" + expect(page).to have_css("a.voted") end within('.against') do expect(page).to have_content "50%" + expect(page).to have_css("a.no-voted") end end From cdaa3e28080704a546b5394c46483e7afbe242cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juanjo=20Baz=C3=A1n?= Date: Thu, 6 Aug 2015 18:53:40 +0200 Subject: [PATCH 3/5] favors dom_id --- app/views/debates/_featured_debate.html.erb | 2 +- spec/features/tags_spec.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/debates/_featured_debate.html.erb b/app/views/debates/_featured_debate.html.erb index 708053a19..602b3d603 100644 --- a/app/views/debates/_featured_debate.html.erb +++ b/app/views/debates/_featured_debate.html.erb @@ -1,5 +1,5 @@
-