adds css classes to voted options in debates
for logged users. Closes #88
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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="in-favor inline-block">
|
||||
<%= 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>
|
||||
<span><%= percentage('likes', debate) %></span>
|
||||
<% end %>
|
||||
@@ -10,7 +19,7 @@
|
||||
<span class="divider"></span>
|
||||
|
||||
<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>
|
||||
<span><%= percentage('dislikes', debate) %></span>
|
||||
<% end %>
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user