From 7a601a7b31e984476c74ce34bd9ecb98e05c744e Mon Sep 17 00:00:00 2001 From: f Date: Mon, 30 Apr 2018 15:51:39 -0300 Subject: [PATCH] =?UTF-8?q?reordenar=20art=C3=ADculos!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/login_controller.rb | 2 +- app/controllers/sites_controller.rb | 14 ++++++++++++++ app/models/site.rb | 25 ++++++++++++++++++++----- app/views/layouts/_flash.haml | 7 +++++++ app/views/login/new.haml | 4 +--- app/views/posts/index.haml | 1 + config/locales/en.yml | 5 +++++ config/locales/es.yml | 5 +++++ test/models/site_test.rb | 15 +++++++++++++++ 9 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 app/views/layouts/_flash.haml diff --git a/app/controllers/login_controller.rb b/app/controllers/login_controller.rb index a0ce18b8..91def510 100644 --- a/app/controllers/login_controller.rb +++ b/app/controllers/login_controller.rb @@ -18,7 +18,7 @@ class LoginController < ApplicationController # TODO enviar a la URL de donde vinimos redirect_to sites_path else - flash[:error] = t('login.error') + flash[:danger] = t('login.error') render 'login/new' end end diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index ee32154a..238e48a2 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -25,8 +25,22 @@ class SitesController < ApplicationController def build_log @site = find_site + # TODO eliminar ANSI render file: @site.build_log, layout: false, content_type: 'text/plain; charset=utf-8' end + + def reorder_posts + @site = find_site + lang = params.require(:posts).require(:lang) + + if @site.reorder_collection(lang, params.require(:posts).require(:order)) + flash[:info] = I18n.t('info.posts.reorder') + else + flash[:danger] = I18n.t('errors.posts.reorder') + end + + redirect_to site_posts_path @site + end end diff --git a/app/models/site.rb b/app/models/site.rb index 67f0f41e..858ef73d 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -196,16 +196,31 @@ class Site end.all? end - # Reordena la colección usando la posición actual de los artículos y - # guarda los cambios - def reorder_collection!(collection = 'posts') - posts_for(collection).each_with_index do |p, i| - p.update_attributes order: i + # Reordena la colección usando la posición informada + # + # new_order es un hash cuya key es la posición actual del post y el + # valor la posición nueva + def reorder_collection(collection = 'posts', new_order) + posts_to_order = posts_for collection + # No pasa nada si no estamos pasando la misma cantidad de artículos + return if new_order.keys.count != posts_to_order.count + # Y si no estamos pasando el mismo orden + return if new_order.values.map(&:to_i).sort != new_order.keys.map(&:to_i).sort + + # Recorre todos los posts y asigna el nuevo orden + posts_to_order.each_with_index do |p, i| + p.update_attributes order: new_order[i.to_s].to_i p.save end posts_for(collection).map(&:ordered?).all? end + + # Reordena la colección usando la posición actual de los artículos + def reorder_collection!(collection = 'posts') + order = Hash[posts_for(collection).count.times.map { |i| [i.to_s,i.to_s] }] + reorder_collection collection, order + end alias :reorder_posts! :reorder_collection! # El directorio donde se almacenan los sitios diff --git a/app/views/layouts/_flash.haml b/app/views/layouts/_flash.haml new file mode 100644 index 00000000..61e11cc5 --- /dev/null +++ b/app/views/layouts/_flash.haml @@ -0,0 +1,7 @@ +- flash.each do |type, message| + .alert.alert-dismissible.fade.show{role: 'alert', class: "alert-#{type.to_s}"} + = message + %button.close{type: 'button', + data: { dismiss: 'alert' }, + 'aria-label': t('help.close') } + %span{'aria-hidden': true} × diff --git a/app/views/login/new.haml b/app/views/login/new.haml index c16b77bc..d6b4fd97 100644 --- a/app/views/login/new.haml +++ b/app/views/login/new.haml @@ -1,8 +1,6 @@ .row.align-items-center.justify-content-center.full-height .col-md-6.align-self-center - - if flash[:error] - .alert.alert-danger{role: 'alert'} - = flash[:error] + = render 'layouts/flash' = form_tag login_path do .form-group diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index fc405e10..c9e6ec2a 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -18,6 +18,7 @@ .row .col + = render 'layouts/flash' - if @posts.present? = form_tag site_reorder_posts_path, method: :post do = hidden_field 'posts', 'lang', value: @lang diff --git a/config/locales/en.yml b/config/locales/en.yml index ca22cba3..82398bc7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -2,6 +2,11 @@ en: errors: argument_error: 'Argument `%{argument}` must be an instance of %{class}' unknown_locale: 'Unknown %{locale} locale' + posts: + reorder: "We're sorry, we couldn't reorder the articles" + info: + posts: + reorder: "The articles have been reordered!" help: category: 'Category' logout: 'Close the session' diff --git a/config/locales/es.yml b/config/locales/es.yml index 1a3ac06d..9d47c5a2 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -2,6 +2,11 @@ es: errors: argument_error: 'El argumento `%{argument}` debe ser una instancia de %{class}' unknown_locale: 'El idioma %{locale} es desconocido' + posts: + reorder: "Lo sentimos, no pudimos reordenar los artículos." + info: + posts: + reorder: "¡Los artículos fueron reordenados!" help: category: 'Categoría' logout: 'Cierra la sesión' diff --git a/test/models/site_test.rb b/test/models/site_test.rb index f640e8b2..e42ebd30 100644 --- a/test/models/site_test.rb +++ b/test/models/site_test.rb @@ -24,4 +24,19 @@ class SiteTest < ActiveSupport::TestCase assert @site.reorder_collection! assert @site.ordered? end + + test 'No podemos poner órdenes arbitrarios' do + total = @site.posts.count + new_order = Hash[total.times.map { |i| [i.to_s,rand(total)] }] + + assert_not @site.reorder_collection('posts', new_order) + end + + test 'Si les damos un orden alternativo los reordenamos' do + total = @site.posts.count + order = total.times.map { |i| i.to_s } + new_order = Hash[order.zip(order.shuffle)] + + assert @site.reorder_collection('posts', new_order) + end end