Merge pull request #9 from AyuntamientoMadrid/votes-9

Votes for debates
This commit is contained in:
Juanjo Bazán
2015-07-18 18:26:24 +02:00
12 changed files with 159 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ gem 'acts_as_commentable_with_threading'
gem 'acts-as-taggable-on' gem 'acts-as-taggable-on'
gem "responders" gem "responders"
gem 'foundation-rails' gem 'foundation-rails'
gem 'acts_as_votable'
group :development, :test do group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console # Call 'byebug' anywhere in the code to stop execution and get a debugger console

View File

@@ -42,6 +42,7 @@ GEM
activerecord (>= 4.0) activerecord (>= 4.0)
activesupport (>= 4.0) activesupport (>= 4.0)
awesome_nested_set (>= 3.0) awesome_nested_set (>= 3.0)
acts_as_votable (0.10.0)
arel (6.0.2) arel (6.0.2)
awesome_nested_set (3.0.2) awesome_nested_set (3.0.2)
activerecord (>= 4.0.0, < 5) activerecord (>= 4.0.0, < 5)
@@ -198,6 +199,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
acts-as-taggable-on acts-as-taggable-on
acts_as_commentable_with_threading acts_as_commentable_with_threading
acts_as_votable
byebug byebug
capybara capybara
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)

View File

@@ -0,0 +1,20 @@
class VotesController < ApplicationController
before_action :set_debate
before_action :authenticate_user!
def create
register_vote
notice = @debate.vote_registered? ? "Gracias por votar." : "Tu voto ya ha sido registrado."
redirect_to @debate, notice: notice
end
private
def set_debate
@debate = Debate.find(params[:debate_id])
end
def register_vote
@debate.vote_by voter: current_user, vote: params[:value]
end
end

View File

@@ -1,4 +1,6 @@
require 'numeric'
class Debate < ActiveRecord::Base class Debate < ActiveRecord::Base
acts_as_votable
acts_as_commentable acts_as_commentable
acts_as_taggable acts_as_taggable
@@ -9,4 +11,23 @@ class Debate < ActiveRecord::Base
validates :author, presence: true validates :author, presence: true
validates :terms_of_service, acceptance: { allow_nil: false }, on: :create validates :terms_of_service, acceptance: { allow_nil: false }, on: :create
#vote can be 'likes' or 'dislikes'
def percentage(vote)
return if total_votes == 0
send(vote).percent_of(total_votes)
end
def likes
get_likes.size
end
def dislikes
get_dislikes.size
end
def total_votes
votes_for.size
end
end end

2
app/models/vote.rb Normal file
View File

@@ -0,0 +1,2 @@
class Vote < ActsAsVotable::Vote
end

View File

@@ -7,6 +7,21 @@
</p> </p>
</div> </div>
<div>
<div id='in_favor'>
<%= link_to "up", debate_votes_path(@debate, value: 'yes'), method: "post" %>
<%= @debate.percentage('likes') %>%
</div>
<div id='against'>
<%= link_to "down", debate_votes_path(@debate, value: 'no'), method: "post" %>
<%= @debate.percentage('dislikes') %>%
</div>
Votos <%= @debate.total_votes %>
</div>
<br/><br/>
<%= render 'shared/tags', debate: @debate %> <%= render 'shared/tags', debate: @debate %>
<br/><br/> <br/><br/>

View File

@@ -7,6 +7,7 @@ Rails.application.routes.draw do
# You can have the root of your site routed with "root" # You can have the root of your site routed with "root"
root 'welcome#index' root 'welcome#index'
resources :debates do resources :debates do
resources :votes, only: :create
resources :comments, only: :create resources :comments, only: :create
end end

View File

@@ -0,0 +1,22 @@
class ActsAsVotableMigration < ActiveRecord::Migration
def self.up
create_table :votes do |t|
t.references :votable, :polymorphic => true
t.references :voter, :polymorphic => true
t.boolean :vote_flag
t.string :vote_scope
t.integer :vote_weight
t.timestamps
end
add_index :votes, [:voter_id, :voter_type, :vote_scope]
add_index :votes, [:votable_id, :votable_type, :vote_scope]
end
def self.down
drop_table :votes
end
end

View File

@@ -78,4 +78,19 @@ ActiveRecord::Schema.define(version: 20150718091702) do
add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
create_table "votes", force: :cascade do |t|
t.integer "votable_id"
t.string "votable_type"
t.integer "voter_id"
t.string "voter_type"
t.boolean "vote_flag"
t.string "vote_scope"
t.integer "vote_weight"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "votes", ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope"
add_index "votes", ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope"
end end

5
lib/numeric.rb Normal file
View File

@@ -0,0 +1,5 @@
class Numeric
def percent_of(n)
(self.to_f / n * 100).to_i
end
end

View File

@@ -14,6 +14,12 @@ FactoryGirl.define do
association :author, factory: :user association :author, factory: :user
end end
factory :vote do
association :votable, factory: :debate
association :voter, factory: :user
vote_flag true
end
factory :comment do factory :comment do
commentable commentable
user user

View File

@@ -0,0 +1,49 @@
require 'rails_helper'
feature 'Votes', :focus do
background do
@manuela = create(:user)
@pablo = create(:user)
@debate = create(:debate)
login_as(@manuela)
visit debate_path(@debate)
end
scenario 'Show' do
vote = create(:vote, voter: @manuela, votable: @debate, vote_flag: true)
vote = create(:vote, voter: @pablo, votable: @debate, vote_flag: false)
visit debate_path(@debate)
expect(page).to have_content "Votos 2"
within('#in_favor') do
expect(page).to have_content "50%"
end
within('#against') do
expect(page).to have_content "50%"
end
end
scenario 'Create' do
click_link 'up'
expect(page).to have_content "Gracias por votar"
end
scenario 'Update' do
click_link 'up'
click_link 'down'
expect(page).to have_content "Gracias por votar"
end
scenario 'Trying to vote multiple times' do
click_link 'up'
click_link 'up'
expect(page).to have_content "Tu voto ya ha sido registrado"
expect(page).to have_content "Votos 1"
end
end