From 9af2c4f2443c3a7deaa7e0fb6b59714c2499c53a Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 22 Apr 2024 16:42:12 +0200 Subject: [PATCH] feat: show a simple FAQ --- app/controllers/faq_controller.rb | 27 ++++++++++++++++ app/services/faqs_loader_service.rb | 32 +++++++++++++++++++ app/views/faq/show.html.haml | 6 ++++ config/initializers/inflections.rb | 2 ++ config/locales/faqs.fr.yml | 9 ++++++ config/routes.rb | 2 ++ .../comment-creer-ma-demarche.fr.md | 16 ++++++++++ 7 files changed, 94 insertions(+) create mode 100644 app/controllers/faq_controller.rb create mode 100644 app/services/faqs_loader_service.rb create mode 100644 app/views/faq/show.html.haml create mode 100644 config/locales/faqs.fr.yml create mode 100644 doc/faqs/administrateur/comment-creer-ma-demarche.fr.md diff --git a/app/controllers/faq_controller.rb b/app/controllers/faq_controller.rb new file mode 100644 index 000000000..ba0b69a95 --- /dev/null +++ b/app/controllers/faq_controller.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class FAQController < ApplicationController + before_action :load_faq_data, only: :show + + def show + @renderer = Redcarpet::Markdown.new( + Redcarpet::BareRenderer.new(class_names_map: { list: 'fr-ol-content--override' }) + ) + end + + private + + def loader_service + @loader_service ||= FAQsLoaderService.new + end + + def load_faq_data + path = "#{params[:category]}/#{params[:slug]}" + faq_data = loader_service.find(path) + + @content = faq_data.content + @metadata = faq_data.front_matter.symbolize_keys + rescue KeyError + raise ActionController::RoutingError.new("FAQ not found: #{path}") + end +end diff --git a/app/services/faqs_loader_service.rb b/app/services/faqs_loader_service.rb new file mode 100644 index 000000000..2665d8aa8 --- /dev/null +++ b/app/services/faqs_loader_service.rb @@ -0,0 +1,32 @@ +# frozen_string_literal: true + +class FAQsLoaderService + PATH = Rails.root.join('doc', 'faqs').freeze + ORDER = ['usager', 'instructeur', 'administrateur'].freeze + + def initialize + @faqs_by_path ||= Rails.cache.fetch("faqs_data", expires_in: 1.day) do + load_faqs + end + end + + def find(path) + file_path = @faqs_by_path.fetch(path).fetch(:file_path) + + FrontMatterParser::Parser.parse_file(file_path) + end + + private + + def load_faqs + Dir.glob("#{PATH}/**/*.md").each_with_object({}) do |file_path, faqs_by_path| + parsed = FrontMatterParser::Parser.parse_file(file_path) + front_matter = parsed.front_matter.symbolize_keys + + faq_data = front_matter.slice(:slug, :title, :category, :subcategory, :locale, :keywords).merge(file_path: file_path) + + path = front_matter.fetch(:category) + '/' + front_matter.fetch(:slug) + faqs_by_path[path] = faq_data + end + end +end diff --git a/app/views/faq/show.html.haml b/app/views/faq/show.html.haml new file mode 100644 index 000000000..46dc34cd9 --- /dev/null +++ b/app/views/faq/show.html.haml @@ -0,0 +1,6 @@ +- content_for(:title, @metadata[:title]) + +.fr-container.fr-my-4w + .fr-grid-row + .fr-col-12.fr-col-md-8.fr-py-12v + = @renderer.render(@content).html_safe diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb index 6b101ffa2..c444fa05e 100644 --- a/config/initializers/inflections.rb +++ b/config/initializers/inflections.rb @@ -18,6 +18,8 @@ ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'URL' inflect.acronym 'SVA' inflect.acronym 'SVR' + inflect.acronym 'FAQ' + inflect.acronym 'FAQs' inflect.irregular 'type_de_champ', 'types_de_champ' inflect.irregular 'type_de_champ_private', 'types_de_champ_private' inflect.irregular 'procedure_revision_type_de_champ', 'procedure_revision_types_de_champ' diff --git a/config/locales/faqs.fr.yml b/config/locales/faqs.fr.yml new file mode 100644 index 000000000..14c82c8b9 --- /dev/null +++ b/config/locales/faqs.fr.yml @@ -0,0 +1,9 @@ +fr: + faq: + administrateur: + name: Administrateur (création d’un formulaire) + description: Informations pour les administrateurs sur la configuration de la plateforme ou la création de démarches. + subcategories: + create_procedure: + name: Je veux créer une démarche en ligne + description: Comment créer une nouvelle démarche ? diff --git a/config/routes.rb b/config/routes.rb index fb0bc9302..b5b8520d2 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -715,6 +715,8 @@ Rails.application.routes.draw do resources :release_notes, only: [:index] + get '/faq/:category/:slug', to: 'faq#show', as: :faq + get '/404', to: 'errors#not_found' get '/422', to: 'errors#unprocessable_entity' get '/500', to: 'errors#internal_server_error' diff --git a/doc/faqs/administrateur/comment-creer-ma-demarche.fr.md b/doc/faqs/administrateur/comment-creer-ma-demarche.fr.md new file mode 100644 index 000000000..7dfd16b7d --- /dev/null +++ b/doc/faqs/administrateur/comment-creer-ma-demarche.fr.md @@ -0,0 +1,16 @@ +--- +category: "administrateur" +subcategory: "create_procedure" +slug: "comment-creer-ma-demarche" +locale: "fr" +keywords: "création démarche, tutoriel, guide bonnes pratiques, administrateur" +title: "Comment créer ma démarche ?" +--- + +# Comment créer ma démarche ? + +Envie de vous lancer dans l’aventure ? Bravo ! Pour vous accompagner tout au long de la création de votre démarche, nous avons préparé un petit tutoriel spécialement pour vous : + +[Tutoriel Administrateur](https://doc.demarches-simplifiees.fr/tutoriels/tutoriel-administrateur) + +Pour compléter cette prise en main de l’outil, n’hésitez pas à consulter [notre guide de bonnes pratiques](https://456404736-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-L7_aKvpAJdAIEfxHudA%2Fuploads%2FGJm7S7LVjHPKVlMCE36e%2FGuide%20des%20bonnes%20pratiques%20démarches-simplifiees.pdf?alt=media&token=228e63c7-a168-4656-9cda-3f53a10645c2).