diff --git a/app/models/site.rb b/app/models/site.rb index 1b2e116..f8259dc 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 54e94c6..93aff94 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 a8bb31d..73c9ca4 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