diff --git a/Gemfile b/Gemfile index 47504f8f..66baec53 100644 --- a/Gemfile +++ b/Gemfile @@ -79,6 +79,7 @@ gem 'webpacker' gem 'yaml_db', git: 'https://0xacab.org/sutty/yaml_db.git' gem 'kaminari' gem 'device_detector' +gem 'dry-schema' gem 'htmlbeautifier' gem 'rubanok' diff --git a/Gemfile.lock b/Gemfile.lock index e20d74be..32547ac7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -646,6 +646,7 @@ DEPENDENCIES distributed-press-api-client (~> 0.4.1) dotenv-rails down + dry-schema ed25519 email_address! exception_notification diff --git a/app/models/site.rb b/app/models/site.rb index 920cd51e..3b145de6 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 90dbae18..178fd0b7 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -31,6 +31,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do add_role_to_deploys! role site.save && + site.update_options_from_theme && site.config.write && commit_config(action: :create) && site.reset.nil? &&