From 443807b772dce4f527583063cf4a5a2d42d48e19 Mon Sep 17 00:00:00 2001 From: f Date: Mon, 30 Apr 2018 17:50:29 -0300 Subject: [PATCH] ordenar por fecha o numero de orden --- app/assets/javascripts/drag.js | 21 ++++++++++++++++++--- app/controllers/posts_controller.rb | 6 ++++++ app/controllers/sites_controller.rb | 1 + app/views/posts/index.haml | 17 ++++++++++++++++- config/locales/en.yml | 5 +++++ config/locales/es.yml | 5 +++++ 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/app/assets/javascripts/drag.js b/app/assets/javascripts/drag.js index 0f604636..e97ec642 100644 --- a/app/assets/javascripts/drag.js +++ b/app/assets/javascripts/drag.js @@ -6,9 +6,24 @@ $(document).on('turbolinks:load', function() { }).on('drop', function(from, to, el, mode) { // Al soltar, reordenamos toda la tabla $('.post_order').val(function(i,v) { return i; }); - $('.reorder-posts-panel') - .addClass('d-block') - .removeClass('d-none'); + // Mostramos el nuevo orden también + $('.order.is').text(function(i,v) { return i; }); + // Solo mostramos el valor anterior si no coincide con el valor + // actual. Como lo mostramos entre comillas, usamos el atributo + // data-order del + $('.order.was').each(function() { + var el = $(this); + var is = el.parent().find('.order.is'); + + if (is.text() == el.data('order')) { + el.addClass('d-none'); + } else { + el.removeClass('d-none'); + } + }); + + // Muestra el panel + $('.reorder-posts-panel').addClass('d-block').removeClass('d-none'); }); // Ocultar el panel de ayuda con botón diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 86059af3..72b3967d 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -6,6 +6,12 @@ class PostsController < ApplicationController @site = find_site @category = session[:category] = params.dig(:category) @posts = @site.posts_for(@lang) + + if params[:sort_by].present? + @posts.sort_by! do |p| + p.send(params[:sort_by].to_s) + end + end end def show diff --git a/app/controllers/sites_controller.rb b/app/controllers/sites_controller.rb index 238e48a2..7ff72acf 100644 --- a/app/controllers/sites_controller.rb +++ b/app/controllers/sites_controller.rb @@ -43,4 +43,5 @@ class SitesController < ApplicationController redirect_to site_posts_path @site end + end diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index c9e6ec2a..8df63d2d 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -20,6 +20,16 @@ .col = render 'layouts/flash' - if @posts.present? + .row + .col + %small.pull-right + = t('posts.sort.by') + - %w[order date].each do |s| + = link_to t("posts.sort.#{s}"), + site_posts_path(@site, + category: @category, + lang: @lang, + 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'} @@ -38,10 +48,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: post.order || i, class: 'post_order' + = 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})" %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 82398bc7..b7196356 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -137,6 +137,11 @@ en: ar: 'Arabic' posts: reorder_posts: 'Reorder posts' + sort: + by: 'Sort by' + order: 'order' + date: 'date' + order: 'Order' new: 'New post' index: 'Posts' edit: 'Edit' diff --git a/config/locales/es.yml b/config/locales/es.yml index 9d47c5a2..d5743cc1 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -139,6 +139,11 @@ es: ar: 'árabe' posts: reorder_posts: 'Reordenar artículos' + sort: + by: 'Ordenar por' + order: 'posición' + date: 'fecha' + order: 'Posición' new: 'Empezar un artículo nuevo' index: 'Artículos' edit: 'Editar'