diff --git a/Gemfile b/Gemfile index e5e6ed10..5714b981 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 'rubanok' gem 'after_commit_everywhere', '~> 1.0' diff --git a/Gemfile.lock b/Gemfile.lock index 7087e013..7f5284ef 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -644,6 +644,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 8c02f91b..d47a8e50 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 self.filter_attributes += [/_key/, /_ciphertext\z/] 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 1a85d401..36868c51 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -29,6 +29,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? &&