Merge pull request #108 from AyuntamientoMadrid/voted-css
Adds CSS class to voted options
This commit is contained in:
@@ -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
|
||||||
|
|||||||
14
app/helpers/votes_helper.rb
Normal file
14
app/helpers/votes_helper.rb
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
module VotesHelper
|
||||||
|
|
||||||
|
def css_classes_for_debate_vote(voted_values, debate)
|
||||||
|
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
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
@@ -2,6 +2,8 @@ class User < ActiveRecord::Base
|
|||||||
devise :database_authenticatable, :registerable, :confirmable,
|
devise :database_authenticatable, :registerable, :confirmable,
|
||||||
:recoverable, :rememberable, :trackable, :validatable
|
:recoverable, :rememberable, :trackable, :validatable
|
||||||
|
|
||||||
|
acts_as_voter
|
||||||
|
|
||||||
validates :first_name, presence: true, unless: :use_nickname?
|
validates :first_name, presence: true, unless: :use_nickname?
|
||||||
validates :last_name, presence: true, unless: :use_nickname?
|
validates :last_name, presence: true, unless: :use_nickname?
|
||||||
validates :nickname, presence: true, if: :use_nickname?
|
validates :nickname, presence: true, if: :use_nickname?
|
||||||
@@ -9,4 +11,12 @@ class User < ActiveRecord::Base
|
|||||||
def name
|
def name
|
||||||
use_nickname? ? nickname : "#{first_name} #{last_name}"
|
use_nickname? ? nickname : "#{first_name} #{last_name}"
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<div class="small-12 medium-4 column">
|
<div class="small-12 medium-4 column">
|
||||||
<div id="debate-<%= featured_debate.id %>" class="debate-featured">
|
<div id="<%= dom_id featured_debate %>" class="debate-featured">
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
|
|
||||||
<div class="debate-content">
|
<div class="debate-content">
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
<% voted_classes = css_classes_for_debate_vote(@voted_values, debate) %>
|
||||||
<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 +11,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 %>
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ feature 'Tags' do
|
|||||||
|
|
||||||
visit debates_path
|
visit debates_path
|
||||||
|
|
||||||
within "#debate-#{earth.id}" do
|
within "#debate_#{earth.id}" do
|
||||||
expect(page).to have_content "Medio Ambiente"
|
expect(page).to have_content "Medio Ambiente"
|
||||||
end
|
end
|
||||||
|
|
||||||
within "#debate-#{money.id}" do
|
within "#debate_#{money.id}" do
|
||||||
expect(page).to have_content "Economía"
|
expect(page).to have_content "Economía"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
@@ -49,10 +102,12 @@ feature 'Votes' do
|
|||||||
|
|
||||||
within('.in-favor') do
|
within('.in-favor') do
|
||||||
expect(page).to have_content "100%"
|
expect(page).to have_content "100%"
|
||||||
|
expect(page).to have_css("a.voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
within('.against') do
|
within('.against') do
|
||||||
expect(page).to have_content "0%"
|
expect(page).to have_content "0%"
|
||||||
|
expect(page).to have_css("a.no-voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(page).to have_content "1 vote"
|
expect(page).to have_content "1 vote"
|
||||||
@@ -66,10 +121,12 @@ feature 'Votes' do
|
|||||||
|
|
||||||
within('.in-favor') do
|
within('.in-favor') do
|
||||||
expect(page).to have_content "100%"
|
expect(page).to have_content "100%"
|
||||||
|
expect(page).to have_css("a.voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
within('.against') do
|
within('.against') do
|
||||||
expect(page).to have_content "0%"
|
expect(page).to have_content "0%"
|
||||||
|
expect(page).to have_css("a.no-voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(page).to have_content "1 vote"
|
expect(page).to have_content "1 vote"
|
||||||
@@ -88,10 +145,12 @@ feature 'Votes' do
|
|||||||
|
|
||||||
within('.in-favor') do
|
within('.in-favor') do
|
||||||
expect(page).to have_content "100%"
|
expect(page).to have_content "100%"
|
||||||
|
expect(page).to have_css("a.voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
within('.against') do
|
within('.against') do
|
||||||
expect(page).to have_content "0%"
|
expect(page).to have_content "0%"
|
||||||
|
expect(page).to have_css("a.no-voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(page).to have_content "1 vote"
|
expect(page).to have_content "1 vote"
|
||||||
@@ -105,10 +164,12 @@ 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 have_css("a.no-voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
within('.against') do
|
within('.against') do
|
||||||
expect(page).to have_content "100%"
|
expect(page).to have_content "100%"
|
||||||
|
expect(page).to have_css("a.voted")
|
||||||
end
|
end
|
||||||
|
|
||||||
expect(page).to have_content "1 vote"
|
expect(page).to have_content "1 vote"
|
||||||
|
|||||||
@@ -1,6 +1,33 @@
|
|||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
describe User do
|
describe User do
|
||||||
|
|
||||||
|
describe "#votes_on_debates" do
|
||||||
|
before(:each) do
|
||||||
|
@user = create(:user)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
subject { build(:user) }
|
subject { build(:user) }
|
||||||
|
|
||||||
it "is valid" do
|
it "is valid" do
|
||||||
@@ -46,5 +73,4 @@ describe User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user