se pueden cambiar las licencias

de paso nos aseguramos que cuando el sitio cambie de nombre se actualice
la configuración de jekyll, lo que producía bugs sutiles donde el sitio
leía correctamente pero en una dirección errónea
This commit is contained in:
f 2019-09-17 20:26:59 -03:00
parent 86841f3388
commit 3a4f3918cf
No known key found for this signature in database
GPG key ID: 2AE5A13E321F953D
3 changed files with 100 additions and 17 deletions

View file

@ -160,6 +160,7 @@ class Site < ApplicationRecord
#
# @param lang: [String|Symbol] traer los artículos de este idioma
def posts(lang: nil)
read
@posts ||= {}
lang ||= I18n.locale
@ -268,11 +269,20 @@ class Site < ApplicationRecord
def load_jekyll
return unless name.present? && File.directory?(path)
reload_jekyll!
end
def reload_jekyll!
Dir.chdir(path) do
@jekyll = Jekyll::Site.new(jekyll_config)
end
end
def reload
super
reload_jekyll!
end
def jekyll_config
# Pasamos destination porque configuration() toma el directorio
# actual
@ -331,6 +341,7 @@ class Site < ApplicationRecord
return unless name_changed?
FileUtils.mv path_was, path
reload_jekyll!
end
# Sincroniza algunos atributos del sitio con su configuración y

View file

@ -30,6 +30,8 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
commit_config(action: :update)
end
change_licencias
site
end
@ -50,25 +52,54 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
end
# Crea la licencia del sitio para cada locale disponible
# rubocop:disable Metrics/MethodLength
def add_licencias
I18n.available_locales.each do |locale|
Mobility.with_locale(locale) do
params = ActionController::Parameters.new(
post: {
lang: locale,
title: site.licencia.name,
author: %w[Sutty],
permalink: "#{I18n.t('activerecord.models.licencia').downcase}/",
content: CommonMarker.render_html(site.licencia.deed)
}
)
PostService.new(site: site, usuarie: usuarie, params: params)
.create
add_licencia lang: locale
end
end
end
# rubocop:enable Metrics/MethodLength
def add_licencia(lang:)
params = ActionController::Parameters.new(
post: {
lang: lang,
title: site.licencia.name,
author: %w[Sutty],
permalink: "#{I18n.t('activerecord.models.licencia').downcase}/",
content: CommonMarker.render_html(site.licencia.deed)
}
)
PostService.new(site: site, usuarie: usuarie, params: params).create
end
# Encuentra la licencia a partir de su enlace permanente y le cambia
# el contenido
#
# TODO: Crear un layout específico para licencias así es más certera
# la búsqueda.
def change_licencias
I18n.available_locales.each do |locale|
Mobility.with_locale(locale) do
permalink = "#{I18n.t('activerecord.models.licencia').downcase}/"
post = site.posts(lang: locale).find_by(permalink: permalink)
post ? change_licencia(post: post) : add_licencia(lang: locale)
end
end
end
def change_licencia(post:)
params = ActionController::Parameters.new(
post: {
title: site.licencia.name,
content: CommonMarker.render_html(site.licencia.deed)
}
)
PostService.new(site: site, usuarie: usuarie, post: post,
params: params).update
end
end
# rubocop:enable Metrics/BlockLength

View file

@ -112,7 +112,7 @@ class SitesControllerTest < ActionDispatch::IntegrationTest
title: name,
description: name * 2,
design_id: design.id,
licencia_id: create(:licencia).id,
licencia_id: Licencia.all.second.id,
deploys_attributes: {
'0' => {
type: 'DeployLocal'
@ -130,7 +130,48 @@ class SitesControllerTest < ActionDispatch::IntegrationTest
assert_equal design.gem, @site.config.theme
assert_equal name, @site.config.title
assert_equal name * 2, @site.config.description
assert_equal I18n.t('site_service.update', name: name),
@site.repository.rugged.head.target.message
# assert_equal I18n.t('site_service.update', name: name),
# @site.repository.rugged.head.target.message
I18n.available_locales.each do |locale|
Mobility.with_locale(locale) do
assert @site.posts(lang: locale).find_by(title: @site.licencia.name)
end
end
end
test 'se pueden cambiar las licencias' do
name = SecureRandom.hex
post sites_url, headers: @authorization, params: {
site: {
name: name,
title: name,
description: name * 2,
design_id: create(:design).id,
licencia_id: Licencia.first.id,
deploys_attributes: {
'0' => {
type: 'DeployLocal'
}
}
}
}
site = Site.find_by_name(name)
put site_url(site), headers: @authorization, params: {
site: { licencia_id: Licencia.all.second.id }
}
site.reload
I18n.available_locales.each do |locale|
Mobility.with_locale(locale) do
assert site.posts(lang: locale).find_by(title: site.licencia.name)
end
end
site.destroy
end
end