From 1ce6ffbae74a70e5b943b50e22ffd204b1b2e983 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 18 May 2022 05:54:03 +0200
Subject: [PATCH] Posts published in the future are indeed not published

---
 .../admin/communication/websites_controller.rb  |  2 ++
 app/models/communication/website/post.rb        | 17 ++++++++++++++++-
 .../communication/websites/show/_pages.html.erb |  3 ++-
 .../communication/websites/show/_posts.html.erb |  5 ++++-
 4 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/app/controllers/admin/communication/websites_controller.rb b/app/controllers/admin/communication/websites_controller.rb
index aa95abda1..a4bdc6970 100644
--- a/app/controllers/admin/communication/websites_controller.rb
+++ b/app/controllers/admin/communication/websites_controller.rb
@@ -13,6 +13,8 @@ class Admin::Communication::WebsitesController < Admin::Communication::Applicati
   end
 
   def show
+    @pages = @website.pages.published.recent
+    @posts = @website.posts.published.recent
     breadcrumb
   end
 
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 7702f2fa2..c498286f6 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -66,7 +66,18 @@ class Communication::Website::Post < ApplicationRecord
   before_validation :set_published_at, if: :published_changed?
   after_save_commit :update_authors_statuses!, if: :saved_change_to_author_id?
 
-  scope :published, -> { where(published: true) }
+  scope :published, -> {
+    where("
+      communication_website_posts.published = true AND
+      DATE(communication_website_posts.published_at) <= now()
+    ")
+  }
+  scope :published_in_the_future, -> {
+    where("
+      communication_website_posts.published = true AND
+      DATE(communication_website_posts.published_at) > now()
+    ")
+  }
   scope :ordered, -> { order(published_at: :desc, created_at: :desc) }
   scope :recent, -> { order(published_at: :desc).limit(5) }
   scope :for_author, -> (author_id) { where(author_id: author_id) }
@@ -84,6 +95,10 @@ class Communication::Website::Post < ApplicationRecord
     ", term: "%#{sanitize_sql_like(term)}%")
   }
 
+  def published?
+    published && published_at.to_date <= Date.today
+  end
+
   # Is it used?
   def path
     # used in menu_item#static_target
diff --git a/app/views/admin/communication/websites/show/_pages.html.erb b/app/views/admin/communication/websites/show/_pages.html.erb
index cbc93af84..17fbbd207 100644
--- a/app/views/admin/communication/websites/show/_pages.html.erb
+++ b/app/views/admin/communication/websites/show/_pages.html.erb
@@ -15,5 +15,6 @@
       <% end %>
     </h2>
   </div>
-  <%= render 'admin/communication/websites/pages/list', pages: @website.pages.recent %>
+  <%= render 'admin/communication/websites/pages/list',
+              pages: @pages %>
 </div>
diff --git a/app/views/admin/communication/websites/show/_posts.html.erb b/app/views/admin/communication/websites/show/_posts.html.erb
index d1d97da04..9f91cbbdc 100644
--- a/app/views/admin/communication/websites/show/_posts.html.erb
+++ b/app/views/admin/communication/websites/show/_posts.html.erb
@@ -18,5 +18,8 @@
       <% end %>
     </h2>
   </div>
-  <%= render 'admin/communication/websites/posts/list', posts: @website.posts.recent, hide_author: true, hide_category: true %>
+  <%= render 'admin/communication/websites/posts/list',
+              posts: @posts,
+              hide_author: true,
+              hide_category: true %>
 </div>
-- 
GitLab