Return error when the related content already exists
It looks like this bug was introduced in commit 7ca55c4. Before that
commit, a message saying "You added a new related content" was shown,
but (as expected) the related content wasn't added twice.
We now check this case and add a slightly clearer message.
This commit is contained in:
@@ -15,6 +15,8 @@ class RelatedContentsController < ApplicationController
|
|||||||
flash[:success] = t("related_content.success")
|
flash[:success] = t("related_content.success")
|
||||||
elsif related_content.same_parent_and_child?
|
elsif related_content.same_parent_and_child?
|
||||||
flash[:error] = t("related_content.error_itself")
|
flash[:error] = t("related_content.error_itself")
|
||||||
|
elsif related_content.duplicate?
|
||||||
|
flash[:error] = t("related_content.error_duplicate")
|
||||||
else
|
else
|
||||||
flash[:error] = t("related_content.error", url: Setting["url"])
|
flash[:error] = t("related_content.error", url: Setting["url"])
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -35,6 +35,10 @@ class RelatedContent < ApplicationRecord
|
|||||||
parent_relationable == child_relationable
|
parent_relationable == child_relationable
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def duplicate?
|
||||||
|
parent_relationable.relationed_contents.include?(child_relationable)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def different_parent_and_child
|
def different_parent_and_child
|
||||||
|
|||||||
@@ -927,6 +927,7 @@ en:
|
|||||||
help: "You can add links of %{models} inside of %{org}."
|
help: "You can add links of %{models} inside of %{org}."
|
||||||
submit: "Add"
|
submit: "Add"
|
||||||
error: "Link not valid. Remember to start with %{url}."
|
error: "Link not valid. Remember to start with %{url}."
|
||||||
|
error_duplicate: "The related content you are adding already exists."
|
||||||
error_itself: "Link not valid. You cannot relate a content to itself."
|
error_itself: "Link not valid. You cannot relate a content to itself."
|
||||||
success: "You added a new related content"
|
success: "You added a new related content"
|
||||||
is_related: "Is it related content?"
|
is_related: "Is it related content?"
|
||||||
|
|||||||
@@ -927,6 +927,7 @@ es:
|
|||||||
help: "Puedes introducir cualquier enlace de %{models} que esté dentro de %{org}."
|
help: "Puedes introducir cualquier enlace de %{models} que esté dentro de %{org}."
|
||||||
submit: "Añadir"
|
submit: "Añadir"
|
||||||
error: "Enlace no válido. Recuerda que debe empezar por %{url}."
|
error: "Enlace no válido. Recuerda que debe empezar por %{url}."
|
||||||
|
error_duplicate: "El contenido relacionado que estás añadiendo ya existe."
|
||||||
error_itself: "Enlace no válido. No se puede relacionar un contenido consigo mismo."
|
error_itself: "Enlace no válido. No se puede relacionar un contenido consigo mismo."
|
||||||
success: "Has añadido un nuevo contenido relacionado"
|
success: "Has añadido un nuevo contenido relacionado"
|
||||||
is_related: "¿Es contenido relacionado?"
|
is_related: "¿Es contenido relacionado?"
|
||||||
|
|||||||
@@ -117,6 +117,21 @@ shared_examples "relationable" do |relationable_model_name|
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario "returns an error when the related content already exists" do
|
||||||
|
create(:related_content, parent_relationable: relationable, child_relationable: related1)
|
||||||
|
login_as(user)
|
||||||
|
visit relationable.url
|
||||||
|
|
||||||
|
click_button "Add related content"
|
||||||
|
|
||||||
|
within("#related_content") do
|
||||||
|
fill_in "url", with: url + related1.url.to_s
|
||||||
|
click_button "Add"
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(page).to have_content "The related content you are adding already exists."
|
||||||
|
end
|
||||||
|
|
||||||
scenario "related content can be scored positively" do
|
scenario "related content can be scored positively" do
|
||||||
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user