From 27a1ca315e95f5815695523aa4c700ad39140738 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Gaya?= <sebastien.gaya@gmail.com>
Date: Tue, 4 Jan 2022 15:51:27 +0100
Subject: [PATCH] handle blobs in git

---
 app/models/communication/website/category.rb           |  4 ++++
 app/models/communication/website/home.rb               |  5 +++++
 app/models/communication/website/menu/item.rb          |  4 ++++
 app/models/communication/website/page.rb               |  6 +++++-
 app/models/communication/website/post.rb               |  2 +-
 app/models/concerns/with_tree.rb                       |  2 +-
 app/models/education/program.rb                        |  4 ++++
 .../{ => admin}/active_storage/blobs/static.html.erb   |  0
 config/initializers/active_storage.rb                  | 10 ++++++++++
 9 files changed, 34 insertions(+), 3 deletions(-)
 rename app/views/{ => admin}/active_storage/blobs/static.html.erb (100%)

diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb
index 56c83147c..08c2fa1f6 100644
--- a/app/models/communication/website/category.rb
+++ b/app/models/communication/website/category.rb
@@ -94,6 +94,10 @@ class Communication::Website::Category < ApplicationRecord
     end
   end
 
+  def siblings
+    self.class.unscoped.where(parent: parent, university: university, website: website).where.not(id: id)
+  end
+
   protected
 
   def set_position
diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb
index 0228b9258..52a62f20e 100644
--- a/app/models/communication/website/home.rb
+++ b/app/models/communication/website/home.rb
@@ -37,4 +37,9 @@ class Communication::Website::Home < ApplicationRecord
   def git_path_static
     'content/_index.html'
   end
+
+  def git_dependencies_static
+    active_storage_blobs
+  end
+
 end
diff --git a/app/models/communication/website/menu/item.rb b/app/models/communication/website/menu/item.rb
index 39f079a91..781c15a65 100644
--- a/app/models/communication/website/menu/item.rb
+++ b/app/models/communication/website/menu/item.rb
@@ -119,6 +119,10 @@ class Communication::Website::Menu::Item < ApplicationRecord
     menu.force_publish!
   end
 
+  def siblings
+    self.class.unscoped.where(parent: parent, university: university, website: website).where.not(id: id)
+  end
+
   protected
 
   def set_position
diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb
index 631d3b0ed..365463b5c 100644
--- a/app/models/communication/website/page.rb
+++ b/app/models/communication/website/page.rb
@@ -76,7 +76,7 @@ class Communication::Website::Page < ApplicationRecord
   end
 
   def git_dependencies_static
-    descendents + siblings
+    descendents + siblings + active_storage_blobs
   end
 
   def to_s
@@ -97,6 +97,10 @@ class Communication::Website::Page < ApplicationRecord
     end
   end
 
+  def siblings
+    self.class.unscoped.where(parent: parent, university: university, website: website).where.not(id: id)
+  end
+
   protected
 
   def slug_unavailable?(slug)
diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb
index 616d3a21d..c852f337c 100644
--- a/app/models/communication/website/post.rb
+++ b/app/models/communication/website/post.rb
@@ -72,7 +72,7 @@ class Communication::Website::Post < ApplicationRecord
   end
 
   def git_dependencies_static
-    [author] + categories
+    [author] + categories + active_storage_blobs
   end
 
   def to_s
diff --git a/app/models/concerns/with_tree.rb b/app/models/concerns/with_tree.rb
index 8bfa2d557..9d15c5e8b 100644
--- a/app/models/concerns/with_tree.rb
+++ b/app/models/concerns/with_tree.rb
@@ -24,7 +24,7 @@ module WithTree
     end
 
     def siblings
-      self.class.where(parent: parent, university: university, website: website).where.not(id: id)
+      self.class.unscoped.where(parent: parent, university: university).where.not(id: id)
     end
 
     def self_and_children(level)
diff --git a/app/models/education/program.rb b/app/models/education/program.rb
index 998df27c4..98884b238 100644
--- a/app/models/education/program.rb
+++ b/app/models/education/program.rb
@@ -111,6 +111,10 @@ class Education::Program < ApplicationRecord
     "content/programs/#{path}/_index.html".gsub(/\/+/, '/')
   end
 
+  def git_dependencies_static
+    active_storage_blobs
+  end
+
   def update_children_paths
     children.each do |child|
       child.update_column :path, child.generated_path
diff --git a/app/views/active_storage/blobs/static.html.erb b/app/views/admin/active_storage/blobs/static.html.erb
similarity index 100%
rename from app/views/active_storage/blobs/static.html.erb
rename to app/views/admin/active_storage/blobs/static.html.erb
diff --git a/config/initializers/active_storage.rb b/config/initializers/active_storage.rb
index efe52fbbf..c03a9137f 100644
--- a/config/initializers/active_storage.rb
+++ b/config/initializers/active_storage.rb
@@ -32,3 +32,13 @@ ActiveStorage::Filename.class_eval do
     [base_filename, extension_with_delimiter].join('')
   end
 end
+
+module ActiveStorageGitPathStatic
+  def git_path_static
+    "data/media/#{id[0..1]}/#{id}.yml"
+  end
+end
+
+ActiveSupport::Reloader.to_prepare do
+  ActiveStorage::Blob.include ActiveStorageGitPathStatic
+end
-- 
GitLab