2024-04-22 16:42:12 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class FAQsLoaderService
|
|
|
|
PATH = Rails.root.join('doc', 'faqs').freeze
|
|
|
|
ORDER = ['usager', 'instructeur', 'administrateur'].freeze
|
|
|
|
|
2024-04-16 12:24:39 +02:00
|
|
|
attr_reader :substitutions
|
|
|
|
|
|
|
|
def initialize(substitutions)
|
|
|
|
@substitutions = substitutions
|
|
|
|
|
2024-04-22 17:31:18 +02:00
|
|
|
@faqs_by_path ||= Rails.cache.fetch(["faqs_data", ApplicationVersion.current, substitutions], expires_in: 1.week) do
|
2024-04-22 16:42:12 +02:00
|
|
|
load_faqs
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(path)
|
|
|
|
file_path = @faqs_by_path.fetch(path).fetch(:file_path)
|
|
|
|
|
2024-04-16 12:24:39 +02:00
|
|
|
parse_with_substitutions(file_path)
|
2024-04-22 16:42:12 +02:00
|
|
|
end
|
|
|
|
|
2024-04-22 16:43:57 +02:00
|
|
|
def faqs_for_category(category)
|
|
|
|
@faqs_by_path.values
|
|
|
|
.filter { |faq| faq[:category] == category }
|
|
|
|
.group_by { |faq| faq[:subcategory] }
|
|
|
|
end
|
|
|
|
|
2024-04-22 16:44:27 +02:00
|
|
|
def all
|
|
|
|
@faqs_by_path.values
|
|
|
|
.group_by { |faq| faq.fetch(:category) }
|
|
|
|
.sort_by { |category, _| ORDER.index(category) || ORDER.size }
|
|
|
|
.to_h
|
|
|
|
.transform_values do |faqs|
|
|
|
|
faqs.group_by { |faq| faq.fetch(:subcategory) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-04-22 16:42:12 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def load_faqs
|
|
|
|
Dir.glob("#{PATH}/**/*.md").each_with_object({}) do |file_path, faqs_by_path|
|
2024-04-16 12:24:39 +02:00
|
|
|
parsed = parse_with_substitutions(file_path)
|
2024-04-22 16:42:12 +02:00
|
|
|
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
|
2024-04-16 12:24:39 +02:00
|
|
|
|
|
|
|
# Substitute all string before front matter parser so metadata are also substituted.
|
|
|
|
# using standard ruby formatting, ie => `%{my_var} % { my_var: 'value' }`
|
|
|
|
# We have to escape % chars not used for substitutions, ie. not preceeded by {
|
|
|
|
def parse_with_substitutions(file_path)
|
|
|
|
substituted_content = File.read(file_path).gsub(/%(?!{)/, '%%') % substitutions
|
|
|
|
|
|
|
|
FrontMatterParser::Parser.new(:md).call(substituted_content)
|
|
|
|
end
|
2024-04-22 16:42:12 +02:00
|
|
|
end
|