reordenar artículos!

This commit is contained in:
f 2018-04-30 15:51:39 -03:00
parent c64abe16b6
commit 7a601a7b31
No known key found for this signature in database
GPG key ID: F3FDAB97B5F9F7E7
9 changed files with 69 additions and 9 deletions

View file

@ -18,7 +18,7 @@ class LoginController < ApplicationController
# TODO enviar a la URL de donde vinimos # TODO enviar a la URL de donde vinimos
redirect_to sites_path redirect_to sites_path
else else
flash[:error] = t('login.error') flash[:danger] = t('login.error')
render 'login/new' render 'login/new'
end end
end end

View file

@ -25,8 +25,22 @@ class SitesController < ApplicationController
def build_log def build_log
@site = find_site @site = find_site
# TODO eliminar ANSI
render file: @site.build_log, render file: @site.build_log,
layout: false, layout: false,
content_type: 'text/plain; charset=utf-8' content_type: 'text/plain; charset=utf-8'
end 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 end

View file

@ -196,16 +196,31 @@ class Site
end.all? end.all?
end end
# Reordena la colección usando la posición actual de los artículos y # Reordena la colección usando la posición informada
# guarda los cambios #
def reorder_collection!(collection = 'posts') # new_order es un hash cuya key es la posición actual del post y el
posts_for(collection).each_with_index do |p, i| # valor la posición nueva
p.update_attributes order: i 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 p.save
end end
posts_for(collection).map(&:ordered?).all? posts_for(collection).map(&:ordered?).all?
end 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! alias :reorder_posts! :reorder_collection!
# El directorio donde se almacenan los sitios # El directorio donde se almacenan los sitios

View file

@ -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} &times;

View file

@ -1,8 +1,6 @@
.row.align-items-center.justify-content-center.full-height .row.align-items-center.justify-content-center.full-height
.col-md-6.align-self-center .col-md-6.align-self-center
- if flash[:error] = render 'layouts/flash'
.alert.alert-danger{role: 'alert'}
= flash[:error]
= form_tag login_path do = form_tag login_path do
.form-group .form-group

View file

@ -18,6 +18,7 @@
.row .row
.col .col
= render 'layouts/flash'
- if @posts.present? - if @posts.present?
= form_tag site_reorder_posts_path, method: :post do = form_tag site_reorder_posts_path, method: :post do
= hidden_field 'posts', 'lang', value: @lang = hidden_field 'posts', 'lang', value: @lang

View file

@ -2,6 +2,11 @@ en:
errors: errors:
argument_error: 'Argument `%{argument}` must be an instance of %{class}' argument_error: 'Argument `%{argument}` must be an instance of %{class}'
unknown_locale: 'Unknown %{locale} locale' 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: help:
category: 'Category' category: 'Category'
logout: 'Close the session' logout: 'Close the session'

View file

@ -2,6 +2,11 @@ es:
errors: errors:
argument_error: 'El argumento `%{argument}` debe ser una instancia de %{class}' argument_error: 'El argumento `%{argument}` debe ser una instancia de %{class}'
unknown_locale: 'El idioma %{locale} es desconocido' 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: help:
category: 'Categoría' category: 'Categoría'
logout: 'Cierra la sesión' logout: 'Cierra la sesión'

View file

@ -24,4 +24,19 @@ class SiteTest < ActiveSupport::TestCase
assert @site.reorder_collection! assert @site.reorder_collection!
assert @site.ordered? assert @site.ordered?
end 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 end