adds css classes to voted options in debates

for logged users.
Closes #88
This commit is contained in:
Juanjo Bazán
2015-08-06 18:53:05 +02:00
parent 6e7832957a
commit 33489e7b0c
3 changed files with 72 additions and 2 deletions

View File

@@ -7,13 +7,16 @@ class DebatesController < ApplicationController
def index def index
if params[:tag] if params[:tag]
@debates = Debate.tagged_with(params[:tag]).order("created_at DESC") @debates = Debate.tagged_with(params[:tag]).order("created_at DESC")
set_voted_values @debates.map(&:id)
else else
@debates = Debate.all.order("created_at DESC") @debates = Debate.all.order("created_at DESC")
set_voted_values @debates.map(&:id)
@featured_debates = @debates.to_a.shift(3) @featured_debates = @debates.to_a.shift(3)
end end
end end
def show def show
set_voted_values [@debate.id]
end end
def new def new
@@ -40,6 +43,7 @@ class DebatesController < ApplicationController
def vote def vote
@debate.vote_by(voter: current_user, vote: params[:value]) @debate.vote_by(voter: current_user, vote: params[:value])
set_voted_values [@debate.id]
end end
@@ -55,4 +59,8 @@ class DebatesController < ApplicationController
def validate_ownership def validate_ownership
raise ActiveRecord::RecordNotFound unless @debate.editable_by?(current_user) raise ActiveRecord::RecordNotFound unless @debate.editable_by?(current_user)
end end
def set_voted_values(debates_ids)
@voted_values = current_user ? current_user.votes_on_debates(debates_ids) : {}
end
end end

View File

@@ -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 %>
<div class="votes"> <div class="votes">
<div class="in-favor inline-block"> <div class="in-favor inline-block">
<%= link_to vote_debate_path(debate, value: 'yes'), <%= 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 %>
<i class="icon-like"></i> <i class="icon-like"></i>
<span><%= percentage('likes', debate) %></span> <span><%= percentage('likes', debate) %></span>
<% end %> <% end %>
@@ -10,7 +19,7 @@
<span class="divider"></span> <span class="divider"></span>
<div class="against inline-block"> <div class="against inline-block">
<%= 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 %>
<i class="icon-unlike"></i> <i class="icon-unlike"></i>
<span><%= percentage('dislikes', debate) %></span> <span><%= percentage('dislikes', debate) %></span>
<% end %> <% end %>

View File

@@ -13,6 +13,53 @@ feature 'Votes' do
visit debate_path(@debate) visit debate_path(@debate)
end 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 scenario 'Show no votes' do
visit debate_path(@debate) visit debate_path(@debate)
@@ -20,10 +67,14 @@ feature 'Votes' do
within('.in-favor') do within('.in-favor') do
expect(page).to have_content "0%" 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
within('.against') do within('.against') do
expect(page).to have_content "0%" 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
end end
@@ -37,10 +88,12 @@ feature 'Votes' do
within('.in-favor') do within('.in-favor') do
expect(page).to have_content "50%" expect(page).to have_content "50%"
expect(page).to have_css("a.voted")
end end
within('.against') do within('.against') do
expect(page).to have_content "50%" expect(page).to have_content "50%"
expect(page).to have_css("a.no-voted")
end end
end end