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

testear relación has_and_belongs_to_many

un artículo puede pertenecer y tener muchos artículos (una relación
poliamorosa de muchos a muchos)

también utilizamos asignación para aprovechar `value=`.
This commit is contained in:
f 2021-05-13 19:50:41 -03:00
parent 817d5650f8
commit fc6e7da5d6
2 changed files with 65 additions and 12 deletions

View file

@ -18,6 +18,7 @@ class MetadataHasAndBelongsToMany < MetadataHasMany
#
# Buscamos en belongs_to la relación local, si se eliminó hay que
# quitarla de la relación remota, sino hay que agregarla.
#
def save
# XXX: No usamos super
self[:value] = sanitize value
@ -25,27 +26,21 @@ class MetadataHasAndBelongsToMany < MetadataHasMany
return true unless changed?
return true unless inverse?
# XXX: Usamos asignación para aprovechar value= que setea el valor
# anterior en @value_was
(had_many - has_many).each do |remove|
remove[inverse]&.value&.delete post.uuid.value
remove[inverse].value = remove[inverse].value.reject do |rej|
rej == post.uuid.value
end
end
(has_many - had_many).each do |add|
next unless add[inverse]
next if add[inverse].value.include? post.uuid.value
add[inverse].value << post.uuid.value
add[inverse].value = (add[inverse].value.dup << post.uuid.value)
end
true
end
private
# Igual que en MetadataRelatedPosts
# TODO: Mover a un módulo
def sanitize(uuid)
super(uuid.map do |u|
u.to_s.gsub(/[^a-f0-9\-]/i, '')
end)
end
end

View file

@ -0,0 +1,58 @@
# frozen_string_literal: true
require 'test_helper'
require_relative 'metadata_test'
class MetadataHasAndBelongsManyTest < ActiveSupport::TestCase
include MetadataTest
test 'se pueden relacionar artículos' do
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
post = @site.posts.create(layout: :post, title: SecureRandom.hex)
post.authors.value = [author.uuid.value]
assert post.save
assert_includes author.posts.has_many, post
assert_includes post.authors.has_many, author
end
test 'se puede eliminar la relación' do
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
post = @site.posts.create(layout: :post, title: SecureRandom.hex, authors: [author.uuid.value])
assert_includes post.authors.value, author.uuid.value
assert_includes author.posts.value, post.uuid.value
post.authors.value = []
ENV['HOLA'] = 'hola'
assert post.save
assert_not_includes author.posts.has_many, post
assert_not_includes post.authors.has_many, author
assert_includes author.posts.had_many, post
assert_includes post.authors.had_many, author
end
test 'se puede cambiar la relación' do
author = @site.posts.create(layout: :author, title: SecureRandom.hex)
post1 = @site.posts.create(layout: :post, title: SecureRandom.hex, authors: [author.uuid.value])
post2 = @site.posts.create(layout: :post, title: SecureRandom.hex)
author.posts.value = [post2.uuid.value]
assert author.save
assert_not_includes author.posts.has_many, post1
assert_not_includes post1.authors.has_many, author
assert_includes author.posts.had_many, post1
assert_includes post1.authors.had_many, author
assert_not_includes author.posts.had_many, post2
assert_not_includes post2.authors.had_many, author
assert_includes author.posts.has_many, post2
assert_includes post2.authors.has_many, author
end
end