From 4379e496293324a47196cb3bc7bb1870fe5f744c Mon Sep 17 00:00:00 2001 From: f Date: Wed, 2 May 2018 14:23:45 -0300 Subject: [PATCH] =?UTF-8?q?tener=20una=20ruta=20de=20migraci=C3=B3n=20haci?= =?UTF-8?q?a=20poder=20ordenar=20posts?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ademas, no depender de la posición en el orden por fechas, salvo al principio que es lo unico que tenemos --- app/assets/javascripts/drag.js | 6 +++-- app/controllers/sites_controller.rb | 8 +++++- app/models/site.rb | 19 ++++++++----- app/views/posts/index.haml | 41 ++++++++++++++++++----------- config/locales/en.yml | 2 ++ config/locales/es.yml | 2 ++ test/models/site_test.rb | 24 +++++++++++++++++ 7 files changed, 78 insertions(+), 24 deletions(-) diff --git a/app/assets/javascripts/drag.js b/app/assets/javascripts/drag.js index e97ec642..0ebd534a 100644 --- a/app/assets/javascripts/drag.js +++ b/app/assets/javascripts/drag.js @@ -1,13 +1,15 @@ $(document).on('turbolinks:load', function() { + var were = $('.order.was').map(function() { return $(this).data('order') }); + tableDragger(document.querySelector('.table-draggable'), { mode: 'row', onlyBody: true, dragHandler: '.handle' }).on('drop', function(from, to, el, mode) { // Al soltar, reordenamos toda la tabla - $('.post_order').val(function(i,v) { return i; }); + $('.post_order').val(function(i,v) { return were[i]; }); // Mostramos el nuevo orden también - $('.order.is').text(function(i,v) { return i; }); + $('.order.is').text(function(i,v) { console.log(i, were[i]); return were[i]; }); // Solo mostramos el valor anterior si no coincide con el valor // actual. Como lo mostramos entre comillas, usamos el atributo // data-order del diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 7ff72acf..ab85343a 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -35,7 +35,13 @@ class SitesController < ApplicationController @site = find_site lang = params.require(:posts).require(:lang) - if @site.reorder_collection(lang, params.require(:posts).require(:order)) + if params[:posts][:force].present? + result = @site.reorder_collection! lang + else + result = @site.reorder_collection(lang, params.require(:posts).require(:order)) + end + + if result flash[:info] = I18n.t('info.posts.reorder') else flash[:danger] = I18n.t('errors.posts.reorder') diff --git a/app/models/site.rb b/app/models/site.rb index 858ef73d..9273ad85 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -201,19 +201,26 @@ class Site # 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 + # Tenemos que pasar el mismo orden return if new_order.values.map(&:to_i).sort != new_order.keys.map(&:to_i).sort + # Solo traer los posts que vamos a modificar + posts_to_order = posts_for(collection).values_at(*new_order.keys.map(&:to_i)) + # 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 + # Usar el index si el artículo no estaba ordenado, para tener una + # ruta de adopción + oo = (p.order || i).to_s + no = new_order[oo].to_i + # No modificar nada si no hace falta + next if p.order == no + + p.update_attributes order: no p.save end - posts_for(collection).map(&:ordered?).all? + posts_to_order.map(&:ordered?).all? end # Reordena la colección usando la posición actual de los artículos diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index 8df63d2d..84e32a8d 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -32,13 +32,24 @@ sort_by: s) = form_tag site_reorder_posts_path, method: :post do = hidden_field 'posts', 'lang', value: @lang - .reorder-posts-panel.alert.alert-info.alert-dismissible.fade.show{role: 'alert'} - = raw t('help.posts.reorder') - %br - = submit_tag t('posts.reorder_posts'), class: 'btn btn-success' - %button.close{type: 'button', - 'aria-label': t('help.close') } - %span{'aria-hidden': true} × + - if @site.ordered? @lang + .reorder-posts-panel.alert.alert-info.alert-dismissible.fade.show{role: 'alert'} + = raw t('help.posts.reorder') + %br + = submit_tag t('posts.reorder_posts'), class: 'btn btn-success' + %button.close{type: 'button', + 'aria-label': t('help.close') } + %span{'aria-hidden': true} × + - else + .alert.alert-danger.alert-dismissible.fade.show{role: 'alert'} + = raw t('errors.posts.disordered') + %br + = hidden_field 'posts', 'force', value: true + = submit_tag t('errors.posts.disordered_button'), class: 'btn btn-danger' + %button.close{type: 'button', + data: { dismiss: 'alert' }, + 'aria-label': t('help.close') } + %span{'aria-hidden': true} × %table.table.table-condensed.table-striped.table-draggable %tbody - @posts.each_with_index do |post, i| @@ -48,15 +59,15 @@ - next unless post.categories.include?(@category) -# establecer la direccion del texto - direction = post.get_front_matter(:dir) - - order = post.order || i %tr - %td - = fa_icon 'arrows-v', class: 'handle' - = hidden_field 'posts[order]', i, value: order, class: 'post_order' - %small - %br - %span.order.is= order - %span.order.was.d-none{data: { order: order }}= "(#{order})" + - if @site.ordered? @lang + %td + = fa_icon 'arrows-v', class: 'handle' + = hidden_field 'posts[order]', i, value: post.order, class: 'post_order' + %small + %br + %span.order.is= post.order + %span.order.was.d-none{data: { order: post.order }}= "(#{post.order})" %td{class: direction} = link_to post.title, site_post_path(@site, post, lang: @lang) diff --git a/config/locales/en.yml b/config/locales/en.yml index b7196356..4a6e6de8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -4,6 +4,8 @@ en: unknown_locale: 'Unknown %{locale} locale' posts: reorder: "We're sorry, we couldn't reorder the articles" + disordered: "The posts are disordered, this will prevent you from reordering them!" + disordered_button: 'Reorder!' info: posts: reorder: "The articles have been reordered!" diff --git a/config/locales/es.yml b/config/locales/es.yml index d5743cc1..3b815105 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -4,6 +4,8 @@ es: unknown_locale: 'El idioma %{locale} es desconocido' posts: reorder: "Lo sentimos, no pudimos reordenar los artículos." + disordered: 'Los artículos no tienen número de orden, esto impedirá que los puedas reordenar' + disordered_button: '¡Reordenar!' info: posts: reorder: "¡Los artículos fueron reordenados!" diff --git a/test/models/site_test.rb b/test/models/site_test.rb index e42ebd30..49155d27 100644 --- a/test/models/site_test.rb +++ b/test/models/site_test.rb @@ -38,5 +38,29 @@ class SiteTest < ActiveSupport::TestCase new_order = Hash[order.zip(order.shuffle)] assert @site.reorder_collection('posts', new_order) + + # podemos hacer este test porque reordenar los posts no ordena el + # array + new_order.each do |k,v| + v = v.to_i + k = k.to_i + assert_equal v, @site.posts[k].order + end + end + + test 'Podemos reordenar solo una parte de los artículos' do + total = @site.posts.count + order = (total - rand(total - 1)).times.map { |i| i.to_s } + new_order = Hash[order.zip(order.shuffle)] + + assert @site.reorder_collection('posts', new_order) + + # podemos hacer este test porque reordenar los posts no ordena el + # array + new_order.each do |k,v| + v = v.to_i + k = k.to_i + assert_equal v, @site.posts[k].order + end end end