From e2656264acd17d6dbfa43ab27134358ced698233 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Wed, 23 Feb 2022 21:09:45 +0100 Subject: [PATCH] Fix #213 --- .../website/categories_controller.rb | 11 +++++++++-- app/models/communication/website/category.rb | 15 +++++++++++++-- .../website/categories/_form.html.erb | 15 +++++++++++++++ .../website/categories/show.html.erb | 16 ++++++++++++++++ .../website/categories/static.html.erb | 4 ++++ config/locales/communication/en.yml | 2 ++ config/locales/communication/fr.yml | 2 ++ config/routes/admin/communication.rb | 1 + ...mage_alt_to_communication_website_category.rb | 5 +++++ db/schema.rb | 3 ++- 10 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20220223193608_add_featured_image_alt_to_communication_website_category.rb diff --git a/app/controllers/admin/communication/website/categories_controller.rb b/app/controllers/admin/communication/website/categories_controller.rb index 1c609c597..d755ae142 100644 --- a/app/controllers/admin/communication/website/categories_controller.rb +++ b/app/controllers/admin/communication/website/categories_controller.rb @@ -33,7 +33,12 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication @posts = @category.posts.ordered.page(params[:page]) breadcrumb end - + + def static + @about = @category + render layout: false + end + def new @category.website = @website breadcrumb @@ -84,7 +89,9 @@ class Admin::Communication::Website::CategoriesController < Admin::Communication def category_params params.require(:communication_website_category) - .permit(:website_id, :name, :description, :slug, :parent_id) + .permit( + :website_id, :name, :description, :slug, :parent_id, + :featured_image, :featured_image_delete, :featured_image_infos, :featured_image_alt) .merge(university_id: current_university.id) end end diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb index f1bbf7bd9..5dd734d33 100644 --- a/app/models/communication/website/category.rb +++ b/app/models/communication/website/category.rb @@ -4,6 +4,7 @@ # # id :uuid not null, primary key # description :text +# featured_image_alt :string # github_path :text # is_programs_root :boolean default(FALSE) # name :string @@ -33,6 +34,8 @@ # class Communication::Website::Category < ApplicationRecord include WithGit + include WithFeaturedImage + include WithBlobs include WithMenuItemTarget include WithSlug # We override slug_unavailable? method include WithTree @@ -76,11 +79,11 @@ class Communication::Website::Category < ApplicationRecord end def git_dependencies(website) - [self] + descendents + posts + [self] + descendents + active_storage_blobs + posts end def git_destroy_dependencies(website) - [self] + descendents + [self] + descendents + active_storage_blobs end def update_children_paths @@ -103,4 +106,12 @@ class Communication::Website::Category < ApplicationRecord def slug_unavailable?(slug) self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists? end + + def explicit_blob_ids + super.concat [best_featured_image&.blob_id] + end + + def inherited_blob_ids + [best_featured_image&.blob_id] + end end diff --git a/app/views/admin/communication/website/categories/_form.html.erb b/app/views/admin/communication/website/categories/_form.html.erb index fc0f0cd9a..c0c122f02 100644 --- a/app/views/admin/communication/website/categories/_form.html.erb +++ b/app/views/admin/communication/website/categories/_form.html.erb @@ -31,6 +31,21 @@ </ul> </div> </div> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0"><%= t('activerecord.attributes.communication/website/category.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 + %> + <%= f.input :featured_image_alt %> + </div> + </div> </div> </div> <% content_for :action_bar_right do %> diff --git a/app/views/admin/communication/website/categories/show.html.erb b/app/views/admin/communication/website/categories/show.html.erb index 1d529ac45..cff0dcbea 100644 --- a/app/views/admin/communication/website/categories/show.html.erb +++ b/app/views/admin/communication/website/categories/show.html.erb @@ -45,6 +45,16 @@ <% end %> </div> </div> + <% if @category.featured_image.attached? && @category.featured_image.variable? %> + <div class="card flex-fill w-100"> + <div class="card-header"> + <h5 class="card-title mb-0"><%= t('activerecord.attributes.communication/website/category.featured_image') %></h5> + </div> + <div class="card-body"> + <%= image_tag @category.featured_image.variant(resize: '600'), class: 'img-fluid mb-3' %><br> + </div> + </div> + <% end %> </div> </div> <% if @posts.total_count > 0 %> @@ -60,6 +70,12 @@ <% end %> <% end %> +<% content_for :action_bar_left do %> + <%= link_to t('static'), + static_admin_communication_website_category_path(@category), + class: button_classes('btn-light') if can? :static, @category %> +<% end %> + <% content_for :action_bar_right do %> <%= edit_link @category %> <% end %> diff --git a/app/views/admin/communication/website/categories/static.html.erb b/app/views/admin/communication/website/categories/static.html.erb index 1b5b089e4..500d4021e 100644 --- a/app/views/admin/communication/website/categories/static.html.erb +++ b/app/views/admin/communication/website/categories/static.html.erb @@ -2,5 +2,9 @@ title: "<%= @about.name %>" slug: "<%= @about.slug %>" position: <%= @about.position %> +<% if @about.best_featured_image.attached? %> +image: "<%= @about.best_featured_image.blob.id %>" +image_alt: "<%= @about.featured_image_alt %>" +<% end %> --- <%= prepare_html_for_static @about.description, @about.university %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index f1ff17788..257d635cd 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -48,6 +48,8 @@ en: communication/website/category: children: Children categories description: Description (SEO) + featured_image: Featured image + featured_image_alt: Alt text name: Name parent: Parent category slug: Slug diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index ac824d3f5..ab6348c31 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -51,6 +51,8 @@ fr: communication/website/category: children: Catégories enfants description: Description (SEO) + featured_image: Image à la une + featured_image_alt: Texte alternatif name: Nom parent: Catégorie parente slug: Slug diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index 5b465162b..d92061b04 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -21,6 +21,7 @@ namespace :communication do end member do get :children + get :static end end resources :authors, controller: 'website/authors', only: [:index, :show] diff --git a/db/migrate/20220223193608_add_featured_image_alt_to_communication_website_category.rb b/db/migrate/20220223193608_add_featured_image_alt_to_communication_website_category.rb new file mode 100644 index 000000000..3d2cd0bd1 --- /dev/null +++ b/db/migrate/20220223193608_add_featured_image_alt_to_communication_website_category.rb @@ -0,0 +1,5 @@ +class AddFeaturedImageAltToCommunicationWebsiteCategory < ActiveRecord::Migration[6.1] + def change + add_column :communication_website_categories, :featured_image_alt, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index f5d25a912..fc6fe5f52 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: 2022_02_23_082242) do +ActiveRecord::Schema.define(version: 2022_02_23_193608) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" @@ -108,6 +108,7 @@ ActiveRecord::Schema.define(version: 2022_02_23_082242) do t.uuid "program_id" t.boolean "is_programs_root", default: false t.string "path" + t.string "featured_image_alt" t.index ["communication_website_id"], name: "idx_communication_website_post_cats_on_communication_website_id" t.index ["parent_id"], name: "index_communication_website_categories_on_parent_id" t.index ["program_id"], name: "index_communication_website_categories_on_program_id" -- GitLab