From 519147011e13bc12f001ae83fd85246c3be9f72a Mon Sep 17 00:00:00 2001
From: pabois <pierreandre.boissinot@noesya.coop>
Date: Fri, 19 Nov 2021 14:52:04 +0100
Subject: [PATCH] homepage

---
 .../communication/website/home_controller.rb  | 29 ++++++++
 .../communication/website/pages_controller.rb |  1 +
 app/models/ability.rb                         |  2 +
 app/models/communication/website/home.rb      | 18 +++++
 .../communication/website/home/edit.html.erb  | 35 +++++++++
 .../website/home/jekyll.html.erb              | 10 +++
 .../communication/websites/show.html.erb      | 74 +++++++++++++------
 config/locales/communication/en.yml           |  5 ++
 config/locales/communication/fr.yml           |  5 ++
 config/routes/admin/communication.rb          |  2 +
 ...20211119134711_add_github_path_to_homes.rb |  5 ++
 db/schema.rb                                  |  3 +-
 12 files changed, 167 insertions(+), 22 deletions(-)
 create mode 100644 app/controllers/admin/communication/website/home_controller.rb
 create mode 100644 app/views/admin/communication/website/home/edit.html.erb
 create mode 100644 app/views/admin/communication/website/home/jekyll.html.erb
 create mode 100644 db/migrate/20211119134711_add_github_path_to_homes.rb

diff --git a/app/controllers/admin/communication/website/home_controller.rb b/app/controllers/admin/communication/website/home_controller.rb
new file mode 100644
index 000000000..59ce01e0a
--- /dev/null
+++ b/app/controllers/admin/communication/website/home_controller.rb
@@ -0,0 +1,29 @@
+class Admin::Communication::Website::HomeController < Admin::Communication::Website::ApplicationController
+  before_action :get_home
+
+  def edit
+    breadcrumb
+    add_breadcrumb Communication::Website::Home.model_name.human
+  end
+
+  def update
+    if @home.update(home_params)
+      redirect_to admin_communication_website_path(@website), notice: t('admin.successfully_updated_html', model: Communication::Website::Home.model_name.human)
+    else
+      breadcrumb
+      add_breadcrumb Communication::Website::Home.model_name.human
+      render :edit, status: :unprocessable_entity
+    end
+  end
+
+  protected
+
+  def get_home
+    @home = @website.home
+  end
+
+  def home_params
+    params.require(:communication_website_home)
+          .permit(:text, :featured_image, :featured_image_delete, :featured_image_infos)
+  end
+end
diff --git a/app/controllers/admin/communication/website/pages_controller.rb b/app/controllers/admin/communication/website/pages_controller.rb
index 94d6260a8..f599f794c 100644
--- a/app/controllers/admin/communication/website/pages_controller.rb
+++ b/app/controllers/admin/communication/website/pages_controller.rb
@@ -55,6 +55,7 @@ class Admin::Communication::Website::PagesController < Admin::Communication::Web
   end
 
   def update
+    byebug
     if @page.update(page_params)
       redirect_to admin_communication_website_page_path(@page), notice: t('admin.successfully_updated_html', model: @page.to_s)
     else
diff --git a/app/models/ability.rb b/app/models/ability.rb
index a89951285..72e77c192 100644
--- a/app/models/ability.rb
+++ b/app/models/ability.rb
@@ -16,6 +16,7 @@ class Ability
     can :read, Communication::Website, university_id: @user.university_id
     can :read, Communication::Website::Page, university_id: @user.university_id
     can :read, Communication::Website::Post, university_id: @user.university_id
+    can :read, Communication::Website::Home, university_id: @user.university_id
     can :read, Communication::Website::Imported::Website, university_id: @user.university_id
     can :read, Communication::Website::Imported::Page, university_id: @user.university_id
     can :read, Communication::Website::Imported::Post, university_id: @user.university_id
@@ -35,6 +36,7 @@ class Ability
     can :manage, Communication::Website::Page, university_id: @user.university_id
     can :manage, Communication::Website::Post, university_id: @user.university_id
     can :manage, Communication::Website::Category, university_id: @user.university_id
+    can :manage, Communication::Website::Home, university_id: @user.university_id
     can [:read, :update, :reorder], Communication::Website::Menu, university_id: @user.university_id
     can :manage, Communication::Website::Menu::Item, university_id: @user.university_id
     can :manage, Communication::Website::Imported::Website, university_id: @user.university_id
diff --git a/app/models/communication/website/home.rb b/app/models/communication/website/home.rb
index e964ed99b..7ed72d7be 100644
--- a/app/models/communication/website/home.rb
+++ b/app/models/communication/website/home.rb
@@ -3,6 +3,7 @@
 # Table name: communication_website_homes
 #
 #  id                       :uuid             not null, primary key
+#  github_path              :text
 #  created_at               :datetime         not null
 #  updated_at               :datetime         not null
 #  communication_website_id :uuid             not null
@@ -19,6 +20,7 @@
 #  fk_rails_...  (university_id => universities.id)
 #
 class Communication::Website::Home < ApplicationRecord
+  include WithGithub
 
   belongs_to :university
   belongs_to :website, foreign_key: :communication_website_id
@@ -26,5 +28,21 @@ class Communication::Website::Home < ApplicationRecord
   has_rich_text :text
   has_one_attached_deletable :featured_image
 
+  def github_path_generated
+    '_pages/index.html'
+  end
+
+  def to_s
+    ''
+  end
+
+  def to_jekyll
+    ApplicationController.render(
+      template: 'admin/communication/website/home/jekyll',
+      layout: false,
+      assigns: { home: self }
+    )
+  end
+
 
 end
diff --git a/app/views/admin/communication/website/home/edit.html.erb b/app/views/admin/communication/website/home/edit.html.erb
new file mode 100644
index 000000000..0ed10d7c3
--- /dev/null
+++ b/app/views/admin/communication/website/home/edit.html.erb
@@ -0,0 +1,35 @@
+<% content_for :title, t('activerecord.models.communication/website/home.one') %>
+
+<%= simple_form_for @home, url: admin_communication_website_home_path(website_id: @website.id) do |f| %>
+  <div class="row">
+    <div class="col-md-8">
+      <div class="card flex-fill w-100">
+        <div class="card-header">
+          <h5 class="card-title mb-0"><%= t('content') %></h5>
+        </div>
+        <div class="card-body">
+          <%= f.input :text, as: :rich_text_area %>
+        </div>
+      </div>
+    </div>
+    <div class="col-md-4">
+      <div class="card flex-fill w-100">
+        <div class="card-header">
+          <h5 class="card-title mb-0"><%= t('activerecord.attributes.communication/website/page.featured_image') %></h5>
+        </div>
+        <div class="card-body">
+          <%= f.input :featured_image,
+                      as: :single_deletable_file,
+                      direct_upload: true,
+                      label: false,
+                      input_html: { accept: '.jpg,.jpeg,.png' },
+                      preview: true
+           %>
+        </div>
+      </div>
+    </div>
+  </div>
+  <% content_for :action_bar_right do %>
+    <%= submit f %>
+  <% end %>
+<% end %>
diff --git a/app/views/admin/communication/website/home/jekyll.html.erb b/app/views/admin/communication/website/home/jekyll.html.erb
new file mode 100644
index 000000000..7dff2486e
--- /dev/null
+++ b/app/views/admin/communication/website/home/jekyll.html.erb
@@ -0,0 +1,10 @@
+---
+layout: home
+permalink: "/"
+<% if @home.featured_image.attached? %>
+image: "<%= @home.featured_image.blob.id %>"
+<% end %>
+description: >
+  <%= prepare_for_github @home.text, @home.university %>
+---
+<%= @home.github_frontmatter.content.html_safe %>
diff --git a/app/views/admin/communication/websites/show.html.erb b/app/views/admin/communication/websites/show.html.erb
index f2c550bc2..ccffbb28a 100644
--- a/app/views/admin/communication/websites/show.html.erb
+++ b/app/views/admin/communication/websites/show.html.erb
@@ -10,6 +10,58 @@
   <% end %>
 <% end %>
 
+<div class="row">
+  <div class="col-md-4">
+    <div class="card mt-5">
+      <div class="card-header">
+        <div class="float-end">
+          <%= link_to t('edit'),
+                      admin_communication_website_home_path(website_id: @website),
+                      class: button_classes if can?(:update, Communication::Website::Home) %>
+        </div>
+        <h2 class="card-title">
+          <%= Communication::Website::Home.model_name.human %>
+        </h2>
+      </div>
+      <div class="card-body">
+        <div class="row">
+          <% unless @website.home.text.blank? %>
+            <div class="col-md-6">
+              <%= @website.home.text %>
+            </div>
+          <% end %>
+          <% if @website.home.featured_image.attached? %>
+            <div class="col-md-6">
+              <%= image_tag @website.home.featured_image.variant(resize: '300x^'), class: 'img-fluid' %>
+            </div>
+          <% end %>
+        </div>
+      </div>
+    </div>
+  </div>
+  <div class="col-md-4">
+    <div class="card mt-5">
+      <div class="card-header">
+        <div class="float-end">
+          <%= link_to t('create'),
+                      new_admin_communication_website_menu_path(website_id: @website),
+                      class: button_classes if can?(:create, Communication::Website::Menu) %>
+        </div>
+        <h2 class="card-title">
+          <%= link_to admin_communication_website_menus_path(website_id: @website) do %>
+            <%= Communication::Website::Menu.model_name.human(count: 2) %>
+            <small>
+              -
+              <%= t('communication.website.see_all', number: @website.menus.count) %>
+            </small>
+          <% end %>
+        </h2>
+      </div>
+      <%= render 'admin/communication/website/menus/list', menus: @website.menus.ordered %>
+    </div>
+  </div>
+</div>
+
 <div class="card mt-5">
   <div class="card-header">
     <div class="float-end">
@@ -72,27 +124,7 @@
       <%= render 'admin/communication/website/categories/list', categories: @website.categories.root.ordered %>
     </div>
   </div>
-  <div class="col-md-4">
-    <div class="card mt-5">
-      <div class="card-header">
-        <div class="float-end">
-          <%= link_to t('create'),
-                      new_admin_communication_website_menu_path(website_id: @website),
-                      class: button_classes if can?(:create, Communication::Website::Menu) %>
-        </div>
-        <h2 class="card-title">
-          <%= link_to admin_communication_website_menus_path(website_id: @website) do %>
-            <%= Communication::Website::Menu.model_name.human(count: 2) %>
-            <small>
-              -
-              <%= t('communication.website.see_all', number: @website.menus.count) %>
-            </small>
-          <% end %>
-        </h2>
-      </div>
-      <%= render 'admin/communication/website/menus/list', menus: @website.menus.ordered %>
-    </div>
-  </div>
+
   <div class="col-md-4">
     <div class="card mt-5">
       <div class="card-header">
diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml
index 7111456f7..23975e9a2 100644
--- a/config/locales/communication/en.yml
+++ b/config/locales/communication/en.yml
@@ -31,6 +31,8 @@ en:
         one: Category
         other: Categories
         all: All categories
+      communication/website/home:
+        one: Homepage
       communication/website/imported/website:
         one: Imported website
         other: Imported websites
@@ -71,6 +73,9 @@ en:
         name: Name
         parent: Parent category
         slug: Slug
+      communication/website/home:
+        text: Text
+        featured_image: Featured image
       communication/website/imported/category:
         original: Original
       communication/website/imported/medium:
diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml
index 1028e4588..9efca97a4 100644
--- a/config/locales/communication/fr.yml
+++ b/config/locales/communication/fr.yml
@@ -31,6 +31,8 @@ fr:
         one: Catégorie
         other: Catégories
         all: Toutes les catégories
+      communication/website/home:
+        one: Homepage
       communication/website/imported/website:
         one: Site importé
         other: Sites importés
@@ -71,6 +73,9 @@ fr:
         name: Nom
         parent: Catégorie parente
         slug: Slug
+      communication/website/home:
+        text: Texte
+        featured_image: Image à la une
       communication/website/imported/category:
         original: Original
       communication/website/imported/medium:
diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb
index 4fe17a2a4..d27e977b9 100644
--- a/config/routes/admin/communication.rb
+++ b/config/routes/admin/communication.rb
@@ -1,5 +1,7 @@
 namespace :communication do
   resources :websites do
+    get 'home' => 'website/home#edit'
+    patch 'home' => 'website/home#update'
     member do
       get :import
       post :import
diff --git a/db/migrate/20211119134711_add_github_path_to_homes.rb b/db/migrate/20211119134711_add_github_path_to_homes.rb
new file mode 100644
index 000000000..e3247a6e1
--- /dev/null
+++ b/db/migrate/20211119134711_add_github_path_to_homes.rb
@@ -0,0 +1,5 @@
+class AddGithubPathToHomes < ActiveRecord::Migration[6.1]
+  def change
+    add_column :communication_website_homes, :github_path, :text
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 2308848c0..75ba4abda 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 2021_11_19_111703) do
+ActiveRecord::Schema.define(version: 2021_11_19_134711) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "pgcrypto"
@@ -121,6 +121,7 @@ ActiveRecord::Schema.define(version: 2021_11_19_111703) do
     t.uuid "communication_website_id", null: false
     t.datetime "created_at", precision: 6, null: false
     t.datetime "updated_at", precision: 6, null: false
+    t.text "github_path"
     t.index ["communication_website_id"], name: "idx_comm_website_homes_on_communication_website_id"
     t.index ["university_id"], name: "index_communication_website_homes_on_university_id"
   end
-- 
GitLab