5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 15:36:22 +00:00

no permitir artículos duplicados

This commit is contained in:
f 2021-03-25 19:17:34 -03:00
parent 365146ed59
commit e3727eb6b2
2 changed files with 29 additions and 0 deletions

View file

@ -267,6 +267,18 @@ class Post
return false if validate && !valid?
# Salir si tenemos que cambiar el nombre del archivo y no pudimos
return false if !new? && path.changed? && !update_path!
# Si el archivo va a estar duplicado, agregar un número al slug
if new? && written?
original_slug = slug.value
count = 1
while written?
count += 1
slug.value = "#{original_slug}-#{count}"
end
end
return false unless save_attributes!
return false unless write

View file

@ -181,4 +181,21 @@ class PostTest < ActiveSupport::TestCase
assert_equal title, post.content.value
assert post.save
end
test 'no se pueden repetir los nombres' do
title = SecureRandom.hex
post = @site.posts.build(title: title,
description: title,
content: title)
slug = post.slug.value
assert post.save
another_post = @site.posts.build(title: title, description: title, content: title)
assert_equal slug, another_post.slug.value
assert another_post.save
assert_not_equal slug, another_post.slug.value
end
end