2018-06-15 21:34:33 +00:00
|
|
|
# Representa los distintos tipos de campos que pueden venir de una
|
|
|
|
# plantilla compleja
|
|
|
|
class Post
|
|
|
|
class TemplateField
|
|
|
|
attr_reader :post, :contents, :key
|
|
|
|
|
|
|
|
def initialize(post, key, contents)
|
|
|
|
@post = post
|
|
|
|
@key = key
|
|
|
|
@contents = contents
|
|
|
|
end
|
|
|
|
|
2018-06-15 22:13:18 +00:00
|
|
|
# Obtiene el valor
|
|
|
|
def value
|
|
|
|
complex? ? contents.dig('value') : contents
|
|
|
|
end
|
|
|
|
|
2018-06-15 21:34:33 +00:00
|
|
|
def type
|
|
|
|
return @type if @type
|
|
|
|
|
|
|
|
case
|
2018-06-15 21:40:22 +00:00
|
|
|
when text_area?
|
2018-06-15 21:34:33 +00:00
|
|
|
@type = 'text_area'
|
2018-06-15 21:40:22 +00:00
|
|
|
when string?
|
|
|
|
@type = 'text'
|
|
|
|
when string? && contents.split('/', 2).count == 2
|
2018-06-15 21:34:33 +00:00
|
|
|
@type = 'select'
|
2018-06-15 21:40:22 +00:00
|
|
|
when array?
|
2018-06-15 21:34:33 +00:00
|
|
|
@type = 'select'
|
2018-06-15 21:40:22 +00:00
|
|
|
when boolean?
|
2018-06-15 21:34:33 +00:00
|
|
|
@type = 'check_box'
|
|
|
|
end
|
|
|
|
|
|
|
|
@type
|
|
|
|
end
|
|
|
|
|
2018-06-15 21:44:35 +00:00
|
|
|
# El campo es requerido si es complejo y se especifica que lo sea
|
|
|
|
def required?
|
|
|
|
complex? && contents.dig('required')
|
|
|
|
end
|
|
|
|
|
2018-06-15 21:40:22 +00:00
|
|
|
def boolean?
|
2018-06-15 21:44:35 +00:00
|
|
|
value.is_a?(FalseClass) || value.is_a?(TrueClass)
|
2018-06-15 21:40:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def string?
|
2018-06-15 21:44:35 +00:00
|
|
|
value.is_a? String
|
2018-06-15 21:40:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def text_area?
|
2018-06-15 21:44:35 +00:00
|
|
|
value == 'text'
|
2018-06-15 21:40:22 +00:00
|
|
|
end
|
|
|
|
|
2018-06-15 21:34:33 +00:00
|
|
|
# Si la plantilla es simple no está admitiendo Hashes como valores
|
|
|
|
def simple?
|
|
|
|
!complex?
|
|
|
|
end
|
|
|
|
|
|
|
|
def complex?
|
|
|
|
contents.is_a? Hash
|
|
|
|
end
|
|
|
|
|
|
|
|
# XXX Retrocompatibilidad
|
|
|
|
def to_s
|
|
|
|
key
|
|
|
|
end
|
|
|
|
|
2018-06-15 21:40:22 +00:00
|
|
|
# Convierte el campo en un parámetro
|
2018-06-15 21:34:33 +00:00
|
|
|
def to_param
|
|
|
|
if array?
|
|
|
|
{ key.to_sym => [] }
|
|
|
|
else
|
|
|
|
key.to_sym
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def array?
|
2018-06-15 21:44:35 +00:00
|
|
|
value.is_a? Array
|
2018-06-15 21:34:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# TODO detectar cuando es complejo y tomar el valor de :multiple
|
|
|
|
def multiple?
|
|
|
|
array?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Obtiene los valores posibles para el campo de la plantilla
|
|
|
|
def values
|
|
|
|
return '' if %w[string text].include? value
|
|
|
|
|
|
|
|
# Para obtener los valores posibles, hay que procesar la string y
|
|
|
|
# convertirla a parametros
|
|
|
|
#
|
|
|
|
# XXX Prestar atención a no enviar metodos privados
|
|
|
|
|
|
|
|
# Si es una array de un solo elemento, es un indicador de que
|
|
|
|
# tenemos que rellenarla con los valores que indica
|
|
|
|
if value.is_a?(Array) && value.count == 1
|
|
|
|
values = value.first
|
|
|
|
else
|
|
|
|
values = value
|
|
|
|
end
|
|
|
|
|
|
|
|
# Procesar el valor
|
|
|
|
if values.is_a?(String)
|
|
|
|
value = values.split(':', 2).map do |v|
|
|
|
|
collection, attr = v.split('/', 2)
|
|
|
|
|
|
|
|
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)
|
|
|
|
else
|
|
|
|
post.site.everything_of(attr, lang: collection)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
value = value.last.zip value.first
|
|
|
|
end
|
|
|
|
|
|
|
|
# En última instancia, traer el valor por defecto
|
|
|
|
value
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|