diff --git a/Gemfile b/Gemfile
index bff809a9f..da69551ab 100644
--- a/Gemfile
+++ b/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
diff --git a/Gemfile.lock b/Gemfile.lock
index 470e5ccc8..601246774 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -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)
diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb
new file mode 100644
index 000000000..d3c35a220
--- /dev/null
+++ b/app/controllers/votes_controller.rb
@@ -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
diff --git a/app/models/debate.rb b/app/models/debate.rb
index d924e494e..0bebf6b8e 100644
--- a/app/models/debate.rb
+++ b/app/models/debate.rb
@@ -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
\ No newline at end of file
diff --git a/app/models/vote.rb b/app/models/vote.rb
new file mode 100644
index 000000000..47dd3f007
--- /dev/null
+++ b/app/models/vote.rb
@@ -0,0 +1,2 @@
+class Vote < ActsAsVotable::Vote
+end
\ No newline at end of file
diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb
index ce1472104..5efdd833a 100644
--- a/app/views/debates/show.html.erb
+++ b/app/views/debates/show.html.erb
@@ -7,6 +7,21 @@
+
+
+ <%= link_to "up", debate_votes_path(@debate, value: 'yes'), method: "post" %>
+ <%= @debate.percentage('likes') %>%
+
+
+
+ <%= link_to "down", debate_votes_path(@debate, value: 'no'), method: "post" %>
+ <%= @debate.percentage('dislikes') %>%
+
+
+ Votos <%= @debate.total_votes %>
+
+
+
<%= render 'shared/tags', debate: @debate %>
diff --git a/config/routes.rb b/config/routes.rb
index 1bcc6d0a8..a527a7ad5 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -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
diff --git a/db/migrate/20150717180105_acts_as_votable_migration.rb b/db/migrate/20150717180105_acts_as_votable_migration.rb
new file mode 100644
index 000000000..4bdcbe377
--- /dev/null
+++ b/db/migrate/20150717180105_acts_as_votable_migration.rb
@@ -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
diff --git a/db/schema.rb b/db/schema.rb
index 5a5c87ef2..1a32c61a7 100644
--- a/db/schema.rb
+++ b/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
diff --git a/lib/numeric.rb b/lib/numeric.rb
new file mode 100644
index 000000000..c741715dd
--- /dev/null
+++ b/lib/numeric.rb
@@ -0,0 +1,5 @@
+class Numeric
+ def percent_of(n)
+ (self.to_f / n * 100).to_i
+ end
+end
\ No newline at end of file
diff --git a/spec/factories.rb b/spec/factories.rb
index 1001990f4..9e9ab2d0a 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -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
diff --git a/spec/features/votes_spec.rb b/spec/features/votes_spec.rb
new file mode 100644
index 000000000..e5e068bcc
--- /dev/null
+++ b/spec/features/votes_spec.rb
@@ -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
\ No newline at end of file