From 74124fb784edc704d7a1e544aa46e95c11a32f6a Mon Sep 17 00:00:00 2001 From: Vicente Mendoza Date: Thu, 1 Feb 2018 14:28:56 +0100 Subject: [PATCH 1/2] Control when the investment is trying to relate to itself. --- app/controllers/related_contents_controller.rb | 11 ++++++++--- config/locales/en/general.yml | 1 + config/locales/es/general.yml | 1 + config/locales/val/general.yml | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/related_contents_controller.rb b/app/controllers/related_contents_controller.rb index dd25cf02f..db6be0018 100644 --- a/app/controllers/related_contents_controller.rb +++ b/app/controllers/related_contents_controller.rb @@ -5,13 +5,18 @@ class RelatedContentsController < ApplicationController def create if relationable_object && related_object - RelatedContent.create(parent_relationable: @relationable, child_relationable: @related, author: current_user) - flash[:success] = t('related_content.success') + if relationable_object.url != related_object.url + RelatedContent.create(parent_relationable: @relationable, child_relationable: @related, author: current_user) + + flash[:success] = t('related_content.success') + else + flash[:error] = t('related_content.error_itself') + end + else flash[:error] = t('related_content.error', url: Setting['url']) end - redirect_to @relationable.url end diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index c3960a3df..29686093a 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -821,6 +821,7 @@ en: help: "You can add links of %{models} inside of %{org}." submit: "Add" error: "Link not valid. Remember to start with %{url}." + error_itself: "Link not valid. You cannot relate a content to itself." success: "You added a new related content" is_related: "¿Is it related content?" score_positive: "Yes" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 3200f4be0..d80109f03 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -820,6 +820,7 @@ es: help: "Puedes introducir cualquier enlace de %{models} que esté dentro de %{org}." submit: "Añadir" error: "Enlace no válido. Recuerda que debe empezar por %{url}." + error_itself: "Enlace no válido. No se puede relacionar un contenido consigo mismo." success: "Has añadido un nuevo contenido relacionado" is_related: "¿Es contenido relacionado?" score_positive: "Sí" diff --git a/config/locales/val/general.yml b/config/locales/val/general.yml index 4a8dc195f..58f49b1bd 100644 --- a/config/locales/val/general.yml +++ b/config/locales/val/general.yml @@ -819,6 +819,7 @@ val: help: "Pots afegir enllaços de %{models} en %{org}." submit: "Afegir" error: "Enllaç no vàlid. Recorda iniciar amb %{url}." + error_itself: "Enllaç no vàlid. No es pot relacionar un contingut aconseguisc mateix" success: "Has afegit nou contingut relacionat" is_related: "Es contingut relacionat?" score_positive: "Sí" From 295facccb1a1ac91cdb1b525158ad70ed6094a29 Mon Sep 17 00:00:00 2001 From: Vicente Mendoza Date: Fri, 2 Feb 2018 10:57:09 +0100 Subject: [PATCH 2/2] Test to check that returns error when it comes to relate an investment content with itself. --- spec/shared/features/relationable.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/shared/features/relationable.rb b/spec/shared/features/relationable.rb index 9753e79ff..d2e1c4cdb 100644 --- a/spec/shared/features/relationable.rb +++ b/spec/shared/features/relationable.rb @@ -71,6 +71,20 @@ shared_examples "relationable" do |relationable_model_name| expect(page).to have_content("Link not valid. Remember to start with #{Setting[:url]}.") end + scenario 'returns error when relating content URL to itself' do + login_as(user) + visit relationable.url + + click_on("Add related content") + + within("#related_content") do + fill_in 'url', with: Setting[:url] + relationable.url.to_s + click_button "Add" + end + + expect(page).to have_content("Link not valid. You cannot relate a content to itself") + end + scenario 'related content can be scored positively', :js do related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))