5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-23 04:06:21 +00:00

feat: crear artículos anidados #15066

This commit is contained in:
f 2024-04-17 18:09:05 -03:00
parent 363e2d8385
commit 5e2bddf7cc
No known key found for this signature in database
3 changed files with 30 additions and 1 deletions

View file

@ -12,6 +12,10 @@ MetadataTemplate = Struct.new(:site, :document, :name, :label, :type,
false false
end end
def nested?
false
end
def inspect def inspect
"#<#{self.class} site=#{site.name.inspect} post=#{post.id.inspect} value=#{value.inspect}>" "#<#{self.class} site=#{site.name.inspect} post=#{post.id.inspect} value=#{value.inspect}>"
end end

View file

@ -423,6 +423,13 @@ class Post
@usuaries ||= document_usuaries.empty? ? [] : Usuarie.where(id: document_usuaries).to_a @usuaries ||= document_usuaries.empty? ? [] : Usuarie.where(id: document_usuaries).to_a
end end
# Todos los atributos anidados
#
# @return [Array<Symbol>]
def nested_attributes
@nested_attributes ||= attributes.map { |a| self[a] }.select(&:nested?).map(&:name)
end
private private
# Levanta un error si al construir el artículo no pasamos un atributo. # Levanta un error si al construir el artículo no pasamos un atributo.

View file

@ -17,6 +17,8 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
post.slug.value = p[:slug] if p[:slug].present? post.slug.value = p[:slug] if p[:slug].present?
end end
# Crea los posts anidados
create_nested_posts! post, params[:post]
update_related_posts update_related_posts
commit(action: :created, add: files) if post.save commit(action: :created, add: files) if post.save
@ -122,7 +124,7 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
# Solo permitir cambiar estos atributos de cada articulo # Solo permitir cambiar estos atributos de cada articulo
def post_params def post_params
params.require(:post).permit(post.params) @post_params ||= params.require(:post).permit(post.params).to_h
end end
# Eliminar metadatos internos # Eliminar metadatos internos
@ -173,4 +175,20 @@ PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
site.update licencia: Licencia.find_by_icons('custom') site.update licencia: Licencia.find_by_icons('custom')
end end
end end
# Encuentra todos los posts anidados y los crea o modifica
def create_nested_posts!(post, params)
post.nested_attributes.each do |nested_attribute|
nested_metadata = post[nested_attribute]
# @todo find_or_initialize
nested_post = site.posts(lang: post.lang.value).build(layout: nested_metadata.nested)
nested_params = params.require(nested_attribute).permit(nested_post.params).to_hash
# Completa la relación 1:1
nested_params[nested_metadata.inverse.to_s] = post.uuid.value
post[nested_attribute].value = nested_post.uuid.value
files << nested_post.path.absolute if nested_post.update(nested_params)
end
end
end end