5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-04 15:35:44 +00:00

los autocompletados pueden tener un nivel mas

This commit is contained in:
f 2018-06-21 14:32:54 -03:00
parent 9c06220f9b
commit 6d3a3739b3
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7

View file

@ -4,6 +4,8 @@ class Post
class TemplateField
attr_reader :post, :contents, :key
STRING_VALUES = %w[string text url number email].freeze
def initialize(post, key, contents)
@post = post
@key = key
@ -79,7 +81,9 @@ class Post
# Convierte el campo en un parámetro
def to_param
if array?
if nested?
{ key.to_sym => {} }
elsif array?
{ key.to_sym => [] }
else
key.to_sym
@ -157,7 +161,7 @@ class Post
# Obtiene los valores posibles para el campo de la plantilla
def values
return '' if %w[string text].include? value
return '' if STRING_VALUES.include? value
# Para obtener los valores posibles, hay que procesar la string y
# convertirla a parametros
@ -175,15 +179,29 @@ class Post
# Procesar el valor
if values.is_a?(String)
value = values.split(':', 2).map do |v|
collection, attr = v.split('/', 2)
collection, attr, subattr = v.split('/', 3)
if collection == 'site'
# TODO puede ser peligroso permitir acceder a cualquier
# atributo de site? No estamos trayendo nada fuera de
# lo normal
post.site.send(attr.to_sym)
# Si hay un subatributo, tenemos que averiguar todos los
# valores dentro de el
# TODO volver elegante!
elsif subattr
post.site.everything_of(attr, lang: collection).compact.map do |v|
tv = v.dig('value')
if tv.is_a? Array
tv.map do |sv|
sv[subattr]
end
else
tv[subattr]
end
end
else
post.site.everything_of(attr, lang: collection)
post.site.everything_of(attr, lang: collection).compact
end
end