From ffd8790beb8205996199f229a2a3d559d80d561c Mon Sep 17 00:00:00 2001 From: pabois <pierreandre.boissinot@noesya.coop> Date: Wed, 20 Oct 2021 12:22:09 +0200 Subject: [PATCH] post editor --- .../javascripts/admin/trix_direct_upload.js | 20 ++++++++++++++ .../direct_uploads_controller.rb | 27 +++++++++++++++++++ app/inputs/trix_editor_input.rb | 10 +++++-- app/models/communication/website/page.rb | 4 ++- app/models/communication/website/post.rb | 4 ++- app/models/concerns/.keep | 0 app/models/concerns/with_slug.rb | 22 +++++++++++++++ .../website/pages/_list.html.erb | 6 +++-- .../website/posts/_form.html.erb | 4 +-- .../website/posts/_list.html.erb | 20 +++++++------- .../communication/website/posts/show.html.erb | 18 ++++++------- .../communication/websites/index.html.erb | 6 +++-- app/views/admin/users/index.html.erb | 6 +++-- app/views/server/languages/index.html.erb | 6 +++-- app/views/server/universities/index.html.erb | 6 +++-- config/locales/communication/en.yml | 10 +++++++ config/locales/communication/fr.yml | 14 ++++++++-- 17 files changed, 147 insertions(+), 36 deletions(-) create mode 100644 app/assets/javascripts/admin/trix_direct_upload.js create mode 100644 app/controllers/active_storage/direct_uploads_controller.rb delete mode 100644 app/models/concerns/.keep create mode 100644 app/models/concerns/with_slug.rb diff --git a/app/assets/javascripts/admin/trix_direct_upload.js b/app/assets/javascripts/admin/trix_direct_upload.js new file mode 100644 index 000000000..f622fde38 --- /dev/null +++ b/app/assets/javascripts/admin/trix_direct_upload.js @@ -0,0 +1,20 @@ +(function() { + + addEventListener("trix-attachment-add", function(event) { + var file = event.attachment.file; + if (file) { + var upload = new window.ActiveStorage.DirectUpload(file,'/rails/active_storage/direct_uploads', window); + upload.create((error, attributes) => { + if (error) { + return false; + } else { + return event.attachment.setAttributes({ + url: `/rails/active_storage/blobs/${attributes.signed_id}/${attributes.filename}`, + href: `/rails/active_storage/blobs/${attributes.signed_id}/${attributes.filename}`, + }); + } + }); + } + }) + +})(); diff --git a/app/controllers/active_storage/direct_uploads_controller.rb b/app/controllers/active_storage/direct_uploads_controller.rb new file mode 100644 index 000000000..fe237d614 --- /dev/null +++ b/app/controllers/active_storage/direct_uploads_controller.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# Creates a new blob on the server side in anticipation of a direct-to-service upload from the client side. +# When the client-side upload is completed, the signed_blob_id can be submitted as part of the form to reference +# the blob that was created up front. +class ActiveStorage::DirectUploadsController < ActiveStorage::BaseController + include ApplicationController::WithUniversity + + def create + blob = ActiveStorage::Blob.create_before_direct_upload!(**blob_args) + blob.update_column(:university_id, current_university.id) + render json: direct_upload_json(blob) + end + + private + def blob_args + params.require(:blob).permit(:filename, :byte_size, :checksum, :content_type, metadata: {}).to_h.symbolize_keys + end + + def direct_upload_json(blob) + blob.as_json(root: false, methods: :signed_id).merge(direct_upload: { + url: blob.service_url_for_direct_upload, + headers: blob.service_headers_for_direct_upload + }) + end + +end diff --git a/app/inputs/trix_editor_input.rb b/app/inputs/trix_editor_input.rb index 7e71b8ff5..248f61147 100644 --- a/app/inputs/trix_editor_input.rb +++ b/app/inputs/trix_editor_input.rb @@ -27,10 +27,16 @@ class TrixEditorInput < SimpleForm::Inputs::Base end def id - "#{@builder.object.class.to_s.downcase}_#{attribute_name}" + "#{object_name}_#{attribute_name}" end def name - "#{@builder.object.class.to_s.downcase}[#{attribute_name}]" + "#{object_name}[#{attribute_name}]" + end + + private + + def object_name + @builder.object.class.to_s.downcase.gsub('::', '_') end end diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 65c9cdddc..a47badb52 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -33,6 +33,8 @@ # class Communication::Website::Page < ApplicationRecord + include WithSlug + belongs_to :university belongs_to :website, foreign_key: :communication_website_id @@ -45,7 +47,7 @@ class Communication::Website::Page < ApplicationRecord has_one :imported_page, class_name: 'Communication::Website::Imported::Page', foreign_key: :page_id, - dependent: :nullify + dependent: :destroy validates :title, presence: true diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 7d40a1fb3..e7d4f3883 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -25,13 +25,15 @@ # fk_rails_... (university_id => universities.id) # class Communication::Website::Post < ApplicationRecord + include WithSlug + belongs_to :university belongs_to :website, foreign_key: :communication_website_id has_one :imported_post, class_name: 'Communication::Website::Imported::Post', foreign_key: :post_id, - dependent: :nullify + dependent: :destroy scope :ordered, -> { order(published_at: :desc, created_at: :desc) } scope :recent, -> { order(published_at: :desc).limit(5) } diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/models/concerns/with_slug.rb b/app/models/concerns/with_slug.rb new file mode 100644 index 000000000..cf2785e4c --- /dev/null +++ b/app/models/concerns/with_slug.rb @@ -0,0 +1,22 @@ +module WithSlug + extend ActiveSupport::Concern + + included do + before_validation :generate_slug, if: Proc.new { |o| o.slug.blank? } + end + + protected + + def generate_slug + n = nil + loop do + self.slug = [to_s.parameterize, n].compact.join('-') + break if slug_available? + n = n.to_i + 1 + end + end + + def slug_available? + self.class.unscoped.where.not(id: self.id).where(university_id: self.university_id, slug: self.slug).none? + end +end diff --git a/app/views/admin/communication/website/pages/_list.html.erb b/app/views/admin/communication/website/pages/_list.html.erb index 8d714dc6b..310b52c54 100644 --- a/app/views/admin/communication/website/pages/_list.html.erb +++ b/app/views/admin/communication/website/pages/_list.html.erb @@ -14,14 +14,16 @@ <td><%= page.path %></td> <td><%= link_to page.parent, admin_communication_website_page_path(website_id: page.website.id, id: page.parent.id) if page.parent %></td> <td class="text-end"> - <%= link_to t('edit'), + <div class="btn-group" role="group"> + <%= link_to t('edit'), edit_admin_communication_website_page_path(website_id: page.website.id, id: page.id), class: button_classes %> - <%= link_to t('delete'), + <%= link_to t('delete'), admin_communication_website_page_path(website_id: page.website.id, id: page.id), method: :delete, data: { confirm: t('please-confirm') }, class: button_classes_danger %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/communication/website/posts/_form.html.erb b/app/views/admin/communication/website/posts/_form.html.erb index 07d1078e5..a55a613a0 100644 --- a/app/views/admin/communication/website/posts/_form.html.erb +++ b/app/views/admin/communication/website/posts/_form.html.erb @@ -3,7 +3,7 @@ <div class="col-md-8"> <div class="card flex-fill w-100"> <div class="card-header"> - <h5 class="card-title mb-0">Content</h5> + <h5 class="card-title mb-0"><%= t('communication.website.content') %></h5> </div> <div class="card-body"> <%= f.input :title %> @@ -15,7 +15,7 @@ <div class="col-md-4"> <div class="card flex-fill w-100"> <div class="card-header"> - <h5 class="card-title mb-0">Metadata</h5> + <h5 class="card-title mb-0"><%= t('communication.website.metadata') %></h5> </div> <div class="card-body"> <%= f.input :published %> diff --git a/app/views/admin/communication/website/posts/_list.html.erb b/app/views/admin/communication/website/posts/_list.html.erb index 8761c83b6..7f6971c59 100644 --- a/app/views/admin/communication/website/posts/_list.html.erb +++ b/app/views/admin/communication/website/posts/_list.html.erb @@ -3,7 +3,7 @@ <tr> <th><%= Communication::Website::Post.human_attribute_name('title') %></th> <th><%= Communication::Website::Post.human_attribute_name('published_at') %></th> - <th width="150"></th> + <th></th> </tr> </thead> <tbody> @@ -12,14 +12,16 @@ <td><%= link_to post, admin_communication_website_post_path(website_id: post.website.id, id: post.id) %></td> <td><%= l post.published_at, format: :long if post.published_at %></td> <td class="text-end"> - <%= link_to t('edit'), - edit_admin_communication_website_post_path(website_id: post.website.id, id: post.id), - class: button_classes %> - <%= link_to t('delete'), - admin_communication_website_post_path(website_id: post.website.id, id: post.id), - method: :delete, - data: { confirm: t('please-confirm') }, - class: button_classes_danger %> + <div class="btn-group" role="group"> + <%= link_to t('edit'), + edit_admin_communication_website_post_path(website_id: post.website.id, id: post.id), + class: button_classes %> + <%= link_to t('delete'), + admin_communication_website_post_path(website_id: post.website.id, id: post.id), + method: :delete, + data: { confirm: t('please-confirm') }, + class: button_classes_danger %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb index c228339cd..310601d1f 100644 --- a/app/views/admin/communication/website/posts/show.html.erb +++ b/app/views/admin/communication/website/posts/show.html.erb @@ -4,25 +4,25 @@ <div class="col-md-8"> <div class="card flex-fill w-100"> <div class="card-header"> - <h5 class="card-title mb-0">Content</h5> + <h5 class="card-title mb-0"><%= t('communication.website.content') %></h5> </div> <div class="card-body"> <p> - <strong>Description</strong> - <%= @post.description %> + <strong><%= Communication::Website::Post.human_attribute_name('description') %></strong> + <%= sanitize @post.description %> </p> <p> - <strong>Text</strong> + <strong><%= Communication::Website::Post.human_attribute_name('text') %></strong> </p> - <%= raw @post.text %> + <%= sanitize @post.text %> </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">Metadata</h5> + <h5 class="card-title mb-0"><%= t('communication.website.metadata') %></h5> </div> <table class="<%= table_classes %>"> <tbody> @@ -32,12 +32,12 @@ </tr> <tr> <td><%= Communication::Website::Page.human_attribute_name('published') %></td> - <td><%= @post.published %></td> + <td><%= t @post.published %></td> </tr> <% if @post.imported_post %> <tr> - <td>Imported from</td> - <td><a href="<%= @post.imported_post.url %>" target="_blank">Original URL</a></td> + <td><%= t('communication.website.imported.from') %></td> + <td><a href="<%= @post.imported_post.url %>" target="_blank"><%= @post.imported_post.url %></a></td> </tr> <% end %> </tbody> diff --git a/app/views/admin/communication/websites/index.html.erb b/app/views/admin/communication/websites/index.html.erb index c5c745c2a..bd4d631c6 100644 --- a/app/views/admin/communication/websites/index.html.erb +++ b/app/views/admin/communication/websites/index.html.erb @@ -18,8 +18,10 @@ <td><%= I18n.t("activerecord.attributes.communication/website.about_#{website.about_type}") %></td> <td><%= website.about %></td> <td class="text-end"> - <%= edit_link website %> - <%= destroy_link website %> + <div class="btn-group" role="group"> + <%= edit_link website %> + <%= destroy_link website %> + </div> </td> </tr> <% end %> diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index cf8e84b41..3164b6cef 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -20,8 +20,10 @@ <td><%= user.role.humanize %></td> <td><%= user.language %></td> <td class="text-end"> - <%= edit_link user %> - <%= destroy_link user %> + <div class="btn-group" role="group"> + <%= edit_link user %> + <%= destroy_link user %> + </div> </td> </tr> <% end %> diff --git a/app/views/server/languages/index.html.erb b/app/views/server/languages/index.html.erb index 21d6b647b..9f9d5506a 100644 --- a/app/views/server/languages/index.html.erb +++ b/app/views/server/languages/index.html.erb @@ -15,14 +15,16 @@ <td><%= link_to language, [:server, language] %></td> <td><%= language.iso_code %></td> <td class="text-end"> - <%= link_to t('edit'), + <div class="btn-group" role="group"> + <%= link_to t('edit'), edit_server_language_path(language), class: button_classes %> - <%= link_to t('delete'), + <%= link_to t('delete'), server_language_path(language), method: :delete, data: { confirm: t('please-confirm') }, class: button_classes_danger %> + </div> </td> </tr> <% end %> diff --git a/app/views/server/universities/index.html.erb b/app/views/server/universities/index.html.erb index cc11a845e..8f7a02c7e 100644 --- a/app/views/server/universities/index.html.erb +++ b/app/views/server/universities/index.html.erb @@ -16,14 +16,16 @@ <td><%= link_to university.url, university.url, target: :_blank %></td> <td><%= university.private ? University.human_attribute_name('private') : University.human_attribute_name('public') %></td> <td class="text-end"> - <%= link_to t('edit'), + <div class="btn-group" role="group"> + <%= link_to t('edit'), edit_server_university_path(university), class: button_classes %> - <%= link_to t('delete'), + <%= link_to t('delete'), server_university_path(university), method: :delete, data: { confirm: t('please-confirm') }, class: button_classes_danger %> + </div> </td> </tr> <% end %> diff --git a/config/locales/communication/en.yml b/config/locales/communication/en.yml index ad4de977c..927ca95cb 100644 --- a/config/locales/communication/en.yml +++ b/config/locales/communication/en.yml @@ -1,7 +1,9 @@ en: communication: website: + content: Content imported: + from: Imported from launch: Launch import media: file_size: File size @@ -9,6 +11,7 @@ en: refresh: Refresh import show: Show import pending: Import in progress + metadata: Metadata activemodel: models: communication: Communication @@ -45,6 +48,13 @@ en: published: Published ? parent: Parent page website: Website + communication/website/post: + title: Title + description: Description (SEO) + text: Text + published: Published ? + published_at: Publication date + website: Website simple_form: hints: communication_website_page: diff --git a/config/locales/communication/fr.yml b/config/locales/communication/fr.yml index a487c22d3..5c0fbd229 100644 --- a/config/locales/communication/fr.yml +++ b/config/locales/communication/fr.yml @@ -1,7 +1,9 @@ fr: communication: website: + content: Contenu imported: + from: Importé depuis launch: Importer le site media: file_size: Taille du fichier @@ -9,6 +11,7 @@ fr: refresh: Relancer l'import show: Voir l'import pending: Import en cours + metadata: Informations activemodel: models: communication: Communication @@ -39,11 +42,18 @@ fr: communication/website/imported/medium: filename: Nom du fichier communication/website/page: + description: Description (SEO) + parent: Page parente + published: Publié ? + text: Texte title: Titre + website: Site Web + communication/website/post: description: Description (SEO) - text: Text published: Publié ? - parent: Page parente + published_at: Date de publication + text: Texte + title: Titre website: Site Web simple_form: hints: -- GitLab