Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the management of related content #4499

Merged
merged 1 commit into from
Jun 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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