From 30982d101f012cbb80aef3acb1d7658a59d31361 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:47:19 +0200 Subject: [PATCH 01/15] adds user menu --- app/views/devise/menu/_login_items.html.erb | 9 +++++++++ app/views/layouts/application.html.erb | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 app/views/devise/menu/_login_items.html.erb diff --git a/app/views/devise/menu/_login_items.html.erb b/app/views/devise/menu/_login_items.html.erb new file mode 100644 index 000000000..89b9ad253 --- /dev/null +++ b/app/views/devise/menu/_login_items.html.erb @@ -0,0 +1,9 @@ +<% if user_signed_in? %> +
  • + <%= link_to('Logout', destroy_user_session_path, :method => :delete) %> +
  • +<% else %> +
  • + <%= link_to('Login', new_user_session_path) %> +
  • +<% end %> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 10302f65f..bb1a5c28f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,9 @@

    <%= notice %>

    <%= alert %>

    + + <%= render 'devise/menu/login_items' %> + <%= yield %> \ No newline at end of file From 83654042d9ac470eb895cf31d3d7c81c6b9dccb7 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:46:45 +0200 Subject: [PATCH 02/15] adds devise spanish translations --- config/locales/devise.es.yml | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 config/locales/devise.es.yml diff --git a/config/locales/devise.es.yml b/config/locales/devise.es.yml new file mode 100644 index 000000000..f39099f57 --- /dev/null +++ b/config/locales/devise.es.yml @@ -0,0 +1,58 @@ +es: + devise: + confirmations: + confirmed: "Tu cuenta ha sido confirmada." + send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña." + send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña." + failure: + already_authenticated: "Ya has iniciado sesión." + inactive: "Tu cuenta aún no ha sido activada." + invalid: "%{authentication_keys} o contraseña inválidos." + locked: "Tu cuenta ha sido bloqueada." + last_attempt: "Tienes un último intento antes de que tu cuenta sea bloqueada." + not_found_in_database: "%{authentication_keys} o contraseña inválidos." + timeout: "Tu sesión ha expirado, por favor inicia sesión nuevamente para continuar." + unauthenticated: "Necesitas iniciar sesión o registrarte para continuar." + unconfirmed: "Debes confirmar tu cuenta para continuar." + mailer: + confirmation_instructions: + subject: "Instrucciones de confirmación" + reset_password_instructions: + subject: "Instrucciones para restablecer tu contraseña" + unlock_instructions: + subject: "Instrucciones de desbloqueo" + omniauth_callbacks: + failure: "No se te ha podido autorizar de %{kind} debido a \"%{reason}\"." + success: "Identificado correctamente de %{kind}." + passwords: + no_token: "No puedes acceder a esta página si no es a través de un enlace para restablecer la contraseña. Si has accedido desde el enlace para restablecer la contraseña, asegúrate de que la URL esté completa." + send_instructions: "Recibirás un correo electrónico con instrucciones sobre cómo restablecer tu contraseña en unos minutos." + send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos, recibirás un enlace para restablecer la contraseña en unos minutos." + updated: "Tu contraseña ha cambiado correctamente. Has sido identificado correctamente." + updated_not_active: "Tu contraseña se ha cambiado correctamente." + registrations: + destroyed: "¡Adiós! Tu cuenta ha sido cancelada. Esperamos volver a verte pronto." + signed_up: "¡Bienvenido! Has sido identificado." + signed_up_but_inactive: "Te has registrado correctamente, pero no has podido iniciar sesión porque tu cuenta no ha sido activada." + signed_up_but_locked: "Te has registrado correctamente, pero no has podido iniciar sesión porque tu cuenta está bloqueada." + signed_up_but_unconfirmed: "Se te ha enviado un mensaje con un enlace de confirmación. Por favor visita el enlace para activar tu cuenta." + update_needs_confirmation: "Has actualizado tu cuenta correctamente, sin embargo necesitamos verificar tu nueva cuenta de correo. Por favor revisa tu correo electrónico y visita el enlace para finalizar la confirmación de tu nueva dirección de correo electrónico." + updated: "Has actualizado tu cuenta correctamente." + sessions: + signed_in: "Has iniciado sesión correctamente." + signed_out: "Has cerrado la sesión correctamente." + already_signed_out: "Has cerrado la sesión correctamente." + unlocks: + send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo desbloquear tu cuenta." + send_paranoid_instructions: "Si tu cuenta existe, recibirás un correo electrónico en unos minutos con instrucciones sobre cómo desbloquear tu cuenta." + unlocked: "Tu cuenta ha sido desbloqueada. Por favor inicia sesión para continuar." + errors: + messages: + already_confirmed: "ya has sido confirmado, por favor intenta iniciar sesión." + confirmation_period_expired: "necesitas ser confirmado en %{period}, por favor vuelve a solicitarla." + expired: "ha expirado, por favor vuelve a solicitarla." + not_found: "no se ha encontrado." + not_locked: "no estaba bloqueado." + not_saved: + one: "1 error impidió que este %{resource} fuera guardado:" + other: "%{count} errores impidieron que este %{resource} fuera guardado:" \ No newline at end of file From 62d0a385aba7e4d4f7163d41019390d125dbface Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:46:10 +0200 Subject: [PATCH 03/15] adds first name and last name to users --- app/controllers/registrations_controller.rb | 9 +++++++++ app/views/devise/registrations/new.html.erb | 13 ++++++++++++- config/routes.rb | 3 ++- ...7164054_add_first_name_and_last_name_to_users.rb | 6 ++++++ db/schema.rb | 4 +++- 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 000000000..4ccafe235 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,9 @@ +class RegistrationsController < Devise::RegistrationsController + + private + + def sign_up_params + params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) + end + +end \ No newline at end of file diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 5a238ce6e..8c4da21cd 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,9 +3,20 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> + +
    + <%= f.label :first_name %>
    + <%= f.text_field :first_name, autofocus: true %> +
    + +
    + <%= f.label :last_name %>
    + <%= f.text_field :last_name %> +
    +
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true %> + <%= f.email_field :email %>
    diff --git a/config/routes.rb b/config/routes.rb index eae8764ed..a1dbec65f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do - devise_for :users + devise_for :users, :controllers => { registrations: 'registrations' } + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb b/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb new file mode 100644 index 000000000..3be763bfd --- /dev/null +++ b/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb @@ -0,0 +1,6 @@ +class AddFirstNameAndLastNameToUsers < ActiveRecord::Migration + def change + add_column :users, :first_name, :string + add_column :users, :last_name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 61fb86a58..466223bd8 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: 20150717164054) do create_table "debates", force: :cascade do |t| t.string "title" @@ -34,6 +34,8 @@ ActiveRecord::Schema.define(version: 20150716174358) do t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "first_name" + t.string "last_name" end add_index "users", ["email"], name: "index_users_on_email", unique: true From 8f78509b0cf20703627f1cd7c787914dc34cafb9 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:48:42 +0200 Subject: [PATCH 04/15] adds basic devise feature specs --- spec/factories.rb | 9 ++++++++ spec/features/users_spec.rb | 43 +++++++++++++++++++++++++++++++++++++ spec/rails_helper.rb | 4 ++++ 3 files changed, 56 insertions(+) create mode 100644 spec/features/users_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index 58f320ea7..971791b0b 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,7 +1,16 @@ FactoryGirl.define do + + factory :user do + first_name 'Manuela' + last_name 'Carmena' + sequence(:email) { |n| "manuela#{n}@madrid.es" } + password 'judgmentday' + end + factory :debate do title 'Debate title' description 'Debate description' terms_of_service '1' end + end \ No newline at end of file diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb new file mode 100644 index 000000000..30755c56f --- /dev/null +++ b/spec/features/users_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +feature 'Users' do + + scenario 'Sign up' do + visit '/' + click_link 'Login' + click_link 'Sign up' + + fill_in 'user_first_name', with: 'Manuela' + fill_in 'user_last_name', with: 'Carmena' + fill_in 'user_email', with: 'manuela@madrid.es' + fill_in 'user_password', with: 'judgementday' + fill_in 'user_password_confirmation', with: 'judgementday' + + click_button 'Sign up' + + expect(page).to have_content '¡Bienvenido! Has sido identificado.' + end + + scenario 'Sign in' do + user = create(:user, email: 'manuela@madrid.es', password: 'judgementday') + + visit '/' + click_link 'Login' + fill_in 'user_email', with: 'manuela@madrid.es' + fill_in 'user_password', with: 'judgementday' + click_button 'Log in' + + expect(page).to have_content 'Has iniciado sesión correctamente.' + end + + scenario 'Sign out' do + user = create(:user) + login_as(user) + + visit "/" + click_link 'Logout' + + expect(page).to have_content 'Has cerrado la sesión correctamente.' + end + +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 5c5fedee7..fc6f2bb68 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,6 +6,10 @@ require 'spec_helper' require 'rspec/rails' require 'capybara/rails' require 'capybara/rspec' + +include Warden::Test::Helpers +Warden.test_mode! + ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| From 3108228cc77d3717b019065646ec7ac9431f290b Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:47:19 +0200 Subject: [PATCH 05/15] adds user menu --- app/views/devise/menu/_login_items.html.erb | 9 +++++++++ app/views/layouts/application.html.erb | 3 +++ 2 files changed, 12 insertions(+) create mode 100644 app/views/devise/menu/_login_items.html.erb diff --git a/app/views/devise/menu/_login_items.html.erb b/app/views/devise/menu/_login_items.html.erb new file mode 100644 index 000000000..89b9ad253 --- /dev/null +++ b/app/views/devise/menu/_login_items.html.erb @@ -0,0 +1,9 @@ +<% if user_signed_in? %> +
  • + <%= link_to('Logout', destroy_user_session_path, :method => :delete) %> +
  • +<% else %> +
  • + <%= link_to('Login', new_user_session_path) %> +
  • +<% end %> \ No newline at end of file diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 10302f65f..bb1a5c28f 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,6 +14,9 @@

    <%= notice %>

    <%= alert %>

    + + <%= render 'devise/menu/login_items' %> + <%= yield %> \ No newline at end of file From 9bdf9dce1175aefca198a30c28d842733da64c06 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:46:45 +0200 Subject: [PATCH 06/15] adds devise spanish translations --- config/locales/devise.es.yml | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 config/locales/devise.es.yml diff --git a/config/locales/devise.es.yml b/config/locales/devise.es.yml new file mode 100644 index 000000000..f39099f57 --- /dev/null +++ b/config/locales/devise.es.yml @@ -0,0 +1,58 @@ +es: + devise: + confirmations: + confirmed: "Tu cuenta ha sido confirmada." + send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña." + send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos recibirás un correo electrónico en unos minutos con instrucciones sobre cómo restablecer tu contraseña." + failure: + already_authenticated: "Ya has iniciado sesión." + inactive: "Tu cuenta aún no ha sido activada." + invalid: "%{authentication_keys} o contraseña inválidos." + locked: "Tu cuenta ha sido bloqueada." + last_attempt: "Tienes un último intento antes de que tu cuenta sea bloqueada." + not_found_in_database: "%{authentication_keys} o contraseña inválidos." + timeout: "Tu sesión ha expirado, por favor inicia sesión nuevamente para continuar." + unauthenticated: "Necesitas iniciar sesión o registrarte para continuar." + unconfirmed: "Debes confirmar tu cuenta para continuar." + mailer: + confirmation_instructions: + subject: "Instrucciones de confirmación" + reset_password_instructions: + subject: "Instrucciones para restablecer tu contraseña" + unlock_instructions: + subject: "Instrucciones de desbloqueo" + omniauth_callbacks: + failure: "No se te ha podido autorizar de %{kind} debido a \"%{reason}\"." + success: "Identificado correctamente de %{kind}." + passwords: + no_token: "No puedes acceder a esta página si no es a través de un enlace para restablecer la contraseña. Si has accedido desde el enlace para restablecer la contraseña, asegúrate de que la URL esté completa." + send_instructions: "Recibirás un correo electrónico con instrucciones sobre cómo restablecer tu contraseña en unos minutos." + send_paranoid_instructions: "Si tu correo electrónico existe en nuestra base de datos, recibirás un enlace para restablecer la contraseña en unos minutos." + updated: "Tu contraseña ha cambiado correctamente. Has sido identificado correctamente." + updated_not_active: "Tu contraseña se ha cambiado correctamente." + registrations: + destroyed: "¡Adiós! Tu cuenta ha sido cancelada. Esperamos volver a verte pronto." + signed_up: "¡Bienvenido! Has sido identificado." + signed_up_but_inactive: "Te has registrado correctamente, pero no has podido iniciar sesión porque tu cuenta no ha sido activada." + signed_up_but_locked: "Te has registrado correctamente, pero no has podido iniciar sesión porque tu cuenta está bloqueada." + signed_up_but_unconfirmed: "Se te ha enviado un mensaje con un enlace de confirmación. Por favor visita el enlace para activar tu cuenta." + update_needs_confirmation: "Has actualizado tu cuenta correctamente, sin embargo necesitamos verificar tu nueva cuenta de correo. Por favor revisa tu correo electrónico y visita el enlace para finalizar la confirmación de tu nueva dirección de correo electrónico." + updated: "Has actualizado tu cuenta correctamente." + sessions: + signed_in: "Has iniciado sesión correctamente." + signed_out: "Has cerrado la sesión correctamente." + already_signed_out: "Has cerrado la sesión correctamente." + unlocks: + send_instructions: "Recibirás un correo electrónico en unos minutos con instrucciones sobre cómo desbloquear tu cuenta." + send_paranoid_instructions: "Si tu cuenta existe, recibirás un correo electrónico en unos minutos con instrucciones sobre cómo desbloquear tu cuenta." + unlocked: "Tu cuenta ha sido desbloqueada. Por favor inicia sesión para continuar." + errors: + messages: + already_confirmed: "ya has sido confirmado, por favor intenta iniciar sesión." + confirmation_period_expired: "necesitas ser confirmado en %{period}, por favor vuelve a solicitarla." + expired: "ha expirado, por favor vuelve a solicitarla." + not_found: "no se ha encontrado." + not_locked: "no estaba bloqueado." + not_saved: + one: "1 error impidió que este %{resource} fuera guardado:" + other: "%{count} errores impidieron que este %{resource} fuera guardado:" \ No newline at end of file From 9fa952c118b23c3efe3c04d7ff2e9ceb0abbf658 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:46:10 +0200 Subject: [PATCH 07/15] adds first name and last name to users --- app/controllers/registrations_controller.rb | 9 +++++++++ app/views/devise/registrations/new.html.erb | 13 ++++++++++++- config/routes.rb | 3 ++- ...7164054_add_first_name_and_last_name_to_users.rb | 6 ++++++ db/schema.rb | 4 +++- 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb new file mode 100644 index 000000000..4ccafe235 --- /dev/null +++ b/app/controllers/registrations_controller.rb @@ -0,0 +1,9 @@ +class RegistrationsController < Devise::RegistrationsController + + private + + def sign_up_params + params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation) + end + +end \ No newline at end of file diff --git a/app/views/devise/registrations/new.html.erb b/app/views/devise/registrations/new.html.erb index 5a238ce6e..8c4da21cd 100644 --- a/app/views/devise/registrations/new.html.erb +++ b/app/views/devise/registrations/new.html.erb @@ -3,9 +3,20 @@ <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %> <%= devise_error_messages! %> + +
    + <%= f.label :first_name %>
    + <%= f.text_field :first_name, autofocus: true %> +
    + +
    + <%= f.label :last_name %>
    + <%= f.text_field :last_name %> +
    +
    <%= f.label :email %>
    - <%= f.email_field :email, autofocus: true %> + <%= f.email_field :email %>
    diff --git a/config/routes.rb b/config/routes.rb index eae8764ed..a1dbec65f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do - devise_for :users + devise_for :users, :controllers => { registrations: 'registrations' } + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb b/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb new file mode 100644 index 000000000..3be763bfd --- /dev/null +++ b/db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb @@ -0,0 +1,6 @@ +class AddFirstNameAndLastNameToUsers < ActiveRecord::Migration + def change + add_column :users, :first_name, :string + add_column :users, :last_name, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 61fb86a58..466223bd8 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: 20150717164054) do create_table "debates", force: :cascade do |t| t.string "title" @@ -34,6 +34,8 @@ ActiveRecord::Schema.define(version: 20150716174358) do t.string "last_sign_in_ip" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "first_name" + t.string "last_name" end add_index "users", ["email"], name: "index_users_on_email", unique: true From acc8be3b46503ed80ad9073e75d688b80ad35489 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:48:42 +0200 Subject: [PATCH 08/15] adds basic devise feature specs --- spec/factories.rb | 9 ++++++++ spec/features/users_spec.rb | 43 +++++++++++++++++++++++++++++++++++++ spec/rails_helper.rb | 4 ++++ 3 files changed, 56 insertions(+) create mode 100644 spec/features/users_spec.rb diff --git a/spec/factories.rb b/spec/factories.rb index 58f320ea7..971791b0b 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -1,7 +1,16 @@ FactoryGirl.define do + + factory :user do + first_name 'Manuela' + last_name 'Carmena' + sequence(:email) { |n| "manuela#{n}@madrid.es" } + password 'judgmentday' + end + factory :debate do title 'Debate title' description 'Debate description' terms_of_service '1' end + end \ No newline at end of file diff --git a/spec/features/users_spec.rb b/spec/features/users_spec.rb new file mode 100644 index 000000000..30755c56f --- /dev/null +++ b/spec/features/users_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +feature 'Users' do + + scenario 'Sign up' do + visit '/' + click_link 'Login' + click_link 'Sign up' + + fill_in 'user_first_name', with: 'Manuela' + fill_in 'user_last_name', with: 'Carmena' + fill_in 'user_email', with: 'manuela@madrid.es' + fill_in 'user_password', with: 'judgementday' + fill_in 'user_password_confirmation', with: 'judgementday' + + click_button 'Sign up' + + expect(page).to have_content '¡Bienvenido! Has sido identificado.' + end + + scenario 'Sign in' do + user = create(:user, email: 'manuela@madrid.es', password: 'judgementday') + + visit '/' + click_link 'Login' + fill_in 'user_email', with: 'manuela@madrid.es' + fill_in 'user_password', with: 'judgementday' + click_button 'Log in' + + expect(page).to have_content 'Has iniciado sesión correctamente.' + end + + scenario 'Sign out' do + user = create(:user) + login_as(user) + + visit "/" + click_link 'Logout' + + expect(page).to have_content 'Has cerrado la sesión correctamente.' + end + +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 5c5fedee7..fc6f2bb68 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -6,6 +6,10 @@ require 'spec_helper' require 'rspec/rails' require 'capybara/rails' require 'capybara/rspec' + +include Warden::Test::Helpers +Warden.test_mode! + ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| From e92174e107722a97339963594f3085c0895cace0 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:22 +0200 Subject: [PATCH 09/15] adds author to debates [#11] --- app/controllers/debates_controller.rb | 7 +++++-- app/models/debate.rb | 5 ++++- app/views/debates/_debate.html.erb | 5 ++++- app/views/debates/show.html.erb | 5 ++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 53545887e..718772015 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -1,6 +1,7 @@ class DebatesController < ApplicationController before_action :set_debate, only: [:show, :edit, :update] - + before_action :authenticate_user!, only: [:new, :create] + def index @debates = Debate.all end @@ -16,7 +17,9 @@ class DebatesController < ApplicationController end def create - @debate = Debate.create(debate_params) + @debate = Debate.new(debate_params) + @debate.author = current_user + @debate.save respond_with @debate end diff --git a/app/models/debate.rb b/app/models/debate.rb index c66913c59..22e15fe3e 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,6 +1,9 @@ class Debate < ActiveRecord::Base + belongs_to :author, class_name: 'User', foreign_key: 'author_id' + validates :title, presence: true validates :description, presence: true + validates :author, presence: true validates :terms_of_service, acceptance: { allow_nil: false }, on: :create -end +end \ No newline at end of file diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 4f99c9df9..637c0086c 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -1,6 +1,9 @@

    <%= link_to debate.title, debate %>

    <%= debate.description %>

    -

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

    +

    + Creado el: <%= l debate.created_at.to_date %> + por: <%= debate.author.name %> +



    \ No newline at end of file diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index 777b22d1b..f0a67e527 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -1,7 +1,10 @@

    <%= @debate.title %>

    <%= @debate.description %>

    -

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

    +

    + Creado el: <%= l @debate.created_at.to_date %> + por: <%= @debate.author.name %> +

    <%= link_to 'Edit', edit_debate_path(@debate) %> | From bf11dba0929d3579bf1875c59fc7ddd28ef4f37c Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:31 +0200 Subject: [PATCH 10/15] adds convenience methods and association [#11] --- app/models/user.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index c8220270d..923604dde 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,10 @@ class User < ActiveRecord::Base - # Include default devise modules. Others available are: - # :confirmable, :lockable, :timeoutable and :omniauthable + has_many :debates + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + + def name + "#{first_name} #{last_name}" + end end From 9b4182250bfa359db8f473f31c6ade2ca0e50003 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:57 +0200 Subject: [PATCH 11/15] displays terms of conditions in new and create [#11] --- app/views/debates/_form.html.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 98de3c538..4ece98d8f 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -20,7 +20,7 @@

    Explica con todo el detalle que puedas y de una manera sencilla la idea y que crees que conseguiríamos con ella

    <%= f.text_area :description %> - <% if action_name == 'new' %> + <% if @debate.new_record? %> <%= f.check_box :terms_of_service %> Acepto la política de privacidad y el aviso legal <% end %> From 9a8cc4778499f5fdb660a2ad2040857f69391af6 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:50:14 +0200 Subject: [PATCH 12/15] updates specs for debate authors [#11] --- spec/features/debates_spec.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index 48f7d3b62..ecc4892af 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -12,6 +12,7 @@ feature 'Debates' do expect(page).to have_content "Debate title" expect(page).to have_content "Debate description" expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{Debate.first.author.name}" end end @@ -23,9 +24,13 @@ feature 'Debates' do expect(page).to have_content "Debate title" expect(page).to have_content "Debate description" expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{debate.author.name}" end scenario 'Create' do + author = create(:user) + login_as(author) + visit new_debate_path fill_in 'debate_title', with: 'Acabar con los desahucios' fill_in 'debate_description', with: 'Esto es un tema muy importante porque...' @@ -36,6 +41,8 @@ feature 'Debates' do expect(page).to have_content 'Debate creado correctamente' expect(page).to have_content 'Acabar con los desahucios' expect(page).to have_content 'Esto es un tema muy importante porque...' + expect(page).to have_content "Creado el: #{I18n.l Date.today}" + expect(page).to have_content "por: #{author.name}" end scenario 'Update' do From bcd96ab1b4321336b9254d16d7b03092e27ada87 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 15:09:06 +0200 Subject: [PATCH 13/15] fixing specs [#11] --- spec/factories.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/factories.rb b/spec/factories.rb index 971791b0b..0433e2b15 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -11,6 +11,7 @@ FactoryGirl.define do title 'Debate title' description 'Debate description' terms_of_service '1' + association :author, factory: :user end end \ No newline at end of file From 967b5cdcb5cf6b399781f897fe23b5e3c7be2771 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 15:09:28 +0200 Subject: [PATCH 14/15] adds author validation spec [#11] --- spec/models/debate_spec.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spec/models/debate_spec.rb b/spec/models/debate_spec.rb index 490eea985..f277f1d7b 100644 --- a/spec/models/debate_spec.rb +++ b/spec/models/debate_spec.rb @@ -10,6 +10,11 @@ describe Debate do expect(@debate).to be_valid end + it "should not be valid without an author" do + @debate.author = nil + expect(@debate).to_not be_valid + end + it "should not be valid without a title" do @debate.title = nil expect(@debate).to_not be_valid From 31e17396f567ed08bde8a2d32946dd0dd82ef518 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:11:46 +0200 Subject: [PATCH 15/15] removes unused association [#11] --- app/models/user.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 923604dde..9c55bc59e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,6 +1,4 @@ class User < ActiveRecord::Base - has_many :debates - devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable