From 263dce14358b2a71037f15f7a85e0f859c997bc0 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 26 Mar 2024 15:29:58 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20poder=20traer=20algunas=20opciones=20de?= =?UTF-8?q?=20configuraci=C3=B3n=20desde=20la=20plantilla=20#174?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Gemfile | 1 + Gemfile.lock | 1 + app/models/site.rb | 1 + app/models/site/default_options.rb | 44 ++++++++++++++++++++++++++++++ app/services/site_service.rb | 1 + 5 files changed, 48 insertions(+) create mode 100644 app/models/site/default_options.rb diff --git a/Gemfile b/Gemfile index 466ec079..c9f735a3 100644 --- a/Gemfile +++ b/Gemfile @@ -76,6 +76,7 @@ gem 'webpacker' gem 'yaml_db', git: 'https://0xacab.org/sutty/yaml_db.git' gem 'kaminari' gem 'device_detector' +gem 'dry-schema' # database gem 'hairtrigger' diff --git a/Gemfile.lock b/Gemfile.lock index 78563c84..7b4ac1ba 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -603,6 +603,7 @@ DEPENDENCIES distributed-press-api-client (~> 0.3.0rc0) dotenv-rails down + dry-schema ed25519 email_address! exception_notification diff --git a/app/models/site.rb b/app/models/site.rb index 7b93184f..14e05e5d 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -11,6 +11,7 @@ class Site < ApplicationRecord include Site::BuildStats include Site::LayoutOrdering include Site::SocialDistributedPress + include Site::DefaultOptions include Tienda # Cifrar la llave privada que cifra y decifra campos ocultos. Sutty diff --git a/app/models/site/default_options.rb b/app/models/site/default_options.rb new file mode 100644 index 00000000..3e392782 --- /dev/null +++ b/app/models/site/default_options.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require 'dry-schema' + +class Site + # Las opciones por defecto se aplican durante la creación del sitio y + # luego se permite a les usuaries modificarlas según quieran. Por el + # momento las opciones nuevas que aparezcan no modifican un sitio que + # ya existe. + module DefaultOptions + extend ActiveSupport::Concern + + Schema = Dry::Schema.Params do + optional(:colaboracion_anonima).value(:bool) + optional(:contact).value(:bool) + optional(:acepta_invitades).value(:bool) + optional(:slugify_mode).value(included_in?: Jekyll::Utils::SLUGIFY_MODES) + optional(:pagination).value(:bool) + end + + included do + validate :validate_options_from_theme!, if: :persisted? + + # @return [Dry::Schema::Result] + def options_from_theme + @options_from_theme ||= Schema.call(data['sutty']) + end + + def update_options_from_theme + return true if options_from_theme.to_h.blank? + + update(**options_from_theme.to_h) + end + + private + + def validate_options_from_theme! + options_from_theme.errors.each do |error| + errors.add(:default_options, "#{error.path.map(&:to_s).join('/')} #{error} (#{error.input})") + end + end + end + end +end diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 5c37cfe3..8c3ea6b6 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -27,6 +27,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do site.locales = [usuarie.lang] + I18n.available_locales site.save && + site.update_options_from_theme && site.config.write && commit_config(action: :create) && site.reset.nil? &&