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