From b8e41551f98222d5937241bbdfb85eb7e30b494d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 18:09:48 +0200 Subject: [PATCH 1/4] configures acts_as_votable [#9] --- Gemfile | 1 + Gemfile.lock | 2 ++ ...0150717180105_acts_as_votable_migration.rb | 22 +++++++++++++++++++ db/schema.rb | 17 +++++++++++++- 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20150717180105_acts_as_votable_migration.rb diff --git a/Gemfile b/Gemfile index 45f16f7b4..3f6d2b90e 100644 --- a/Gemfile +++ b/Gemfile @@ -34,6 +34,7 @@ gem 'devise' 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 01408b9c2..d0dfefaaa 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,7 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + acts_as_votable (0.10.0) arel (6.0.2) bcrypt (3.1.10) binding_of_caller (0.7.2) @@ -188,6 +189,7 @@ PLATFORMS ruby DEPENDENCIES + acts_as_votable byebug capybara coffee-rails (~> 4.1.0) 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 61fb86a58..3ead9ea4e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150716174358) do +ActiveRecord::Schema.define(version: 20150717180105) do create_table "debates", force: :cascade do |t| t.string "title" @@ -39,4 +39,19 @@ ActiveRecord::Schema.define(version: 20150716174358) 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 From e006549092ac9d394d7970db979d73b09a49a0cd Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 18:10:19 +0200 Subject: [PATCH 2/4] 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 From 090890f1ef432a92b0c2b76ed7fbca2c5251b7c6 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 18:10:36 +0200 Subject: [PATCH 3/4] adds votes feature specs [#9] --- app/models/vote.rb | 2 ++ spec/factories.rb | 7 ++++++ spec/features/votes_spec.rb | 49 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 app/models/vote.rb create mode 100644 spec/features/votes_spec.rb 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/spec/factories.rb b/spec/factories.rb index 58f320ea7..eb65ecdca 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -4,4 +4,11 @@ FactoryGirl.define do description 'Debate description' terms_of_service '1' end + + factory :vote do + association :votable, factory: :debate + association :voter, factory: :user + vote_flag true + end + end \ No newline at end of file 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 From 30102f88c819aa65ecf6caf2d6d92ff22b8d1a3a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 18:19:45 +0200 Subject: [PATCH 4/4] fixes syntax error [#9] --- spec/factories.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/factories.rb b/spec/factories.rb index 9a7ce6181..9e9ab2d0a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -18,7 +18,8 @@ FactoryGirl.define do association :votable, factory: :debate association :voter, factory: :user vote_flag true - + end + factory :comment do commentable user