Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auto generate agenda categories from programs #1684

Merged
merged 7 commits into from
Mar 4, 2024
Merged
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
16 changes: 16 additions & 0 deletions app/models/communication/website/agenda/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# id :uuid not null, primary key
# featured_image_alt :string
# featured_image_credit :text
# is_programs_root :boolean default(FALSE)
# meta_description :text
# name :string
# path :string
Expand All @@ -17,6 +18,7 @@
# language_id :uuid not null, indexed
# original_id :uuid indexed
# parent_id :uuid indexed
# program_id :uuid indexed
# university_id :uuid not null, indexed
#
# Indexes
Expand All @@ -25,13 +27,15 @@
# index_communication_website_agenda_categories_on_language_id (language_id)
# index_communication_website_agenda_categories_on_original_id (original_id)
# index_communication_website_agenda_categories_on_parent_id (parent_id)
# index_communication_website_agenda_categories_on_program_id (program_id)
# index_communication_website_agenda_categories_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_1e1b9fbf33 (original_id => communication_website_agenda_categories.id)
# fk_rails_692dbf7723 (parent_id => communication_website_agenda_categories.id)
# fk_rails_6cb9a4b8a1 (university_id => universities.id)
# fk_rails_6cd2d2d97e (program_id => education_programs.id)
# fk_rails_7b5ad84dda (communication_website_id => communication_websites.id)
# fk_rails_b0ddee638d (language_id => languages.id)
#
Expand All @@ -47,11 +51,15 @@ class Communication::Website::Agenda::Category < ApplicationRecord
include WithPermalink
include WithPosition
include WithTranslations
include WithTree
include WithUniversity

belongs_to :parent,
class_name: 'Communication::Website::Agenda::Category',
optional: true
belongs_to :program,
class_name: 'Education::Program',
optional: true
has_many :children,
class_name: 'Communication::Website::Agenda::Category',
foreign_key: :parent_id,
Expand Down Expand Up @@ -88,8 +96,16 @@ def references
references
end

def siblings
self.class.unscoped.where(parent: parent, university: university, website: website).where.not(id: id)
end

protected

def last_ordered_element
website.agenda_categories.where(parent_id: parent_id, language_id: language_id).ordered.last
end

def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, language_id: language_id, slug: slug).where.not(id: self.id).exists?
end
Expand Down
22 changes: 14 additions & 8 deletions app/models/communication/website/with_program_categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,27 @@ module Communication::Website::WithProgramCategories
# TODO : I18n
# Actuellement, on ne crée que dans la langue par défaut du website, on ne gère pas les autres langues
def set_programs_categories!
programs_root_category = post_categories.for_language_id(default_language_id).where(is_programs_root: true).first_or_create(
[post_categories, agenda_categories].each do |objects|
programs_root_category = set_root_programs_categories_for!(objects)
set_programs_categories_at_level_for! objects, programs_root_category, education_programs.root.ordered
end
rescue
end

protected

def set_root_programs_categories_for!(objects)
objects.for_language_id(default_language_id).where(is_programs_root: true).first_or_create(
name: 'Offre de formation',
slug: 'offre-de-formation',
is_programs_root: true,
university_id: university.id
)
set_programs_categories_at_level! programs_root_category, education_programs.root.ordered
rescue
end

protected

def set_programs_categories_at_level!(parent_category, programs)
def set_programs_categories_at_level_for!(objects, parent_category, programs)
programs.map.with_index do |program, index|
category = post_categories.for_language_id(default_language_id).where(program_id: program.id).first_or_initialize(
category = objects.for_language_id(default_language_id).where(program_id: program.id).first_or_initialize(
name: program.name,
slug: program.name.parameterize,
university_id: university.id
Expand All @@ -31,7 +37,7 @@ def set_programs_categories_at_level!(parent_category, programs)
category.position = index + 1
category.save
children = education_programs.where(parent_id: program.id).ordered
set_programs_categories_at_level! category, children
set_programs_categories_at_level_for! objects, category, children
end
end
end
6 changes: 5 additions & 1 deletion app/models/education/program/with_websites_categories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ module Education::Program::WithWebsitesCategories
included do
after_save_commit :set_websites_categories

has_many :website_categories,
has_many :website_post_categories,
class_name: 'Communication::Website::Post::Category',
dependent: :destroy

has_many :website_agenda_categories,
class_name: 'Communication::Website::Agenda::Category',
dependent: :destroy
end

def set_websites_categories
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddIsProgramRootToAgendaCategories < ActiveRecord::Migration[7.1]
def change
add_column :communication_website_agenda_categories, :is_programs_root, :boolean, default: false
add_reference :communication_website_agenda_categories, :program, foreign_key: {to_table: :education_programs}, type: :uuid
end
end
10 changes: 7 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion test/models/communication/website/dependency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ def test_change_website_dependencies
# En ajoutant l'école, on rajoute en dépendances :
# - L'école, ses formations, diplômes et sites en cascade (4)
# - Les catégories d'actus liés aux formations, soit la catégorie racine et la catégorie de default_program (2)
# - Les catégories d'agenda liés aux formations, soit la catégorie racine et la catégorie de default_program (2)
# - Les pages "Teachers", "Administrators", "Researchers", "EducationDiplomas", "EducationPrograms" (5)
# Donc un total de 3 + 2 + 5 = 10 dépendances
assert_equal 11, delta
assert_equal 13, delta

Delayed::Job.destroy_all

Expand Down
Loading