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 0000000000000000000000000000000000000000..59ce01e0ac6e1b8455081adb1d94389cb0b405f7 --- /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 94d6260a8466ec03487826fb33b8d667e609158f..f599f794cef1f5aa6af33f006bdbd926fc0d4690 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 a89951285f9a4c09f9ec4b8b35867ffd39aca83b..72e77c19270bb9dd909780c1e72033853d5d34b3 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 e964ed99b00ab990f151f9d7d93f240f2368e6e7..7ed72d7be7b6c6848b218460e40e6f155b3abb6f 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 0000000000000000000000000000000000000000..0ed10d7c326e974165510f78f280b8040b2b1018 --- /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 0000000000000000000000000000000000000000..7dff2486eaee71418043d0a41fa257c2ca24cec2 --- /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 f2c550bc2f249f4bada18a450477e42e9239adb8..ccffbb28a0cafa2fba89e5b6e710cc4a1b53a872 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 7111456f768738cf32cecf9fb38582cbd205792b..23975e9a2188b1922b71bfc5b2ace532b114ef71 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 1028e45885fcaeab2e8cdcafabd5175627969e76..9efca97a4695df9c0af37bf457da2e19ae4b1307 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 4fe17a2a424bb792753f2ed08086f50231ff483a..d27e977b98bc8e629cb6c6dd4837984747668122 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 0000000000000000000000000000000000000000..e3247a6e15beb851e5a6e5e2e919cb83f6e12575 --- /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 2308848c0d92a84ff25491c894e4f082b90bf8f5..75ba4abda76655f950ecbfa2c43c737585b3387d 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