From 817d5650f846e4ce5f965686067d667d08e8b20e Mon Sep 17 00:00:00 2001 From: f Date: Thu, 13 May 2021 19:48:41 -0300 Subject: [PATCH] =?UTF-8?q?testear=20la=20relaci=C3=B3n=20has=5Fmany?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit un artículo puede tener varios artículos, es la inversa de belongs_to también eliminamos la memoización --- app/models/metadata_has_many.rb | 16 +++------- test/models/metadata_has_many_test.rb | 45 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 test/models/metadata_has_many_test.rb diff --git a/app/models/metadata_has_many.rb b/app/models/metadata_has_many.rb index 6135401..a24a5f1 100644 --- a/app/models/metadata_has_many.rb +++ b/app/models/metadata_has_many.rb @@ -6,14 +6,6 @@ # Localmente tenemos un Array de UUIDs. Remotamente tenemos una String # apuntando a un Post, que se mantiene actualizado como el actual. class MetadataHasMany < MetadataRelatedPosts - # Invalidar la relación anterior - def value_was=(new_value) - @had_many = nil - @has_many = nil - - super(new_value) - end - def validate super @@ -24,14 +16,16 @@ class MetadataHasMany < MetadataRelatedPosts # Todos los Post relacionados def has_many - @has_many ||= posts.where(uuid: value) + return default_value if value.blank? + + posts.where(uuid: value) end # La relación anterior def had_many - return [] if value_was.blank? + return default_value if value_was.blank? - @had_many ||= posts.where(uuid: value_was) + posts.where(uuid: value_was) end def inverse? diff --git a/test/models/metadata_has_many_test.rb b/test/models/metadata_has_many_test.rb new file mode 100644 index 0000000..637c942 --- /dev/null +++ b/test/models/metadata_has_many_test.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require 'test_helper' +require_relative 'metadata_test' + +class MetadataHasManyTest < ActiveSupport::TestCase + include MetadataTest + + test 'se pueden relacionar artículos' do + reply = @site.posts.create(layout: :post, title: SecureRandom.hex) + post = @site.posts.create(layout: :post, title: SecureRandom.hex, posts: [reply.uuid.value]) + + assert_equal post, reply.in_reply_to.belongs_to + assert_includes post.posts.has_many, reply + end + + test 'se puede eliminar la relación' do + reply = @site.posts.create(layout: :post, title: SecureRandom.hex) + post = @site.posts.create(layout: :post, title: SecureRandom.hex, posts: [reply.uuid.value]) + + post.posts.value = [] + post.save + + assert_not_equal post, reply.in_reply_to.belongs_to + assert_equal post, reply.in_reply_to.belonged_to + assert_nil reply.in_reply_to.belongs_to + assert_not_includes post.posts.has_many, reply + end + + test 'se puede cambiar la relación' do + reply = @site.posts.create(layout: :post, title: SecureRandom.hex) + post1 = @site.posts.create(layout: :post, title: SecureRandom.hex, posts: [reply.uuid.value]) + post2 = @site.posts.create(layout: :post, title: SecureRandom.hex) + + reply.in_reply_to.value = post2.uuid.value + reply.save + + assert_not_equal post1, reply.in_reply_to.belongs_to + assert_equal post1, reply.in_reply_to.belonged_to + assert_not_includes post1.posts.has_many, reply + + assert_equal post2, reply.in_reply_to.belongs_to + assert_includes post2.posts.has_many, reply + end +end