2020-05-30 19:43:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Site
|
2020-06-02 23:41:40 +00:00
|
|
|
# Gestor de formularios del sitio
|
2020-05-30 19:43:25 +00:00
|
|
|
module Forms
|
|
|
|
# Esta clase es un Hash que es capaz de convertirse a strong params
|
|
|
|
# según la definición del formulario en el sitio.
|
|
|
|
class Form < Hash
|
|
|
|
# Convierte el formulario a strong params
|
|
|
|
#
|
|
|
|
# @return Array
|
|
|
|
def params
|
|
|
|
map do |field, definition|
|
|
|
|
if ARRAY_FIELDS.include? definition['type']
|
|
|
|
{ field.to_sym => [] }
|
|
|
|
else
|
|
|
|
field.to_sym
|
|
|
|
end
|
|
|
|
end.compact
|
|
|
|
end
|
|
|
|
|
2020-06-03 13:57:52 +00:00
|
|
|
# Obtiene la traducción del campo
|
|
|
|
#
|
|
|
|
# @return String
|
2020-05-30 19:43:25 +00:00
|
|
|
def t(field)
|
2020-06-02 23:41:40 +00:00
|
|
|
dig(field.to_s, 'label', I18n.locale.to_s)
|
2020-05-30 19:43:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Campos del formulario que no son necesarios
|
2020-06-02 23:41:40 +00:00
|
|
|
EXCLUDED_FIELDS = %w[separator redirect].freeze
|
2020-05-30 19:43:25 +00:00
|
|
|
# Campos que aceptan valores múltiples (checkboxes, input-map,
|
|
|
|
# input-tag)
|
|
|
|
ARRAY_FIELDS = %w[array].freeze
|
|
|
|
|
|
|
|
# Obtiene todos los formularios disponibles
|
|
|
|
#
|
|
|
|
# @return Array Formularios disponibles para este sitio
|
|
|
|
def forms
|
|
|
|
@forms ||= data.dig('forms').try(:keys) || []
|
|
|
|
end
|
|
|
|
|
|
|
|
# El nombre del formulario está disponible
|
|
|
|
#
|
|
|
|
# @param [String|Symbol] El nombre del formulario
|
|
|
|
def form?(name)
|
|
|
|
forms.include? name.to_s
|
|
|
|
end
|
|
|
|
|
2020-06-03 13:57:52 +00:00
|
|
|
# Obtiene un formulario con los campos definitivos
|
2020-05-30 19:43:25 +00:00
|
|
|
#
|
|
|
|
# @return Site::Forms::Form
|
|
|
|
def form(name)
|
|
|
|
@cached_forms ||= {}
|
2020-06-03 13:57:52 +00:00
|
|
|
@cached_forms[name] ||= Form[data.dig('forms', name).reject { |k,_| excluded? k }]
|
|
|
|
end
|
|
|
|
|
|
|
|
# Detecta si el campo está excluido del formulario final
|
|
|
|
def excluded?(field)
|
|
|
|
EXCLUDED_FIELDS.include? field.to_s
|
2020-05-30 19:43:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|