diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 3f280dd..42fbaba 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -38,6 +38,22 @@ class SitesController < ApplicationController end end + def edit + @site = find_site + authorize @site + end + + def update + @site = find_site + authorize @site + + if @site.update(site_params) + redirect_to sites_path + else + render 'edit' + end + end + # Envía un archivo del directorio público de Jekyll def send_public_file authorize Site diff --git a/app/models/site.rb b/app/models/site.rb index 00d1483..ac449f9 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -23,6 +23,8 @@ class Site < ApplicationRecord # de crearlo after_initialize :load_jekyll! after_create :load_jekyll! + # Cambiar el nombre del directorio + before_update :update_name! attr_accessor :jekyll, :collections @@ -38,7 +40,11 @@ class Site < ApplicationRecord # # Equivale a _sites + nombre def path - @path ||= File.join(Site.site_path, name) + File.join(Site.site_path, name) + end + + def old_path + File.join(Site.site_path, name_was) end # Este sitio acepta invitadxs? @@ -370,4 +376,10 @@ class Site < ApplicationRecord def remove_directories! FileUtils.rm_rf path end + + def update_name! + return unless name_changed? + + FileUtils.mv old_path, path + end end diff --git a/app/views/sites/_form.haml b/app/views/sites/_form.haml new file mode 100644 index 0000000..1f5650b --- /dev/null +++ b/app/views/sites/_form.haml @@ -0,0 +1,6 @@ += form_for @site do |f| + .form-group + = f.label :name + = f.text_field :name, class: 'form-control' + .form-group + = f.submit submit, class: 'btn btn-success' diff --git a/app/views/sites/edit.haml b/app/views/sites/edit.haml new file mode 100644 index 0000000..a7e96a1 --- /dev/null +++ b/app/views/sites/edit.haml @@ -0,0 +1,10 @@ +.row + .col + = render 'layouts/breadcrumb', + crumbs: [link_to(t('sites.index'), sites_path), + t('.title', site: @site.name)] +.row + .col + %h1= t('.title', site: @site.name) + + = render 'form', submit: t('.submit') diff --git a/app/views/sites/new.haml b/app/views/sites/new.haml index 2e28dcd..0b883c1 100644 --- a/app/views/sites/new.haml +++ b/app/views/sites/new.haml @@ -1,14 +1,9 @@ .row .col = render 'layouts/breadcrumb', - crumbs: [ link_to(t('sites.index'), sites_path), t('.title') ] + crumbs: [link_to(t('sites.index'), sites_path), t('.title')] .row .col %h1= t('.title') - = form_for @site do |f| - .form-group - = f.label :name - = f.text_field :name, class: 'form-control' - .form-group - = f.submit t('.submit'), class: 'btn btn-success' + = render 'form', submit: t('.submit') diff --git a/config/locales/en.yml b/config/locales/en.yml index 3e0ecc2..64bde6b 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -155,6 +155,9 @@ en: new: title: 'Create site' submit: 'Create site' + edit: + title: 'Edit %{site}' + submit: 'Save changes' footer: powered_by: 'is developed by' templates: diff --git a/config/locales/es.yml b/config/locales/es.yml index 9608618..b0fa8ef 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -160,6 +160,9 @@ es: new: title: 'Crear un sitio' submit: 'Crear sitio' + edit: + title: 'Editar %{site}' + submit: 'Guardar cambios' footer: powered_by: 'es desarrollada por' i18n: diff --git a/test/models/site_test.rb b/test/models/site_test.rb index 8d73a2b..2040bd9 100644 --- a/test/models/site_test.rb +++ b/test/models/site_test.rb @@ -61,4 +61,15 @@ class SiteTest < ActiveSupport::TestCase assert site.valid? assert !site.posts.empty? end + + test 'se pueden renombrar' do + @site = create :site + path = @site.path + + @site.update_attribute :name, SecureRandom.hex + + assert_not_equal path, @site.path + assert File.directory?(@site.path) + assert_not File.directory?(path) + end end