From 7ddc4e615036d6eb6346e31f759561aa54cda764 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 7 Nov 2019 13:06:40 -0300 Subject: [PATCH] ordenar! --- app/controllers/posts_controller.rb | 4 +++- app/models/metadata_date.rb | 3 +++ app/models/post_relation.rb | 16 ++++++++++++++++ app/views/posts/index.haml | 11 ++++++++++- 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index da0fe1f9..14af15fa 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -9,8 +9,10 @@ class PostsController < ApplicationController authorize Post @site = find_site @category = session[:category] = params.dig(:category) + @layout = params.dig(:layout).try :to_sym # TODO: Aplicar policy_scope - @posts = @site.posts(lang: I18n.locale) + @posts = @site.posts(lang: I18n.locale) + @posts.sort_by! :order, :date end def show diff --git a/app/models/metadata_date.rb b/app/models/metadata_date.rb index ea0bad50..7e26c00e 100644 --- a/app/models/metadata_date.rb +++ b/app/models/metadata_date.rb @@ -1,4 +1,7 @@ # frozen_string_literal: true class MetadataDate < MetadataTemplate + def default_value + Date.today + end end diff --git a/app/models/post_relation.rb b/app/models/post_relation.rb index ce1063b2..22eb5ca9 100644 --- a/app/models/post_relation.rb +++ b/app/models/post_relation.rb @@ -31,6 +31,22 @@ class PostRelation < Array post end + alias sort_by_generic! sort_by! + + # Permite ordenar los artículos por sus atributos + # + # XXX: Prestar atención cuando estamos mezclando artículos con + # diferentes tipos de atributos. + def sort_by!(*attrs) + sort_by_generic! do |post| + attrs.map do |attr| + return 0 unless post.attributes.include? attr + + post.public_send(attr).value + end + end + end + alias find_generic find # Encontra un post por su id convertido a SHA1 diff --git a/app/views/posts/index.haml b/app/views/posts/index.haml index 2418b693..12730e82 100644 --- a/app/views/posts/index.haml +++ b/app/views/posts/index.haml @@ -37,6 +37,8 @@ - if @category - next unless post.attributes.include? :categories - next unless post.categories.value.include?(@category) + - if @layout + - next unless post.layout.name == @layout - next unless policy(post).show? %tr %td @@ -45,6 +47,10 @@ = hidden_field 'post[reorder]', post.sha1, value: i, class: 'reorder' %td + %small + = link_to post.layout.name.to_s.humanize, + site_posts_path(@site, layout: post.layout.name) + %br/ = link_to post.title.value, site_post_path(@site, post.id) - if post.attributes.include? :draft @@ -58,7 +64,10 @@ - post.categories.value.each do |c| = link_to c, site_posts_path(@site, category: c) - %td= post.date.value.strftime('%F') + %td + = post.date.value.strftime('%F') + %br/ + = post.try(:order).try(:value) %td - if policy(post).edit? = link_to t('posts.edit'),