From 35b79ed9b463cdf555be626c49895e795b39509f Mon Sep 17 00:00:00 2001 From: f Date: Mon, 17 Apr 2023 18:30:21 -0300 Subject: [PATCH] feat: organizar layouts #13072 --- app/models/layout.rb | 7 ++++++ app/models/site.rb | 1 + app/models/site/layout_ordering.rb | 38 ++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) create mode 100644 app/models/site/layout_ordering.rb diff --git a/app/models/layout.rb b/app/models/layout.rb index c70829fa..efca66ee 100644 --- a/app/models/layout.rb +++ b/app/models/layout.rb @@ -9,6 +9,13 @@ Layout = Struct.new(:site, :name, :meta, :metadata, keyword_init: true) do name.to_s end + # Obtiene todos los layouts (schemas) dependientes de este. + # + # @return [Array] + def schemas + @schemas ||= site.layouts.to_h.slice(*site.schema_organization[name]).values + end + def attributes @attributes ||= metadata.keys.map(&:to_sym) end diff --git a/app/models/site.rb b/app/models/site.rb index 3f2aa34e..521f298d 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -9,6 +9,7 @@ class Site < ApplicationRecord include Site::Api include Site::DeployDependencies include Site::BuildStats + include Site::LayoutOrdering include Tienda # Cifrar la llave privada que cifra y decifra campos ocultos. Sutty diff --git a/app/models/site/layout_ordering.rb b/app/models/site/layout_ordering.rb new file mode 100644 index 00000000..9fecbf21 --- /dev/null +++ b/app/models/site/layout_ordering.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +class Site + # Obtiene un listado de layouts (schemas) + module LayoutOrdering + extend ActiveSupport::Concern + + included do + + # Obtiene o genera un listado de layouts (schemas) con sus + # dependencias, para poder generar un árbol. + # + # Por defecto, si el sitio no lo soporta, se obtienen los layouts + # ordenados alfabéticamente por traducción. + # + # @return [Hash] + def schema_organization + @schema_organization ||= + begin + schema_organization = data.dig('schema', 'organization') + schema_organization&.symbolize_keys! + schema_organization&.transform_values! do |ary| + ary.map(&:to_sym) + end + + schema_organization || + begin + layouts = self.layouts.sort_by(&:humanized_name).map(&:name) + Hash[layouts.zip([].fill([], 0, layouts.size))] + end + end + end + + # TODO: Deprecar cuando renombremos layouts a schemas + alias layout_organization schema_organization + end + end +end