demarches-normaliennes/app/controllers/faq_controller.rb
Colin Darie 417cac5b83
feat(faq): dynamic substitutions support
Ce code avait été initalement codé, mais le contenu de la FAQ n'en a à
ce stade plus besoin. On le garde sous le coude car c'est peu touchy
et ça pourrait bientôt resservir.
2024-06-24 16:29:32 +02:00

48 lines
1.5 KiB
Ruby
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# frozen_string_literal: true
class FAQController < ApplicationController
before_action :load_faq_data, only: :show
def index
@faqs = loader_service.all
end
def show
@renderer = Redcarpet::Markdown.new(Redcarpet::TrustedRenderer.new(view_context), autolink: true)
@siblings = loader_service.faqs_for_category(@metadata[:category])
end
private
def loader_service
@loader_service ||= begin
substitutions = {
application_base_url: Current.application_base_url,
application_name: Current.application_name,
contact_email: Current.contact_email
}.merge(dynamic_substitutions)
FAQsLoaderService.new(substitutions)
end
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
# Hash of dynamic values used for substitutions.
# The values are fetched from or calculated into the Rails cache.
def dynamic_substitutions
{
# Example:
# procedures_count: Rails.cache.fetch("faq/procedures_count", expires_in: 1.day) { helpers.number_with_delimiter(Procedure.publiee.count, delimiter: ' ') },
}
end
end