5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-07-01 21:16:07 +00:00

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
redirect_to sites_path
else
flash[:error] = t('login.error')
flash[:danger] = t('login.error')
render 'login/new'
end
end

View file

@ -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

View file

@ -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

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
.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

View file

@ -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

View file

@ -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'

View file

@ -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'

View file

@ -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