From fc6e7da5d698c95f10ad10749e9c703a4c6f86ea Mon Sep 17 00:00:00 2001 From: f Date: Thu, 13 May 2021 19:50:41 -0300 Subject: [PATCH] =?UTF-8?q?testear=20relaci=C3=B3n=20has=5Fand=5Fbelongs?= =?UTF-8?q?=5Fto=5Fmany?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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=`. --- .../metadata_has_and_belongs_to_many.rb | 19 +++--- .../metadata_has_and_belongs_to_many_test.rb | 58 +++++++++++++++++++ 2 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 test/models/metadata_has_and_belongs_to_many_test.rb diff --git a/app/models/metadata_has_and_belongs_to_many.rb b/app/models/metadata_has_and_belongs_to_many.rb index f14827ec..2c4f3d43 100644 --- a/app/models/metadata_has_and_belongs_to_many.rb +++ b/app/models/metadata_has_and_belongs_to_many.rb @@ -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 diff --git a/test/models/metadata_has_and_belongs_to_many_test.rb b/test/models/metadata_has_and_belongs_to_many_test.rb new file mode 100644 index 00000000..1404e145 --- /dev/null +++ b/test/models/metadata_has_and_belongs_to_many_test.rb @@ -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