adds proposal votes

This commit is contained in:
rgarcia
2015-09-13 21:56:52 +02:00
parent d4b3e8be94
commit ad8880d02c
13 changed files with 311 additions and 26 deletions

View File

@@ -12,8 +12,5 @@ App.Votes =
initialize: ->
App.Votes.hoverize votes for votes in $("div.votes")
App.Votes.hoverize votes for votes in $("div.supports")
false

View File

@@ -67,6 +67,11 @@ class ProposalsController < ApplicationController
respond_with @proposal, template: 'proposals/_refresh_flag_actions'
end
def vote
@proposal.register_vote(current_user, 'yes')
set_proposal_votes(@proposal)
end
private
def proposal_params

View File

@@ -1,7 +1,7 @@
module VotesHelper
def css_classes_for_debate_vote(debate_votes, debate)
case debate_votes[debate.id]
def css_classes_for_vote(votes, votable)
case votes[votable.id]
when true
{in_favor: "voted", against: "no-voted"}
when false
@@ -11,13 +11,4 @@ module VotesHelper
end
end
def css_classes_for_proposal_vote(proposal_votes, proposal)
case proposal_votes[proposal.id]
when true
{in_favor: "voted"}
else
{in_favor: ""}
end
end
end

View File

@@ -70,7 +70,16 @@ class Proposal < ActiveRecord::Base
end
def votable_by?(user)
user.level_two_verified? || !user.voted_for?(self)
user.level_two_or_three_verified?
end
def register_vote(user, vote_value)
return unless votable_by?(user)
if user.voted_for?(self)
unvote_by(user)
else
vote_by(voter: user, vote: vote_value)
end
end
def code

View File

@@ -1,4 +1,4 @@
<% voted_classes = css_classes_for_debate_vote(@debate_votes, debate) %>
<% voted_classes = css_classes_for_vote(@debate_votes, debate) %>
<div class="votes">
<div class="in-favor inline-block">
<%= link_to vote_debate_path(debate, value: 'yes'),

View File

@@ -1,4 +1,4 @@
<% voted_classes = css_classes_for_proposal_vote(@proposal_votes, proposal) %>
<% voted_classes = css_classes_for_vote(@proposal_votes, proposal) %>
<div class="supports">
<div class="progress small-12 radius">
@@ -9,10 +9,12 @@
<%= t("proposals.proposal.supports", count: proposal.total_votes) %>&nbsp;<span><%= t("proposals.proposal.supports_necessary") %></span>
</span>
<div class="in-favor">
<%= link_to vote_proposal_path(proposal, value: 'yes'), class: "button button-support tiny radius expand #{voted_classes[:in_favor]}",
title: t('proposals.proposal.support'), method: "post", remote: true do %>
<%= t("proposals.proposal.support") %>
<% end %>
</div>
<% if user_signed_in? && current_user.organization? %>
<div class="organizations-votes" style='display:none'>
@@ -23,7 +25,7 @@
<% elsif user_signed_in? && !proposal.votable_by?(current_user)%>
<div class="anonymous-votes" style='display:none'>
<p>
<%= t("votes.anonymous",
<%= t("votes.verified_only",
verify_account: link_to(t("votes.verify_account"), verification_path )).html_safe %>
</p>
</div>

View File

@@ -0,0 +1 @@
$("#<%= dom_id(@proposal) %>_votes").html('<%= j render("proposals/votes", proposal: @proposal) %>');

View File

@@ -4,7 +4,7 @@ ActsAsVotable::Vote.class_eval do
end
def self.for_proposals(proposals)
where(votable_type: 'Debate', votable_id: proposals)
where(votable_type: 'Proposal', votable_id: proposals)
end
def value

View File

@@ -244,7 +244,8 @@ en:
unauthenticated: "You need to %{signin} or %{signup} before continuing."
signin: sign in
signup: sign up
anonymous: "This debate already has too much anonymous votes, %{verify_account} to vote."
anonymous: "Too many anonymous votes, %{verify_account} to vote."
verified_only: "This proposal can only be voted by verified users, %{verify_account}."
verify_account: verify your account
organizations: Organizations can not vote
account:

View File

@@ -244,7 +244,8 @@ es:
unauthenticated: "Necesitas %{signin} o %{signup} para continuar."
signin: iniciar sesión
signup: registrarte
anonymous: "Esta propuesta ya tiene demasiados votos anónimos, para poder votar %{verify_account}."
anonymous: "Demasiados votos anónimos, para poder votar %{verify_account}."
verified_only: "Esta propuesta sólo puede ser votada por usuarios verificados, %{verify_account}."
verify_account: verifica tu cuenta
organizations: Las organizaciones no pueden votar
account:

View File

@@ -10,7 +10,9 @@ feature 'Votes' do
end
feature 'Debates' do
scenario "Home shows user votes on featured debates" do
xscenario "Home shows user votes on featured debates" do
pending "logged in user cannot see this page"
debate1 = create(:debate)
debate2 = create(:debate)
debate3 = create(:debate)
@@ -200,7 +202,8 @@ feature 'Votes' do
expect(page).to have_content "1 vote"
end
scenario 'Create in featured', :js do
xscenario 'Create in featured', :js do
pending "logged in user cannot see this page"
visit root_path
find('.in-favor a').click
@@ -241,6 +244,52 @@ feature 'Votes' do
expect(current_path).to eq(debates_path)
end
end
scenario 'Not logged user trying to vote', :js do
debate = create(:debate)
visit "/"
click_link "Logout"
within("#debate_#{debate.id}") do
find("div.votes").hover
expect_message_you_need_to_sign_in
end
visit debates_path
within("#debate_#{debate.id}") do
find("div.votes").hover
expect_message_you_need_to_sign_in
end
visit debate_path(debate)
within("#debate_#{debate.id}") do
find("div.votes").hover
expect_message_you_need_to_sign_in
end
end
scenario 'Anonymous user trying to vote', :js do
user = create(:user)
debate = create(:debate)
Setting.find_by(key: "max_ratio_anon_votes_on_debates").update(value: 50)
debate.update(cached_anonymous_votes_total: 520, cached_votes_total: 1000)
login_as(user)
visit debates_path
within("#debate_#{debate.id}") do
find("div.votes").hover
expect_message_to_many_anonymous_votes
end
visit debate_path(debate)
within("#debate_#{debate.id}") do
find("div.votes").hover
expect_message_to_many_anonymous_votes
end
end
end
feature 'Comments' do
@@ -324,4 +373,199 @@ feature 'Votes' do
end
end
end
feature 'Proposals' do
xscenario "Home shows user votes on featured proposals" do
pending "logged in user cannot see this page"
proposal1 = create(:proposal)
proposal2 = create(:proposal)
proposal3 = create(:proposal)
create(:vote, voter: @manuela, votable: proposal1, vote_flag: true)
create(:vote, voter: @manuela, votable: proposal3, vote_flag: false)
visit root_path
within("#featured-proposals") do
within("#proposal_#{proposal1.id}_votes") do
within(".supports") do
expect(page).to have_css("a.voted")
expect(page).to_not have_css("a.no-voted")
end
end
within("#proposal_#{proposal2.id}_votes") do
within(".supports") do
expect(page).to_not have_css("a.voted")
expect(page).to_not have_css("a.no-voted")
end
end
within("#proposal_#{proposal3.id}_votes") do
within(".supports") do
expect(page).to have_css("a.no-voted")
expect(page).to_not have_css("a.voted")
end
end
end
end
scenario "Index shows user votes on proposals" do
proposal1 = create(:proposal)
proposal2 = create(:proposal)
proposal3 = create(:proposal)
create(:vote, voter: @manuela, votable: proposal1, vote_flag: true)
create(:vote, voter: @manuela, votable: proposal3, vote_flag: false)
visit proposals_path
within("#proposals") do
within("#proposal_#{proposal1.id}_votes") do
expect(page).to have_css("a.voted")
expect(page).to_not have_css("a.no-voted")
end
within("#proposal_#{proposal2.id}_votes") do
expect(page).to_not have_css("a.voted")
expect(page).to_not have_css("a.no-voted")
end
within("#proposal_#{proposal3.id}_votes") do
expect(page).to have_css("a.no-voted")
expect(page).to_not have_css("a.voted")
end
end
end
feature 'Single proposal' do
background do
@proposal = create(:proposal)
end
scenario 'Show no votes' do
visit proposal_path(@proposal)
expect(page).to have_content "No supports"
within('.supports') do
expect(page).to_not have_css("a.voted")
expect(page).to_not have_css("a.no-voted")
end
end
scenario 'Update', :js do
visit proposal_path(@proposal)
within('.supports') do
find('.in-favor a').click
expect(page).to have_content "1 support"
expect(page).to have_css("a.voted")
find('.in-favor a').click
expect(page).to have_content "No supports"
expect(page).to_not have_css("a.no-voted")
end
end
scenario 'Trying to vote multiple times', :js do
visit proposal_path(@proposal)
within('.supports') do
find('.in-favor a').click
find('.in-favor a').click
expect(page).to have_content "1 support"
end
end
scenario 'Show' do
create(:vote, voter: @manuela, votable: @proposal, vote_flag: true)
create(:vote, voter: @pablo, votable: @proposal, vote_flag: true)
visit proposal_path(@proposal)
within('.supports') do
expect(page).to have_content "2 supports / 53.726"
end
end
scenario 'Create from proposal show', :js do
visit proposal_path(@proposal)
within('.supports') do
find('.in-favor a').click
expect(page).to have_content "1 support"
expect(page).to have_css("a.voted")
end
end
xscenario 'Create in featured', :js do
pending "logged in user cannot see this page"
visit root_path
within("#featured-proposals") do
find('.in-favor a').click
expect(page).to have_content "1 support"
expect(page).to have_css("a.voted")
end
expect(URI.parse(current_url).path).to eq(root_path)
end
scenario 'Create in index', :js do
visit proposals_path
within("#proposals") do
find('.in-favor a').click
expect(page).to have_content "1 support"
expect(page).to have_css("a.voted")
end
expect(URI.parse(current_url).path).to eq(proposals_path)
end
end
end
scenario 'Not logged user trying to vote', :js do
proposal = create(:proposal)
visit "/"
click_link "Logout"
within("#proposal_#{proposal.id}") do
find("div.supports").hover
expect_message_you_need_to_sign_in
end
visit proposals_path
within("#proposal_#{proposal.id}") do
find("div.supports").hover
expect_message_you_need_to_sign_in
end
visit proposal_path(proposal)
within("#proposal_#{proposal.id}") do
find("div.supports").hover
expect_message_you_need_to_sign_in
end
end
scenario "Anonymous user trying to vote", :js do
user = create(:user)
proposal = create(:proposal)
login_as(user)
visit proposals_path
within("#proposal_#{proposal.id}") do
find("div.supports").hover
expect_message_only_verified_can_vote
end
visit proposal_path(proposal)
within("#proposal_#{proposal.id}") do
find("div.supports").hover
expect_message_only_verified_can_vote
end
end
end

View File

@@ -73,4 +73,23 @@ describe Proposal do
expect(proposal.editable?).to be false
end
end
describe "#votable_by?" do
let(:proposal) { create(:proposal) }
it "should be true for level two verified users" do
user = create(:user, residence_verified_at: Time.now, confirmed_phone: "666333111")
expect(proposal.votable_by?(user)).to be true
end
it "should be true for level three verified users" do
user = create(:user, verified_at: Time.now)
expect(proposal.votable_by?(user)).to be true
end
it "should be false for anonymous users" do
user = create(:user)
expect(proposal.votable_by?(user)).to be false
end
end
end

View File

@@ -135,4 +135,19 @@ module CommonActions
expect(page).to have_content 'Correct code'
end
def expect_message_you_need_to_sign_in
expect(page).to have_content 'You need to sign in or sign up before continuing'
expect(page).to have_selector('.in-favor a', visible: false)
end
def expect_message_to_many_anonymous_votes
expect(page).to have_content 'Too many anonymous votes, verify your account to vote.'
expect(page).to have_selector('.in-favor a', visible: false)
end
def expect_message_only_verified_can_vote
expect(page).to have_content 'This proposal can only be voted by verified users, verify your account.'
expect(page).to have_selector('.in-favor a', visible: false)
end
end