Skip to content

Commit

Permalink
Return error when the related content already exists
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
taitus authored and javierm committed Jun 25, 2021
1 parent d2876f5 commit ee66c41
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/related_contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def create
flash[:success] = t("related_content.success")
elsif related_content.same_parent_and_child?
flash[:error] = t("related_content.error_itself")
elsif related_content.duplicate?
flash[:error] = t("related_content.error_duplicate")
else
flash[:error] = t("related_content.error", url: Setting["url"])
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/related_content.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def same_parent_and_child?
parent_relationable == child_relationable
end

def duplicate?
parent_relationable.relationed_contents.include?(child_relationable)
end

private

def different_parent_and_child
Expand Down
1 change: 1 addition & 0 deletions config/locales/en/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ en:
help: "You can add links of %{models} inside of %{org}."
submit: "Add"
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."
success: "You added a new related content"
is_related: "Is it related content?"
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,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_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."
success: "Has añadido un nuevo contenido relacionado"
is_related: "¿Es contenido relacionado?"
Expand Down
15 changes: 15 additions & 0 deletions spec/shared/system/relationable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,21 @@
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
related_content = create(:related_content, parent_relationable: relationable, child_relationable: related1, author: build(:user))

Expand Down

0 comments on commit ee66c41

Please sign in to comment.