From b28bdb04270a0a2de7e224b27fa2255e936a2a98 Mon Sep 17 00:00:00 2001 From: Daniel Nitsikopoulos Date: Mon, 7 Aug 2023 15:47:12 +1000 Subject: [PATCH] Filter week posts on homepage --- app/repos/post_repo.rb | 24 +++++++++++++++++++ app/templates/site/home.html.slim | 10 ++++++++ app/views/site/home.rb | 8 ++++++- lib/adamantium/persistence/relations/posts.rb | 10 ++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/app/repos/post_repo.rb b/app/repos/post_repo.rb index ccf4456..09f7ed7 100644 --- a/app/repos/post_repo.rb +++ b/app/repos/post_repo.rb @@ -94,6 +94,18 @@ module Adamantium .to_a end + def week_posts(limit: nil) + posts + .where(post_type: "post", location: nil) + .exclude(name: nil) + .weekly + .published + .combine(:tags) + .order(Sequel.desc(:published_at)) + .limit(limit) + .to_a + end + def post_listing(limit: nil) posts .where(post_type: "post", location: nil) @@ -105,6 +117,18 @@ module Adamantium .to_a end + def home_post_listing(limit: nil) + posts + .where(post_type: "post", location: nil) + .exclude(name: nil) + .non_weekly + .published + .combine(:tags) + .order(Sequel.desc(:published_at)) + .limit(limit) + .to_a + end + def photo_listing(limit: nil) posts .where(post_type: ["post", "checkin"]) diff --git a/app/templates/site/home.html.slim b/app/templates/site/home.html.slim index 0165c23..7be79bf 100644 --- a/app/templates/site/home.html.slim +++ b/app/templates/site/home.html.slim @@ -19,6 +19,16 @@ div class="mb-8 max-w-screen-md mx-auto border-t-4 border-solid border-gray-400 a class="col-span-7 sm:col-span-1 px-2 text-center transition-colors rounded bg-fuchsia-50 hover:bg-fuchsia-200 dark:bg-fuchsia-900 dark:hover:bg-fuchsia-700 inline-block grid content-center max-h-12 my-auto" href="/statuses" p See all +div class="flex justify-between max-w-prose mx-auto mb-2" + h2 class="text-l text-gray-600 dark:text-gray-200" Weekly posts + a class="rounded px-2 text-blue-600 bg-blue-100 dark:bg-blue-900/60 hover:dark:bg-blue-800 hover:dark:text-blue-100 dark:text-blue-200 hover:text-blue-600 hover:bg-blue-200" href="/tagged/weekly" See all → + +div class="mb-12 max-w-prose mx-auto bg-gray-100 rounded p-2 dark:bg-gray-900 dark:text-gray-100" + ul class="columns-2" + - week_posts.each do |post| + li + a class="hover:underline decoration-wavy" href="/post/#{post.slug}" = post.name + div class="flex justify-between max-w-prose mx-auto mb-2" h2 class="text-l text-gray-600 dark:text-gray-200" 📯 Posts a class="rounded px-2 text-blue-600 bg-blue-100 dark:bg-blue-900/60 hover:dark:bg-blue-800 hover:dark:text-blue-100 dark:text-blue-200 hover:text-blue-600 hover:bg-blue-200" href="/posts" See all → diff --git a/app/views/site/home.rb b/app/views/site/home.rb index 84856fe..3ee36c9 100644 --- a/app/views/site/home.rb +++ b/app/views/site/home.rb @@ -10,8 +10,14 @@ module Adamantium renderer.call(content: markdown_content) end + expose :week_posts do + post_repo.week_posts(limit: 10).map do |post| + Decorators::Posts::Decorator.new(post) + end + end + expose :posts do - post_repo.post_listing(limit: 5).map do |post| + post_repo.home_post_listing(limit: 5).map do |post| Decorators::Posts::Decorator.new(post) end end diff --git a/lib/adamantium/persistence/relations/posts.rb b/lib/adamantium/persistence/relations/posts.rb index 735e895..c27ec77 100644 --- a/lib/adamantium/persistence/relations/posts.rb +++ b/lib/adamantium/persistence/relations/posts.rb @@ -22,6 +22,16 @@ module Adamantium where(self[:published_at] <= Time.now) end + def weekly + ref = post_tags.where(:tag_id => "70").select(:post_id).dataset + where(:id => ref) + end + + def non_weekly + ref = post_tags.where(:tag_id => "70").select(:post_id).dataset + exclude(:id => ref) + end + def published_between(start_date, end_date) where(self[:published_at] >= start_date) .where(self[:published_at] <= end_date)