Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app/services/osuny/category/optimizer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Charge toutes les catégories d'une université en une seule requête et remonte
# la chaîne de parents en mémoire, pour afficher un objet categorizable groupé
# par taxonomie sans le N+1 de la récursion `descendants_and_self`.
module Osuny
module Category
class Optimizer
def initialize(about)
@about = about
end

# { taxonomie => [catégories de about rattachées à cette taxonomie] },
# ordonné, limité aux taxonomies qui ont au moins une catégorie utilisée.
#
# Exemple : { #<Taxonomy "Pays"> => [#<Category "France">, #<Category "Italie">] }
def groups_by_taxonomy
@groups_by_taxonomy ||= taxonomies.each_with_object({}) do |taxonomy, hash|
used = about_categories_by_root[taxonomy]
hash[taxonomy] = used if used.present?
end
end

# Catégories de about qui sont elles-mêmes racines et non-taxonomies.
#
# Exemple : [#<Category "Actualité">, #<Category "Événement">]
def free_categories
@free_categories ||= about_categories.select { |c| c.parent_id.nil? && !c.is_taxonomy }
end

protected

def about_categories
@about_categories ||= @about.categories.includes(:localizations).to_a
end

def categories_by_id
@categories_by_id ||= categories_class.where(university: university)
.includes(:localizations)
.index_by(&:id)
end

def categories_class
@categories_class ||= @about.categories.klass
end

def university
@university ||= @about.university
end

def taxonomies
@taxonomies ||= categories_by_id.values
.select { |c| c.parent_id.nil? && c.is_taxonomy }
.sort_by(&:position)
end

def about_categories_by_root
@about_categories_by_root ||= about_categories.group_by { |c| root_of(c) }
end

def root_of(category)
current = category
current = categories_by_id[current.parent_id] while current && current.parent_id
current
end
end
end
end
19 changes: 4 additions & 15 deletions app/views/admin/application/categories/widget/_show.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
<%
categories_class = about.categories.klass
categories_in_university = categories_class.where(university: current_university)
taxonomies = categories_in_university.taxonomies.ordered
free_categories = categories_in_university.free
free_categories_in_use = about.categories.merge(free_categories)
optimizer = Osuny::Category::Optimizer.new(about)
columns ||= 1
classes = "col-lg-#{12/columns}"
# TODO manage child free categories
%>
<div class="row">
<%
# First, taxonomies
taxonomies.each do |taxonomy|
taxons_in_use = about.categories.merge(taxonomy.descendants_and_self)
next if taxons_in_use.none?
%>
<% optimizer.groups_by_taxonomy.each do |taxonomy, taxons_in_use| %>
<div class="<%= classes %> mb-4">
<%= osuny_label taxonomy.to_s_in(current_language) %>
<ul class="list-unstyled">
Expand All @@ -24,13 +15,11 @@ taxonomies.each do |taxonomy|
</ul>
</div>
<% end %>
<%
# Then, free categories
if free_categories_in_use.any? %>
<% if optimizer.free_categories.any? %>
<div class="<%= classes %> mb-4">
<%= osuny_label t('category.title') %>
<ul class="list-unstyled">
<% free_categories_in_use.each do |category| %>
<% optimizer.free_categories.each do |category| %>
<li><%= osuny_link_localized_if can?(:read, category), category, [:admin, category] %></li>
<% end %>
</ul>
Expand Down
11 changes: 11 additions & 0 deletions config/initializers/bugsnag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,15 @@
next unless user_agent.include?('ChatGPT-User')
false
end)

config.add_on_error(proc do |event|
next unless event.metadata.key?(:active_job)
ignored_error_classes = [
"GoodJob::ActiveJobExtensions::Concurrency::ConcurrencyExceededError",
"Communication::Website::LockError"
]
error_class = event.exceptions.first[:errorClass]
next unless ignored_error_classes.include?(error_class)
false
end)
end
Loading