From e006549092ac9d394d7970db979d73b09a49a0cd Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 18:10:19 +0200 Subject: [PATCH] adds votes to debates [#9] --- app/controllers/votes_controller.rb | 20 ++++++++++++++++++++ app/models/debate.rb | 23 ++++++++++++++++++++++- app/views/debates/show.html.erb | 21 +++++++++++++++++++-- config/routes.rb | 4 +++- lib/numeric.rb | 5 +++++ 5 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 app/controllers/votes_controller.rb create mode 100644 lib/numeric.rb 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 c66913c59..5e810d061 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,27 @@ +require 'numeric' class Debate < ActiveRecord::Base + acts_as_votable + validates :title, presence: true validates :description, presence: true validates :terms_of_service, acceptance: { allow_nil: false }, on: :create -end + + #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/views/debates/show.html.erb b/app/views/debates/show.html.erb index 777b22d1b..132152b3b 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -4,5 +4,22 @@

Creado el: <%= l @debate.created_at.to_date %>

-<%= link_to 'Edit', edit_debate_path(@debate) %> | -<%= link_to 'Back', debates_path %> \ No newline at end of file +
+
+ <%= 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 %> +
+ +

+
+ <%= link_to 'Edit', edit_debate_path(@debate) %> | + <%= link_to 'Back', debates_path %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index eae8764ed..3124c7852 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,9 @@ Rails.application.routes.draw do # You can have the root of your site routed with "root" root 'welcome#index' - resources :debates + resources :debates do + resources :votes, only: :create + end # Example of regular route: # get 'products/:id' => 'catalog#view' 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