mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-29 16:56:22 +00:00
Extraer el compartamiento de plantillas a su propio módulo
This commit is contained in:
parent
a634a66ca7
commit
a306dcaa24
1 changed files with 1 additions and 52 deletions
|
@ -7,6 +7,7 @@ class Site < ApplicationRecord
|
||||||
include Site::Forms
|
include Site::Forms
|
||||||
include Site::FindAndReplace
|
include Site::FindAndReplace
|
||||||
include Site::Api
|
include Site::Api
|
||||||
|
include Site::Themes
|
||||||
include Tienda
|
include Tienda
|
||||||
|
|
||||||
# Cifrar la llave privada que cifra y decifra campos ocultos. Sutty
|
# Cifrar la llave privada que cifra y decifra campos ocultos. Sutty
|
||||||
|
@ -23,18 +24,13 @@ class Site < ApplicationRecord
|
||||||
allow_root_label: true
|
allow_root_label: true
|
||||||
}
|
}
|
||||||
|
|
||||||
validates :design_id, presence: true
|
|
||||||
validates_inclusion_of :status, in: %w[waiting enqueued building]
|
validates_inclusion_of :status, in: %w[waiting enqueued building]
|
||||||
validates_presence_of :title
|
validates_presence_of :title
|
||||||
validates :description, length: { in: 50..160 }
|
validates :description, length: { in: 50..160 }
|
||||||
validate :deploy_local_presence
|
validate :deploy_local_presence
|
||||||
validate :compatible_layouts, on: :update
|
|
||||||
|
|
||||||
attr_reader :incompatible_layouts
|
|
||||||
|
|
||||||
friendly_id :name, use: %i[finders]
|
friendly_id :name, use: %i[finders]
|
||||||
|
|
||||||
belongs_to :design
|
|
||||||
belongs_to :licencia
|
belongs_to :licencia
|
||||||
|
|
||||||
has_many :log_entries, dependent: :destroy
|
has_many :log_entries, dependent: :destroy
|
||||||
|
@ -49,8 +45,6 @@ class Site < ApplicationRecord
|
||||||
# Mantenemos el nombre que les da Jekyll
|
# Mantenemos el nombre que les da Jekyll
|
||||||
has_many_attached :static_files
|
has_many_attached :static_files
|
||||||
|
|
||||||
# Clonar el directorio de esqueleto antes de crear el sitio
|
|
||||||
before_create :clone_skel!
|
|
||||||
# Elimina el directorio al destruir un sitio
|
# Elimina el directorio al destruir un sitio
|
||||||
before_destroy :remove_directories!
|
before_destroy :remove_directories!
|
||||||
# Carga el sitio Jekyll una vez que se inicializa el modelo o después
|
# Carga el sitio Jekyll una vez que se inicializa el modelo o después
|
||||||
|
@ -425,14 +419,6 @@ class Site < ApplicationRecord
|
||||||
self.private_key ||= Lockbox.generate_key
|
self.private_key ||= Lockbox.generate_key
|
||||||
end
|
end
|
||||||
|
|
||||||
# Clona el esqueleto de Sutty para crear el sitio nuevo, no pasa nada
|
|
||||||
# si el sitio ya existe
|
|
||||||
def clone_skel!
|
|
||||||
return if File.directory? path
|
|
||||||
|
|
||||||
Rugged::Repository.clone_at ENV['SKEL_SUTTY'], path
|
|
||||||
end
|
|
||||||
|
|
||||||
# Elimina el directorio del sitio
|
# Elimina el directorio del sitio
|
||||||
def remove_directories!
|
def remove_directories!
|
||||||
FileUtils.rm_rf path
|
FileUtils.rm_rf path
|
||||||
|
@ -475,41 +461,4 @@ class Site < ApplicationRecord
|
||||||
|
|
||||||
errors.add(:deploys, I18n.t('activerecord.errors.models.site.attributes.deploys.deploy_local_presence'))
|
errors.add(:deploys, I18n.t('activerecord.errors.models.site.attributes.deploys.deploy_local_presence'))
|
||||||
end
|
end
|
||||||
|
|
||||||
# Valida que al cambiar de plantilla no tengamos artículos en layouts
|
|
||||||
# inexistentes.
|
|
||||||
def compatible_layouts
|
|
||||||
return unless design_id_changed?
|
|
||||||
|
|
||||||
new_configuration = configuration.dup
|
|
||||||
|
|
||||||
if design.no_theme?
|
|
||||||
new_configuration.delete 'theme'
|
|
||||||
else
|
|
||||||
new_configuration['theme'] = design.gem
|
|
||||||
end
|
|
||||||
|
|
||||||
new_site = Jekyll::Site.new(new_configuration)
|
|
||||||
new_site.read
|
|
||||||
|
|
||||||
# No vale la pena seguir procesando si no hay artículos!
|
|
||||||
return if new_site.documents.empty?
|
|
||||||
|
|
||||||
new_site.documents.map(&:read!)
|
|
||||||
|
|
||||||
old_layouts = new_site.documents.map do |doc|
|
|
||||||
doc.data['layout']
|
|
||||||
end.uniq.compact
|
|
||||||
|
|
||||||
@incompatible_layouts = old_layouts - new_site.layouts.keys
|
|
||||||
|
|
||||||
return if @incompatible_layouts.empty?
|
|
||||||
|
|
||||||
@incompatible_layouts.map! do |layout|
|
|
||||||
i18n.dig('layouts', layout) || layout
|
|
||||||
end
|
|
||||||
|
|
||||||
errors.add(:design_id,
|
|
||||||
I18n.t('activerecord.errors.models.site.attributes.design_id.layout_incompatible.error'))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue