Merge pull request #9 from AyuntamientoMadrid/votes-9
Votes for debates
This commit is contained in:
1
Gemfile
1
Gemfile
@@ -34,6 +34,7 @@ gem 'acts_as_commentable_with_threading'
|
||||
gem 'acts-as-taggable-on'
|
||||
gem "responders"
|
||||
gem 'foundation-rails'
|
||||
gem 'acts_as_votable'
|
||||
|
||||
group :development, :test do
|
||||
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
|
||||
|
||||
@@ -42,6 +42,7 @@ GEM
|
||||
activerecord (>= 4.0)
|
||||
activesupport (>= 4.0)
|
||||
awesome_nested_set (>= 3.0)
|
||||
acts_as_votable (0.10.0)
|
||||
arel (6.0.2)
|
||||
awesome_nested_set (3.0.2)
|
||||
activerecord (>= 4.0.0, < 5)
|
||||
@@ -198,6 +199,7 @@ PLATFORMS
|
||||
DEPENDENCIES
|
||||
acts-as-taggable-on
|
||||
acts_as_commentable_with_threading
|
||||
acts_as_votable
|
||||
byebug
|
||||
capybara
|
||||
coffee-rails (~> 4.1.0)
|
||||
|
||||
20
app/controllers/votes_controller.rb
Normal file
20
app/controllers/votes_controller.rb
Normal 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
|
||||
@@ -1,4 +1,6 @@
|
||||
require 'numeric'
|
||||
class Debate < ActiveRecord::Base
|
||||
acts_as_votable
|
||||
acts_as_commentable
|
||||
acts_as_taggable
|
||||
|
||||
@@ -9,4 +11,23 @@ class Debate < ActiveRecord::Base
|
||||
validates :author, presence: true
|
||||
|
||||
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
|
||||
2
app/models/vote.rb
Normal file
2
app/models/vote.rb
Normal file
@@ -0,0 +1,2 @@
|
||||
class Vote < ActsAsVotable::Vote
|
||||
end
|
||||
@@ -7,6 +7,21 @@
|
||||
</p>
|
||||
</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 %>
|
||||
|
||||
<br/><br/>
|
||||
|
||||
@@ -7,6 +7,7 @@ Rails.application.routes.draw do
|
||||
# You can have the root of your site routed with "root"
|
||||
root 'welcome#index'
|
||||
resources :debates do
|
||||
resources :votes, only: :create
|
||||
resources :comments, only: :create
|
||||
end
|
||||
|
||||
|
||||
22
db/migrate/20150717180105_acts_as_votable_migration.rb
Normal file
22
db/migrate/20150717180105_acts_as_votable_migration.rb
Normal 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
|
||||
15
db/schema.rb
15
db/schema.rb
@@ -78,4 +78,19 @@ ActiveRecord::Schema.define(version: 20150718091702) do
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
5
lib/numeric.rb
Normal file
5
lib/numeric.rb
Normal file
@@ -0,0 +1,5 @@
|
||||
class Numeric
|
||||
def percent_of(n)
|
||||
(self.to_f / n * 100).to_i
|
||||
end
|
||||
end
|
||||
@@ -14,6 +14,12 @@ FactoryGirl.define do
|
||||
association :author, factory: :user
|
||||
end
|
||||
|
||||
factory :vote do
|
||||
association :votable, factory: :debate
|
||||
association :voter, factory: :user
|
||||
vote_flag true
|
||||
end
|
||||
|
||||
factory :comment do
|
||||
commentable
|
||||
user
|
||||
|
||||
49
spec/features/votes_spec.rb
Normal file
49
spec/features/votes_spec.rb
Normal 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
|
||||
Reference in New Issue
Block a user