From c55021e18b7b60bef4ff660eed039a5067736733 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 15:02:46 +0200 Subject: [PATCH 01/15] fixing conflicts [#8] --- app/controllers/registrations_controller.rb | 9 +++ app/views/devise/menu/_login_items.html.erb | 9 +++ app/views/devise/registrations/new.html.erb | 13 ++++- app/views/layouts/application.html.erb | 3 + config/locales/devise.es.yml | 58 +++++++++++++++++++ config/routes.rb | 3 +- ...4_add_first_name_and_last_name_to_users.rb | 6 ++ db/schema.rb | 17 ++++++ spec/factories.rb | 9 +++ spec/features/users_spec.rb | 43 ++++++++++++++ spec/rails_helper.rb | 4 ++ 11 files changed, 172 insertions(+), 2 deletions(-) create mode 100644 app/controllers/registrations_controller.rb create mode 100644 app/views/devise/menu/_login_items.html.erb create mode 100644 config/locales/devise.es.yml create mode 100644 db/migrate/20150717164054_add_first_name_and_last_name_to_users.rb create mode 100644 spec/features/users_spec.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/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/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/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 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 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..ef20ee3cf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -34,9 +34,26 @@ 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 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/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 272c03f6a54c7a2f81a6e04cf009b9a08d5b05bf Mon Sep 17 00:00:00 2001 From: rgarcia Date: Fri, 17 Jul 2015 19:46:10 +0200 Subject: [PATCH 02/15] adds first name and last name to users --- db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index ef20ee3cf..09fd0d0a7 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" From 45094a86bc430edeb8effd84c3f5d6143c43d40d Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:22 +0200 Subject: [PATCH 03/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 20e31def2e0e492501aa4ccdd361df57eafaaff2 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:31 +0200 Subject: [PATCH 04/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 83ac9dc1fc937ac825b6d11501141da57f75a3df Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:49:57 +0200 Subject: [PATCH 05/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 6430693b85a6798d8166c6ff229eab1b29741cd9 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 14:50:14 +0200 Subject: [PATCH 06/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 4ed8881116e36bd7b6be5e2087b9d8b39de8adaa Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 15:09:06 +0200 Subject: [PATCH 07/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 0cb0a691c85203f47f865fada1bf45022e1f143a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 15:09:28 +0200 Subject: [PATCH 08/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 1f725a2c51eb31f2e19c060aca156ef91a1e726c Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:11:46 +0200 Subject: [PATCH 09/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 From 6adedf45b8a1503a55bee2e1af1f5f2b258cdc86 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:23:00 +0200 Subject: [PATCH 10/15] configures act_as_taggable [#8] --- Gemfile | 2 +- Gemfile.lock | 3 +++ app/models/debate.rb | 1 + config/initializers/acts_as_taggable_on.rb | 1 + ...on_migration.acts_as_taggable_on_engine.rb | 25 +++++++++++++++++++ ...ache_to_tags.acts_as_taggable_on_engine.rb | 10 ++++++++ db/schema.rb | 22 +++++++++++++++- 7 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 config/initializers/acts_as_taggable_on.rb create mode 100644 db/migrate/20150718075238_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb create mode 100644 db/migrate/20150718075337_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb diff --git a/Gemfile b/Gemfile index 45f16f7b4..6dd5d0103 100644 --- a/Gemfile +++ b/Gemfile @@ -31,7 +31,7 @@ gem 'devise' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development - +gem 'acts-as-taggable-on' gem "responders" gem 'foundation-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 01408b9c2..9f2595ce7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,6 +36,8 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + acts-as-taggable-on (3.5.0) + activerecord (>= 3.2, < 5) arel (6.0.2) bcrypt (3.1.10) binding_of_caller (0.7.2) @@ -188,6 +190,7 @@ PLATFORMS ruby DEPENDENCIES + acts-as-taggable-on byebug capybara coffee-rails (~> 4.1.0) diff --git a/app/models/debate.rb b/app/models/debate.rb index 22e15fe3e..4d0bf3ff0 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,4 +1,5 @@ class Debate < ActiveRecord::Base + acts_as_taggable belongs_to :author, class_name: 'User', foreign_key: 'author_id' validates :title, presence: true diff --git a/config/initializers/acts_as_taggable_on.rb b/config/initializers/acts_as_taggable_on.rb new file mode 100644 index 000000000..ead1691e4 --- /dev/null +++ b/config/initializers/acts_as_taggable_on.rb @@ -0,0 +1 @@ +ActsAsTaggableOn.delimiter = ',' \ No newline at end of file diff --git a/db/migrate/20150718075238_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb b/db/migrate/20150718075238_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb new file mode 100644 index 000000000..2cf5741f8 --- /dev/null +++ b/db/migrate/20150718075238_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb @@ -0,0 +1,25 @@ +# This migration comes from acts_as_taggable_on_engine (originally 1) +class ActsAsTaggableOnMigration < ActiveRecord::Migration + def self.up + create_table :tags do |t| + t.string :name + end + + create_table :taggings do |t| + t.references :tag + t.references :taggable, polymorphic: true + t.references :tagger, polymorphic: true + t.string :context, limit: 128 + t.datetime :created_at + end + + add_index :tags, :name, unique: true + add_index :taggings, :tag_id + add_index :taggings, [:taggable_id, :taggable_type, :context] + end + + def self.down + drop_table :taggings + drop_table :tags + end +end diff --git a/db/migrate/20150718075337_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb b/db/migrate/20150718075337_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb new file mode 100644 index 000000000..6bbd5ab3d --- /dev/null +++ b/db/migrate/20150718075337_add_taggings_counter_cache_to_tags.acts_as_taggable_on_engine.rb @@ -0,0 +1,10 @@ +# This migration comes from acts_as_taggable_on_engine (originally 3) +class AddTaggingsCounterCacheToTags < ActiveRecord::Migration + def self.up + add_column :tags, :taggings_count, :integer, default: 0 + end + + def self.down + remove_column :tags, :taggings_count + end +end diff --git a/db/schema.rb b/db/schema.rb index 09fd0d0a7..a9958aba6 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: 20150717164054) do +ActiveRecord::Schema.define(version: 20150718075337) do create_table "debates", force: :cascade do |t| t.string "title" @@ -21,6 +21,26 @@ ActiveRecord::Schema.define(version: 20150717164054) do t.datetime "updated_at", null: false end + create_table "taggings", force: :cascade do |t| + t.integer "tag_id" + t.integer "taggable_id" + t.string "taggable_type" + t.integer "tagger_id" + t.string "tagger_type" + t.string "context", limit: 128 + t.datetime "created_at" + end + + add_index "taggings", ["tag_id"], name: "index_taggings_on_tag_id" + add_index "taggings", ["taggable_id", "taggable_type", "context"], name: "index_taggings_on_taggable_id_and_taggable_type_and_context" + + create_table "tags", force: :cascade do |t| + t.string "name" + t.integer "taggings_count", default: 0 + end + + add_index "tags", ["name"], name: "index_tags_on_name", unique: true + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false From 72bc7903102b62a7ba12457704e1bd1d5485fabc Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:24:16 +0200 Subject: [PATCH 11/15] adds tags to debates [#8] --- app/controllers/debates_controller.rb | 8 ++++++-- app/helpers/application_helper.rb | 5 +++++ app/views/debates/_debate.html.erb | 7 +++++-- app/views/debates/_form.html.erb | 5 +++++ app/views/debates/show.html.erb | 3 +++ app/views/shared/_tags.html.erb | 3 +++ 6 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 app/views/shared/_tags.html.erb diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 718772015..2bf5a0024 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -3,7 +3,11 @@ class DebatesController < ApplicationController before_action :authenticate_user!, only: [:new, :create] def index - @debates = Debate.all + if params[:tag] + @debates = Debate.tagged_with(params[:tag]) + else + @debates = Debate.all + end end def show @@ -35,7 +39,7 @@ class DebatesController < ApplicationController end def debate_params - params.require(:debate).permit(:title, :description, :external_link, :terms_of_service) + params.require(:debate).permit(:title, :description, :tag_list, :terms_of_service) end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index de6be7945..74dcfd229 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,2 +1,7 @@ module ApplicationHelper + + def tags(debate) + debate.tag_list.map { |tag| link_to tag, debates_path(tag: tag) }.join(', ').html_safe + end + end diff --git a/app/views/debates/_debate.html.erb b/app/views/debates/_debate.html.erb index 637c0086c..d78776af9 100644 --- a/app/views/debates/_debate.html.erb +++ b/app/views/debates/_debate.html.erb @@ -1,9 +1,12 @@ -
    +

    <%= link_to debate.title, debate %>

    <%= debate.description %>

    +

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

    + +

    <%= render 'shared/tags', debate: debate %>

    -

    \ No newline at end of file +



    \ No newline at end of file diff --git a/app/views/debates/_form.html.erb b/app/views/debates/_form.html.erb index 4ece98d8f..9d0af3892 100644 --- a/app/views/debates/_form.html.erb +++ b/app/views/debates/_form.html.erb @@ -20,6 +20,11 @@

    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 %> +
    + <%= f.label :tag_list, "Temas (separados por comas)" %>
    + <%= f.text_field :tag_list, value: @debate.tag_list.to_s %> +
    + <% if @debate.new_record? %> <%= f.check_box :terms_of_service %> Acepto la política de privacidad y el aviso legal diff --git a/app/views/debates/show.html.erb b/app/views/debates/show.html.erb index f0a67e527..2b0d88b7c 100644 --- a/app/views/debates/show.html.erb +++ b/app/views/debates/show.html.erb @@ -7,5 +7,8 @@

    +<%= render 'shared/tags', debate: @debate %> + +

    <%= link_to 'Edit', edit_debate_path(@debate) %> | <%= link_to 'Back', debates_path %> \ No newline at end of file diff --git a/app/views/shared/_tags.html.erb b/app/views/shared/_tags.html.erb new file mode 100644 index 000000000..990c0d41f --- /dev/null +++ b/app/views/shared/_tags.html.erb @@ -0,0 +1,3 @@ +<% if debate.tags.any? %> + Temas: <%= tags(debate) %> +<% end %> \ No newline at end of file From e20af8ab063fd6657a34b095ede648d85eaeeafd Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:24:30 +0200 Subject: [PATCH 12/15] adds tag cloud [#8] --- app/assets/stylesheets/debates.scss | 7 +++++++ app/views/debates/index.html.erb | 4 +++- app/views/shared/_tag_cloud.html.erb | 5 +++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 app/assets/stylesheets/debates.scss create mode 100644 app/views/shared/_tag_cloud.html.erb diff --git a/app/assets/stylesheets/debates.scss b/app/assets/stylesheets/debates.scss new file mode 100644 index 000000000..cb4df7794 --- /dev/null +++ b/app/assets/stylesheets/debates.scss @@ -0,0 +1,7 @@ +#tag_cloud { + width: 400px; + line-height: 1.6em; + .s { font-size: 0.8em; } + .m { font-size: 1.2em; } + .l { font-size: 1.8em; } +} \ No newline at end of file diff --git a/app/views/debates/index.html.erb b/app/views/debates/index.html.erb index ffd5803ee..50ac86a50 100644 --- a/app/views/debates/index.html.erb +++ b/app/views/debates/index.html.erb @@ -4,5 +4,7 @@ <%= render @debates %>
    -
    +<%= render 'shared/tag_cloud' %> + +

    <%= link_to 'New Debate', new_debate_path %> diff --git a/app/views/shared/_tag_cloud.html.erb b/app/views/shared/_tag_cloud.html.erb new file mode 100644 index 000000000..1aefd074d --- /dev/null +++ b/app/views/shared/_tag_cloud.html.erb @@ -0,0 +1,5 @@ +
    + <% tag_cloud Debate.tag_counts, %w[s m l] do |tag, css_class| %> + <%= link_to "#{tag.name}(#{tag.taggings_count})", debates_path(tag: tag.name), class: css_class %> + <% end %> +
    From eb083c700b14afd5391728a9a66f435d2d79ccbd Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:28:15 +0200 Subject: [PATCH 13/15] adds tags feature specs [#8] --- spec/features/tags_spec.rb | 99 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 spec/features/tags_spec.rb diff --git a/spec/features/tags_spec.rb b/spec/features/tags_spec.rb new file mode 100644 index 000000000..61376480d --- /dev/null +++ b/spec/features/tags_spec.rb @@ -0,0 +1,99 @@ +require 'rails_helper' + +feature 'Tags' do + + scenario 'Index' do + earth = create(:debate, tag_list: 'Medio Ambiente') + money = create(:debate, tag_list: 'Economía') + + visit debates_path + + within "#debate-#{earth.id}" do + expect(page).to have_content "Temas: Medio Ambiente" + end + + within "#debate-#{money.id}" do + expect(page).to have_content "Temas: Economía" + end + end + + scenario 'Filtered' do + 2.times { create(:debate, tag_list: 'Salud') } + 2.times { create(:debate, tag_list: 'Hacienda') } + + visit debates_path + first(:link, "Salud").click + + expect(page).to have_css('.debate', count: 2) + expect(page).to have_content('Temas: Salud') + expect(page).to_not have_content('Temas: Hacienda') + end + + scenario 'Show' do + debate = create(:debate, tag_list: 'Hacienda, Economía') + + visit debate_path(debate) + + expect(page).to have_content "Temas: Hacienda, Economía" + end + + scenario 'Tag Cloud' do + 1.times { create(:debate, tag_list: 'Medio Ambiente') } + 5.times { create(:debate, tag_list: 'Corrupción') } + 10.times { create(:debate, tag_list: 'Economía') } + + visit debates_path + + within "#tag-cloud" do + expect(page).to have_css(".s", text: "Medio Ambiente(1)") + expect(page).to have_css(".m", text: "Corrupción(5)") + expect(page).to have_css(".l", text: "Economía(10)") + end + end + + scenario 'Create' do + user = create(:user) + login_as(user) + + visit new_debate_path + fill_in 'debate_title', with: 'Title' + fill_in 'debate_description', with: 'Description' + check 'debate_terms_of_service' + + fill_in 'debate_tag_list', with: "Impuestos, Economía, Hacienda" + + click_button 'Crear Debate' + + expect(page).to have_content 'Debate creado correctamente' + expect(page).to have_content 'Temas: Impuestos, Economía, Hacienda' + end + + scenario 'Update' do + debate = create(:debate, tag_list: 'Economía') + + login_as(debate.author) + visit edit_debate_path(debate) + + expect(page).to have_selector("input[value='Economía']") + + fill_in 'debate_tag_list', with: "Economía, Hacienda" + click_button 'Actualizar Debate' + + expect(page).to have_content 'Debate actualizado correctamente' + expect(page).to have_content 'Temas: Economía, Hacienda' + end + + scenario 'Delete' do + debate = create(:debate, tag_list: 'Economía') + + login_as(debate.author) + visit edit_debate_path(debate) + + fill_in 'debate_tag_list', with: "" + click_button 'Actualizar Debate' + + expect(page).to have_content 'Debate actualizado correctamente' + expect(page).to_not have_content 'Temas:' + end + +end \ No newline at end of file From dc8969781a2c742a39339c2e0baefe2bd9b264a7 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:33:45 +0200 Subject: [PATCH 14/15] cleans up schema [#8] --- db/schema.rb | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index a9958aba6..188936e6b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -61,19 +61,4 @@ ActiveRecord::Schema.define(version: 20150718075337) 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 e783c2013a3219a5e40d56eadb3ffcaf8be5308a Mon Sep 17 00:00:00 2001 From: rgarcia Date: Sat, 18 Jul 2015 17:50:40 +0200 Subject: [PATCH 15/15] sanitises tag names [#8] --- app/helpers/application_helper.rb | 2 +- app/views/shared/_tag_cloud.html.erb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 74dcfd229..e3f79f023 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,7 +1,7 @@ module ApplicationHelper def tags(debate) - debate.tag_list.map { |tag| link_to tag, debates_path(tag: tag) }.join(', ').html_safe + debate.tag_list.map { |tag| link_to sanitize(tag), debates_path(tag: tag) }.join(', ').html_safe end end diff --git a/app/views/shared/_tag_cloud.html.erb b/app/views/shared/_tag_cloud.html.erb index 1aefd074d..bb61c5a4d 100644 --- a/app/views/shared/_tag_cloud.html.erb +++ b/app/views/shared/_tag_cloud.html.erb @@ -1,5 +1,5 @@
    <% tag_cloud Debate.tag_counts, %w[s m l] do |tag, css_class| %> - <%= link_to "#{tag.name}(#{tag.taggings_count})", debates_path(tag: tag.name), class: css_class %> + <%= link_to sanitize("#{tag.name}(#{tag.taggings_count})"), debates_path(tag: tag.name), class: css_class %> <% end %>