From 3a4f3918cf99a19f150674e52f4720d0e0626cd0 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 17 Sep 2019 20:26:59 -0300 Subject: [PATCH] se pueden cambiar las licencias MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- app/models/site.rb | 11 +++++ app/services/site_service.rb | 59 +++++++++++++++++------ test/controllers/sites_controller_test.rb | 47 ++++++++++++++++-- 3 files changed, 100 insertions(+), 17 deletions(-) diff --git a/app/models/site.rb b/app/models/site.rb index 1b2e1169..f8259dcf 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -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 diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 54e94c6a..93aff947 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -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 diff --git a/test/controllers/sites_controller_test.rb b/test/controllers/sites_controller_test.rb index a8bb31db..73c9ca4c 100644 --- a/test/controllers/sites_controller_test.rb +++ b/test/controllers/sites_controller_test.rb @@ -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