5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-22 19:26: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
end
def nested?
false
end
def inspect
"#<#{self.class} site=#{site.name.inspect} post=#{post.id.inspect} value=#{value.inspect}>"
end

View file

@ -423,6 +423,13 @@ class Post
@usuaries ||= document_usuaries.empty? ? [] : Usuarie.where(id: document_usuaries).to_a
end
# Todos los atributos anidados
#
# @return [Array<Symbol>]
def nested_attributes
@nested_attributes ||= attributes.map { |a| self[a] }.select(&:nested?).map(&:name)
end
private
# 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?
end
# Crea los posts anidados
create_nested_posts! post, params[:post]
update_related_posts
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
def post_params
params.require(:post).permit(post.params)
@post_params ||= params.require(:post).permit(post.params).to_h
end
# 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')
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