From 0bbedd2e260fdd76d76ee09eb51a73e427ae7d67 Mon Sep 17 00:00:00 2001 From: f Date: Tue, 1 Oct 2019 16:41:33 -0300 Subject: [PATCH] =?UTF-8?q?vincular=20a=20versi=C3=B3n=20con=20www?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/sites_controller.rb | 12 ++-------- app/models/deploy_www.rb | 35 +++++++++++++++++++++++++++++ app/models/site.rb | 4 ++++ app/services/site_service.rb | 9 ++++++++ app/views/deploys/_deploy_www.haml | 17 ++++++++++++++ config/locales/en.yml | 14 ++++++++++++ config/locales/es.yml | 16 +++++++++++++ test/models/deploy_local_test.rb | 28 +++++++++++++---------- test/models/deploy_www_test.rb | 19 ++++++++++++++++ test/models/deploy_zip_test.rb | 28 ++++++++++------------- 10 files changed, 144 insertions(+), 38 deletions(-) create mode 100644 app/models/deploy_www.rb create mode 100644 app/views/deploys/_deploy_www.haml create mode 100644 test/models/deploy_www_test.rb diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 23b1530..c770e09 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -23,9 +23,7 @@ class SitesController < ApplicationController def new @site = Site.new authorize @site - - @site.deploys.build type: 'DeployLocal' - @site.deploys.build type: 'DeployZip' + SiteService.new(site: @site).build_deploys end def create @@ -41,14 +39,8 @@ class SitesController < ApplicationController def edit @site = find_site - - # TODO: esto está acá para ayudar en la migración de - if @site.deploys.empty? - @site.deploys.build(type: 'DeployLocal') - @site.deploys.build type: 'DeployZip' - end - authorize @site + SiteService.new(site: @site).build_deploys end def update diff --git a/app/models/deploy_www.rb b/app/models/deploy_www.rb new file mode 100644 index 0000000..818a22a --- /dev/null +++ b/app/models/deploy_www.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# Vincula la versión del sitio con www a la versión sin +class DeployWww < Deploy + store :values, accessors: %i[], coder: JSON + + before_destroy :remove_destination! + + def deploy + File.symlink?(destination) || + File.symlink(site.hostname, destination).zero? + end + + def limit + 1 + end + + def size + File.size destination + end + + def destination + File.join(Rails.root, '_deploy', fqdn) + end + + def fqdn + "www.#{site.hostname}" + end + + private + + def remove_destination! + File.rm_f destination + end +end diff --git a/app/models/site.rb b/app/models/site.rb index 4ed7aaf..bb95fdb 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -5,6 +5,10 @@ class Site < ApplicationRecord include FriendlyId + # TODO: Hacer que los diferentes tipos de deploy se auto registren + # @see app/services/site_service.rb + DEPLOYS = %i[local www zip].freeze + validates :name, uniqueness: true, hostname: true validates :design_id, presence: true validate :deploy_local_presence diff --git a/app/services/site_service.rb b/app/services/site_service.rb index 93aff94..9d6516a 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -35,6 +35,15 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do site end + def build_deploys + Site::DEPLOYS.map { |deploy| "Deploy#{deploy.capitalize}" } + .each do |deploy| + next if site.deploys.find_by type: deploy + + site.deploys.build type: deploy + end + end + private # Guarda los cambios de la configuración en el repositorio git diff --git a/app/views/deploys/_deploy_www.haml b/app/views/deploys/_deploy_www.haml new file mode 100644 index 0000000..2f91c0d --- /dev/null +++ b/app/views/deploys/_deploy_www.haml @@ -0,0 +1,17 @@ +-# Formulario para alojar una copia a www + +.row + .col + = deploy.hidden_field :id + = deploy.hidden_field :type + %h3 + -# + El checkbox invierte la lógica de destrucción porque queremos + crear el deploy si está activado y destruirlo si está + desactivado. + = deploy.check_box :_destroy, + { checked: deploy.object.persisted? }, + '0', '1' + = deploy.label :_destroy, t('.title') + = sanitize_markdown t('.help', fqdn: deploy.object.fqdn), + tags: %w[p strong em a] diff --git a/config/locales/en.yml b/config/locales/en.yml index 8b49078..ae00dcd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -51,6 +51,10 @@ en: title: Build the site success: Success! error: Error + deploy_www: + title: Link to www + success: Success! + error: Error deploy_zip: title: Build ZIP file success: Available for download @@ -209,6 +213,16 @@ en: We're working out the details for allowing your own site domains, you can help us! ejemplo: 'example' + deploy_www: + title: 'Add www to the address' + help: | + When you enable this option, your site will also be available + under . + + The www prefix to web addresses has been a way of refering to + computers that are available on the World Wide Web. But since + the Web has become the hegemonic way of accessing the Internet, + it has become less used. Even so, people still uses them. deploy_zip: title: 'Generate a ZIP file' help: | diff --git a/config/locales/es.yml b/config/locales/es.yml index a74202b..7fdeff7 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -53,6 +53,10 @@ es: title: Generar el sitio success: ¡Éxito! error: Hubo un error + deploy_www: + title: Vincular a versión con www + success: ¡Éxito! + error: Hubo un error deploy_zip: title: Generar archivo ZIP success: Disponible para descargar @@ -212,6 +216,18 @@ es: Estamos desarrollando la posibilidad de agregar tus propios dominios, ¡ayudanos! ejemplo: 'ejemplo' + deploy_www: + title: 'Agregar www a la dirección' + help: | + Cuando habilitas esta opción, tu sitio también estará disponible + como . + + El prefijo www para las direcciones web ha sido una forma de + referirse a las computadoras que están disponibles en la _World + Wide Web_ (WWW, Red de alcance mundial). Pero desde que la Web + se ha vuelto la forma hegemónica de acceder a Internet, el + prefijo se usa cada vez menos. Aun así, algunas personas lo + usan. deploy_zip: title: 'Generar un archivo ZIP' help: | diff --git a/test/models/deploy_local_test.rb b/test/models/deploy_local_test.rb index d5ffe51..7e8712d 100644 --- a/test/models/deploy_local_test.rb +++ b/test/models/deploy_local_test.rb @@ -1,20 +1,24 @@ # frozen_string_literal: true -require 'test_helper' - -class DeployZipTest < ActiveSupport::TestCase +class DeployLocalTest < ActiveSupport::TestCase test 'se puede deployear' do - deploy_local = create :deploy_local + site = create :site + local = create :deploy_local, site: site + deploy = create :deploy_zip, site: site - assert deploy_local.deploy - assert File.directory?(deploy_local.destination) - assert File.exist?(File.join(deploy_local.destination, 'index.html')) - assert_equal 3, deploy_local.build_stats.count + # Primero tenemos que generar el sitio + local.deploy - assert deploy_local.build_stats.map(&:bytes).compact.inject(:+).positive? - assert deploy_local.build_stats.map(&:seconds).compact.inject(:+).positive? + escaped_path = Shellwords.escape(deploy.path) - assert deploy_local.destroy - assert_not File.directory?(deploy_local.destination) + assert deploy.deploy + assert File.file?(deploy.path) + assert_equal 'application/zip', + `file --mime-type "#{escaped_path}"`.split(' ').last + assert_equal 1, deploy.build_stats.count + assert deploy.build_stats.map(&:bytes).inject(:+).positive? + assert deploy.build_stats.map(&:seconds).inject(:+).positive? + + local.destroy end end diff --git a/test/models/deploy_www_test.rb b/test/models/deploy_www_test.rb new file mode 100644 index 0000000..8ae00eb --- /dev/null +++ b/test/models/deploy_www_test.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +require 'test_helper' + +class DeployWwwTest < ActiveSupport::TestCase + test 'se puede deployear' do + site = create :site + local = create :deploy_local, site: site + deploy = create :deploy_www, site: site + + # Primero tenemos que generar el sitio + local.deploy + + assert deploy.deploy + assert File.symlink?(deploy.destination) + + local.destroy + end +end diff --git a/test/models/deploy_zip_test.rb b/test/models/deploy_zip_test.rb index 7e8712d..d5ffe51 100644 --- a/test/models/deploy_zip_test.rb +++ b/test/models/deploy_zip_test.rb @@ -1,24 +1,20 @@ # frozen_string_literal: true -class DeployLocalTest < ActiveSupport::TestCase +require 'test_helper' + +class DeployZipTest < ActiveSupport::TestCase test 'se puede deployear' do - site = create :site - local = create :deploy_local, site: site - deploy = create :deploy_zip, site: site + deploy_local = create :deploy_local - # Primero tenemos que generar el sitio - local.deploy + assert deploy_local.deploy + assert File.directory?(deploy_local.destination) + assert File.exist?(File.join(deploy_local.destination, 'index.html')) + assert_equal 3, deploy_local.build_stats.count - escaped_path = Shellwords.escape(deploy.path) + assert deploy_local.build_stats.map(&:bytes).compact.inject(:+).positive? + assert deploy_local.build_stats.map(&:seconds).compact.inject(:+).positive? - assert deploy.deploy - assert File.file?(deploy.path) - assert_equal 'application/zip', - `file --mime-type "#{escaped_path}"`.split(' ').last - assert_equal 1, deploy.build_stats.count - assert deploy.build_stats.map(&:bytes).inject(:+).positive? - assert deploy.build_stats.map(&:seconds).inject(:+).positive? - - local.destroy + assert deploy_local.destroy + assert_not File.directory?(deploy_local.destination) end end