From c1f1571891e203e2c9d3411e41a082a39b83bbf0 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 23 May 2024 18:19:59 -0300 Subject: [PATCH] feat: ignorar algunos campos (cherry picked from commit 9b3db70bf424e24c96e1c724721da9bf072dc27c) --- app/models/post.rb | 24 +++++++++---------- app/views/posts/_attributes.haml | 3 ++- app/views/posts/_htmx_form.haml | 8 ++++++- app/views/posts/attributes/_new_has_many.haml | 2 ++ 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 327df3e2..e6910e18 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -247,18 +247,6 @@ class Post @metadata[:created_at] ||= MetadataCreatedAt.new(document: document, site: site, layout: layout, name: :created_at, type: :created_at, post: self, required: true) end - # Detecta si es un atributo válido o no, a partir de la tabla de la - # plantilla - def attribute?(mid) - included = DEFAULT_ATTRIBUTES.include?(mid) || - PRIVATE_ATTRIBUTES.include?(mid) || - PUBLIC_ATTRIBUTES.include?(mid) - - included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes) - - included - end - # Devuelve los strong params para el layout. # # XXX: Nos gustaría no tener que instanciar Metadata acá, pero depende @@ -430,6 +418,18 @@ class Post @nested_attributes ||= attributes.map { |a| self[a] }.select(&:nested?).map(&:name) end + # Detecta si es un atributo válido o no, a partir de la tabla de la + # plantilla + def attribute?(mid) + included = DEFAULT_ATTRIBUTES.include?(mid) || + PRIVATE_ATTRIBUTES.include?(mid) || + PUBLIC_ATTRIBUTES.include?(mid) + + included = attributes.include? mid if !included && singleton_class.method_defined?(:attributes) + + included + end + private # Levanta un error si al construir el artículo no pasamos un atributo. diff --git a/app/views/posts/_attributes.haml b/app/views/posts/_attributes.haml index ed958d08..d3c205dc 100644 --- a/app/views/posts/_attributes.haml +++ b/app/views/posts/_attributes.haml @@ -4,7 +4,8 @@ @param post [Post] @param site [Site] @param dir [String] -- post.attributes.each do |attribute| + @param except [Array] +- (post.attributes - local_assigns[:except].to_a).each do |attribute| - metadata = post[attribute] - type = metadata.type diff --git a/app/views/posts/_htmx_form.haml b/app/views/posts/_htmx_form.haml index cd482b35..cfc4071a 100644 --- a/app/views/posts/_htmx_form.haml +++ b/app/views/posts/_htmx_form.haml @@ -6,6 +6,12 @@ @param :locale [Symbol, String] @param :dir [Symbol, String] :ruby + except = %i[date] + + if (inverse = params.permit(:inverse).try(:[], :inverse).presence) + except << inverse.to_sym + end + options = { id: base, multipart: true, @@ -33,4 +39,4 @@ = hidden_field_tag "#{base}[layout]", post.layout.name -# Dibuja cada atributo - = render 'posts/attributes', site: site, post: post, dir: dir, base: base, locale: locale + = render 'posts/attributes', site: site, post: post, dir: dir, base: base, locale: locale, except: except diff --git a/app/views/posts/attributes/_new_has_many.haml b/app/views/posts/attributes/_new_has_many.haml index 275d0335..a9225032 100644 --- a/app/views/posts/attributes/_new_has_many.haml +++ b/app/views/posts/attributes/_new_has_many.haml @@ -86,6 +86,8 @@ %input{ type: 'hidden', name: 'swap', value: 'beforeend' } %input{ type: 'hidden', name: 'base', value: id } %input{ type: 'hidden', name: 'name', value: name } + - if metadata.inverse? + %input{ type: 'hidden', name: 'inverse', value: metadata.inverse } %div{ id: post_modal_id, data: { controller: 'modal' } } = render 'bootstrap/modal', id: post_id, modal_content_attributes: { class: 'h-100' } do - content_for :"#{post_id}_body" do