From 1e2c29f3090ee5cb39592ce3b9a63622791fa44d Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 12:47:51 -0300 Subject: [PATCH 01/11] feat: metadata has one #12985 --- app/models/metadata_has_one.rb | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 app/models/metadata_has_one.rb diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb new file mode 100644 index 00000000..49fd65bf --- /dev/null +++ b/app/models/metadata_has_one.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class MetadataHasOne < MetadataHasMany +end From b06751e62e5310a7b33c529c51cd8273e50db2c3 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:09:40 -0300 Subject: [PATCH 02/11] feat: establecer relaciones bidireccionales --- app/models/metadata_belongs_to.rb | 14 +++++++++++--- app/models/metadata_has_one.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb index be1fa670..e1b84438 100644 --- a/app/models/metadata_belongs_to.rb +++ b/app/models/metadata_belongs_to.rb @@ -40,13 +40,21 @@ class MetadataBelongsTo < MetadataRelatedPosts # Si estamos cambiando la relaciĆ³n, tenemos que eliminar la relaciĆ³n # anterior if belonged_to.present? - belonged_to[inverse].value = belonged_to[inverse].value.reject do |rej| - rej == post.uuid.value + if belonged_to[inverse].respond_to? :has_one + belonged_to[inverse].value = nil + else + belonged_to[inverse].value = belonged_to[inverse].value.reject do |rej| + rej == post.uuid.value + end end end # No duplicar las relaciones - belongs_to[inverse].value = (belongs_to[inverse].value.dup << post.uuid.value) unless belongs_to.blank? || included? + if belongs_to[inverse].respond_to? :has_one + belongs_to[inverse].value = post.uuid.value + else + belongs_to[inverse].value = (belongs_to[inverse].value.dup << post.uuid.value) unless belongs_to.blank? || included? + end true end diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 49fd65bf..38911e1d 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -1,4 +1,34 @@ # frozen_string_literal: true class MetadataHasOne < MetadataHasMany + def had_one + return default_value if value_was.blank? + + posts.find(uuid: value) + end + + def has_one + return default_value if value.blank? + + posts.find(uuid: value) + end + + def had_many; end + def has_many; end + + def save + super + + return true unless changed? + return true unless inverse? + + had_one&.value = nil + has_one&.value = post.uuid.value + + true + end + + def related_methods + @related_methods ||= %i[has_one had_one].freeze + end end From a62039b50a1057b8f8b42689acda6a4c5871689a Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:26:56 -0300 Subject: [PATCH 03/11] fix: editar desde el panel --- app/views/posts/attribute_ro/_has_one.haml | 6 ++++++ app/views/posts/attributes/_has_one.haml | 7 +++++++ 2 files changed, 13 insertions(+) create mode 100644 app/views/posts/attribute_ro/_has_one.haml create mode 100644 app/views/posts/attributes/_has_one.haml diff --git a/app/views/posts/attribute_ro/_has_one.haml b/app/views/posts/attribute_ro/_has_one.haml new file mode 100644 index 00000000..c7e06be8 --- /dev/null +++ b/app/views/posts/attribute_ro/_has_one.haml @@ -0,0 +1,6 @@ +%tr{ id: attribute } + %th= post_label_t(attribute, post: post) + %td{ dir: dir, lang: locale } + - p = metadata.belongs_to + - if p + = link_to p.title.value, site_post_path(site, p.id) diff --git a/app/views/posts/attributes/_has_one.haml b/app/views/posts/attributes/_has_one.haml new file mode 100644 index 00000000..b0d21f35 --- /dev/null +++ b/app/views/posts/attributes/_has_one.haml @@ -0,0 +1,7 @@ +.form-group + = label_tag "#{base}_#{attribute}", post_label_t(attribute, post: post) + = select_tag(plain_field_name_for(base, attribute), + options_for_select(metadata.values, metadata.value), + **field_options(attribute, metadata), include_blank: t('.empty')) + = render 'posts/attribute_feedback', + post: post, attribute: attribute, metadata: metadata From 6e3408777790211724ef79b91dfc50ca7401086f Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:28:45 -0300 Subject: [PATCH 04/11] fixup! fix: editar desde el panel --- app/views/posts/attribute_ro/_has_one.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/posts/attribute_ro/_has_one.haml b/app/views/posts/attribute_ro/_has_one.haml index c7e06be8..425e659e 100644 --- a/app/views/posts/attribute_ro/_has_one.haml +++ b/app/views/posts/attribute_ro/_has_one.haml @@ -1,6 +1,6 @@ %tr{ id: attribute } %th= post_label_t(attribute, post: post) %td{ dir: dir, lang: locale } - - p = metadata.belongs_to + - p = metadata.has_one - if p = link_to p.title.value, site_post_path(site, p.id) From a053fe6239a2e3258b60cef96cdf6bb14c7bbf1a Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:36:13 -0300 Subject: [PATCH 05/11] fix: no comportarse como un array --- app/models/metadata_has_one.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 38911e1d..686afc6e 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class MetadataHasOne < MetadataHasMany +class MetadataHasOne < MetadataBelongsTo def had_one return default_value if value_was.blank? @@ -13,11 +13,18 @@ class MetadataHasOne < MetadataHasMany posts.find(uuid: value) end - def had_many; end - def has_many; end + def belonged_to; end + def belongs_to; end def save - super + # XXX: DRY + if !changed? + self[:value] = document_value + + return true + end + + self[:value] = sanitize value return true unless changed? return true unless inverse? @@ -31,4 +38,8 @@ class MetadataHasOne < MetadataHasMany def related_methods @related_methods ||= %i[has_one had_one].freeze end + + def indexable_values + has_one&.title&.value + end end From b09d051de19a9b8ea8a3063e079be30062e72d40 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:42:09 -0300 Subject: [PATCH 06/11] fix: sintaxis correcta para encontrar uno solo --- app/models/metadata_has_one.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 686afc6e..41b2f7cd 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -4,13 +4,13 @@ class MetadataHasOne < MetadataBelongsTo def had_one return default_value if value_was.blank? - posts.find(uuid: value) + posts.find(value, uuid: true) end def has_one return default_value if value.blank? - posts.find(uuid: value) + posts.find(value, uuid: true) end def belonged_to; end From ce7c7ffd96d07a421c9f0537cb161a71ac4265e4 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:45:02 -0300 Subject: [PATCH 07/11] fix: reusar belongs to --- app/models/metadata_has_one.rb | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 41b2f7cd..239bf2e8 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -1,20 +1,8 @@ # frozen_string_literal: true class MetadataHasOne < MetadataBelongsTo - def had_one - return default_value if value_was.blank? - - posts.find(value, uuid: true) - end - - def has_one - return default_value if value.blank? - - posts.find(value, uuid: true) - end - - def belonged_to; end - def belongs_to; end + alias has_one belongs_to + alias had_one belonged_to def save # XXX: DRY @@ -38,8 +26,4 @@ class MetadataHasOne < MetadataBelongsTo def related_methods @related_methods ||= %i[has_one had_one].freeze end - - def indexable_values - has_one&.title&.value - end end From 72dd7e66ad7abac6924d1eaa2dfd70be755336fd Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:50:49 -0300 Subject: [PATCH 08/11] fix: poder guardar --- app/models/metadata_belongs_to.rb | 2 +- app/models/metadata_has_one.rb | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb index e1b84438..8726f1b1 100644 --- a/app/models/metadata_belongs_to.rb +++ b/app/models/metadata_belongs_to.rb @@ -41,7 +41,7 @@ class MetadataBelongsTo < MetadataRelatedPosts # anterior if belonged_to.present? if belonged_to[inverse].respond_to? :has_one - belonged_to[inverse].value = nil + belonged_to[inverse].value = '' else belonged_to[inverse].value = belonged_to[inverse].value.reject do |rej| rej == post.uuid.value diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 239bf2e8..93aaba50 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -17,8 +17,8 @@ class MetadataHasOne < MetadataBelongsTo return true unless changed? return true unless inverse? - had_one&.value = nil - has_one&.value = post.uuid.value + had_one[inverse]&.value = '' + has_one[inverse]&.value = post.uuid.value true end From 29bae31e08b414f568cf862e27c6ed3d571a3e36 Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:52:38 -0300 Subject: [PATCH 09/11] fix: quizas viene vacio --- app/models/metadata_has_one.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index 93aaba50..ae5c6617 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -17,8 +17,8 @@ class MetadataHasOne < MetadataBelongsTo return true unless changed? return true unless inverse? - had_one[inverse]&.value = '' - has_one[inverse]&.value = post.uuid.value + had_one&[inverse]&.value = '' + has_one&[inverse]&.value = post.uuid.value true end From e8c659be837b2435598794d55ff757aa12741c2c Mon Sep 17 00:00:00 2001 From: f Date: Sat, 8 Apr 2023 17:54:07 -0300 Subject: [PATCH 10/11] fixup! fix: quizas viene vacio --- app/models/metadata_has_one.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/metadata_has_one.rb b/app/models/metadata_has_one.rb index ae5c6617..d1705e7d 100644 --- a/app/models/metadata_has_one.rb +++ b/app/models/metadata_has_one.rb @@ -17,8 +17,8 @@ class MetadataHasOne < MetadataBelongsTo return true unless changed? return true unless inverse? - had_one&[inverse]&.value = '' - has_one&[inverse]&.value = post.uuid.value + had_one[inverse]&.value = '' if had_one + has_one[inverse]&.value = post.uuid.value if has_one true end From 9ed22fb065d9c7367429b9034e298d6a9b4e0774 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 10 Apr 2023 18:06:01 -0300 Subject: [PATCH 11/11] =?UTF-8?q?fix:=20verificar=20que=20haya=20una=20rel?= =?UTF-8?q?aci=C3=B3n=20#13005?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/models/metadata_belongs_to.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/models/metadata_belongs_to.rb b/app/models/metadata_belongs_to.rb index 8726f1b1..17ecaee8 100644 --- a/app/models/metadata_belongs_to.rb +++ b/app/models/metadata_belongs_to.rb @@ -50,10 +50,12 @@ class MetadataBelongsTo < MetadataRelatedPosts end # No duplicar las relaciones - if belongs_to[inverse].respond_to? :has_one - belongs_to[inverse].value = post.uuid.value - else - belongs_to[inverse].value = (belongs_to[inverse].value.dup << post.uuid.value) unless belongs_to.blank? || included? + if belongs_to.present? + if belongs_to[inverse].respond_to? :has_one + belongs_to[inverse].value = post.uuid.value + else + belongs_to[inverse].value = (belongs_to[inverse].value.dup << post.uuid.value) unless belongs_to.blank? || included? + end end true