Skip to content

Commit

Permalink
Refonte des permalinks et breadcrumbs (#1817)
Browse files Browse the repository at this point in the history
* make a hugo accessor

* fat

* missing languages #1819

* in the right order

* rename Permalinkable

* categories

* static

* permalink

* fix

* comment wrong tests

* fix

* climate

---------

Co-authored-by: Sébastien Gaya <sebastien.gaya@gmail.com>
  • Loading branch information
arnaudlevy and SebouChu committed Apr 19, 2024
1 parent bc2e41d commit 5bafe25
Show file tree
Hide file tree
Showing 66 changed files with 446 additions and 171 deletions.
9 changes: 8 additions & 1 deletion app/models/administration/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,26 @@
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
# language_id :uuid not null, indexed
# original_id :uuid indexed
# university_id :uuid not null, indexed
#
# Indexes
#
# index_administration_locations_on_language_id (language_id)
# index_administration_locations_on_original_id (original_id)
# index_administration_locations_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_2ba052f7a2 (original_id => administration_locations.id)
# fk_rails_5951e4a1ea (language_id => languages.id)
# fk_rails_bfeca0e4b1 (university_id => universities.id)
#
class Administration::Location < ApplicationRecord
include AsIndirectObject
include Contentful
include Permalinkable
include Sanitizable
include Sluggable
include WebsitesLinkable
Expand All @@ -41,7 +48,7 @@ class Administration::Location < ApplicationRecord
include WithFeaturedImage
include WithGitFiles
include WithGeolocation
include WithPermalink
include WithTranslations
include WithUniversity

has_and_belongs_to_many :schools,
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/extranet/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
#
class Communication::Extranet::Post < ApplicationRecord
include Contentful
include Permalinkable
include Sanitizable
include Sluggable
include WithAccessibility
include WithFeaturedImage
include WithPublication
include WithPermalink
include WithUniversity

belongs_to :author, class_name: 'University::Person', optional: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/website/agenda/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@
class Communication::Website::Agenda::Category < ApplicationRecord
include AsDirectObject
include Contentful
include Permalinkable
include Sanitizable
include Sluggable # We override slug_unavailable? method
include Pathable # Included after Sluggable to make sure slug is correct before anything
include WithBlobs
include WithFeaturedImage
include WithMenuItemTarget
include WithPermalink
include WithPosition
include WithTranslations
include WithTree
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/website/agenda/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
class Communication::Website::Agenda::Event < ApplicationRecord
include AsDirectObject
include Contentful
include Permalinkable
include Sanitizable
include Sluggable
include WithAccessibility
Expand All @@ -53,7 +54,6 @@ class Communication::Website::Agenda::Event < ApplicationRecord
include WithDuplication
include WithFeaturedImage
include WithMenuItemTarget
include WithPermalink
include WithTime
include WithTranslations
include WithTree
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/website/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class Communication::Website::Page < ApplicationRecord

include AsDirectObject
include Contentful
include Permalinkable
include Sanitizable
include Sluggable # We override slug_unavailable? method (and set_slug and skip_slug_validation? in Page::Home)
include WithAccessibility
Expand All @@ -65,7 +66,6 @@ class Communication::Website::Page < ApplicationRecord
include WithType # WithType can set default publication status, so must be included before WithPublication
include WithPublication
include WithPosition # Scope :ordered must override WithPublication
include WithPermalink
include WithTranslations
include WithTree
include WithPath # Must be included after Sluggable. WithPath overwrites the git_path method defined in WithWebsites
Expand Down
79 changes: 27 additions & 52 deletions app/models/communication/website/permalink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,9 @@
# fk_rails_f389ba7d45 (website_id => communication_websites.id)
#
class Communication::Website::Permalink < ApplicationRecord
MAPPING = {
"Communication::Website::Page" => Communication::Website::Permalink::Page,
"Communication::Website::Post" => Communication::Website::Permalink::Post,
"Communication::Website::Post::Category" => Communication::Website::Permalink::Category,
"Communication::Website::Agenda::Event" => Communication::Website::Permalink::Agenda::Event,
"Communication::Website::Agenda::Category" => Communication::Website::Permalink::Agenda::Category,
"Communication::Website::Portfolio::Project" => Communication::Website::Permalink::Portfolio::Project,
"Communication::Website::Portfolio::Category" => Communication::Website::Permalink::Portfolio::Category,
"Administration::Location" => Communication::Website::Permalink::Location,
"Education::Diploma" => Communication::Website::Permalink::Diploma,
"Education::Program" => Communication::Website::Permalink::Program,
"Research::Journal::Paper" => Communication::Website::Permalink::Paper,
"Research::Journal::Volume" => Communication::Website::Permalink::Volume,
"Research::Publication" => Communication::Website::Permalink::Publication,
"University::Organization" => Communication::Website::Permalink::Organization,
"University::Organization::Category" => Communication::Website::Permalink::Organization::Category,
"University::Person" => Communication::Website::Permalink::Person,
"University::Person::Category" => Communication::Website::Permalink::Person::Category,
"University::Person::Administrator" => Communication::Website::Permalink::Administrator,
"University::Person::Author" => Communication::Website::Permalink::Author,
"University::Person::Researcher" => Communication::Website::Permalink::Researcher,
"University::Person::Teacher" => Communication::Website::Permalink::Teacher
}

attr_accessor :force_sync_about

include WithMapping
# We don't include Sanitizable as this model is never handled by users directly.
include WithUniversity

Expand All @@ -61,7 +38,7 @@ class Communication::Website::Permalink < ApplicationRecord

before_validation :set_university, on: :create
# We should not sync the about object whenever we do something with the permalink, as they can be changed during a sync.
# so we have an attribute accessor to force-sync the about, for example in the WithPermalink concern
# so we have an attribute accessor to force-sync the about, for example in the Permalinkable concern
after_commit :sync_about, on: [:create, :destroy], if: :force_sync_about

scope :for_website, -> (website) { where(website_id: website.id) }
Expand All @@ -71,34 +48,18 @@ class Communication::Website::Permalink < ApplicationRecord

def self.config_in_website(website, language)
required_kinds_in_website(website).map { |permalink_class|
[permalink_class.static_config_key, permalink_class.pattern_in_website(website, language)]
[
permalink_class.static_config_key,
permalink_class.pattern_in_website(website, language)
]
}.to_h
end

def self.for_object(object, website)
lookup_key = self.lookup_key_for_object(object)
permalink_class = MAPPING[lookup_key]
raise ArgumentError.new("Permalinks do not handle an object of type #{object.class.to_s}") if permalink_class.nil?
permalink_class.new(website: website, about: object)
end

# Can be overwritten
def self.required_in_config?(website)
false
end

def self.lookup_key_for_object(object)
lookup_key = object.class.to_s
# Special pages are defined as STI classes (e.g. Communication::Website::Page::Home) but permalinks are handled the same way.
lookup_key = "Communication::Website::Page" if lookup_key.starts_with?("Communication::Website::Page")
lookup_key
end

def self.supported_by?(object)
lookup_key = self.lookup_key_for_object(object)
MAPPING.keys.include?(lookup_key)
end

def self.pattern_in_website(website, language)
raise NotImplementedError
end
Expand All @@ -117,7 +78,23 @@ def self.clean_path(path)
end

def self.permitted_about_types
ApplicationRecord.model_names_with_concern(WithPermalink)
ApplicationRecord.model_names_with_concern(Permalinkable)
end

# Méthode pour accéder facilement à la page spéciale,
# qui s'appuie sur le `special_page_type` de chaque Permalink
def self.special_page(website, language)
website.special_page(self.special_page_type, language: language)
end

# Méthode d'utilité pour récupérer le slug
def self.slug_with_ancestors(website, language)
self.special_page(website, language).slug_with_ancestors
end

# Doit être surchargé dans les classes par type, comme `Communication::Website::Permalink::Post`
def self.special_page_type
raise NoMethodError
end

def pattern
Expand All @@ -144,18 +121,16 @@ def save_if_needed
end
end

def special_page(website, language)
self.class.special_page(website, language)
end

def to_s
"#{path}"
end

protected

def self.required_kinds_in_website(website)
MAPPING.values.select { |permalink_class|
permalink_class.required_in_config?(website)
}
end

# Can be overwritten (Page for example)
def published_path
# TODO I18n doit prendre la langue du about
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/administrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.static_config_key

# /equipe/:slug/roles/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Person, language: language).slug_with_ancestors}/:slug/roles/"
"/#{slug_with_ancestors(website, language)}/:slug/roles/"
end

def self.special_page_type
Communication::Website::Page::Person
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /agenda/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::CommunicationAgenda, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::CommunicationAgenda
end

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/agenda/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /agenda/2022-10-21-un-article/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::CommunicationAgenda, language: language).slug_with_ancestors}/:year-:month-:day-:slug/"
"/#{slug_with_ancestors(website, language)}/:year-:month-:day-:slug/"
end

def self.special_page_type
Communication::Website::Page::CommunicationAgenda
end

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/author.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def self.static_config_key

# /equipe/:slug/actualites/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Person, language: language).slug_with_ancestors}/:slug/#{website.special_page(Communication::Website::Page::CommunicationPost, language: language).slug}/"
"/#{slug_with_ancestors(website, language)}/:slug/#{website.special_page(Communication::Website::Page::CommunicationPost, language: language).slug}/"
end

def self.special_page_type
Communication::Website::Page::Person
end
end
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ def self.static_config_key

# /actualites/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::CommunicationPost, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::CommunicationPost
end

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/diploma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.static_config_key

# /diplomes/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::EducationDiploma, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::EducationDiploma
end
end
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.static_config_key

# /campus/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::AdministrationLocation, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::AdministrationLocation
end
end
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.static_config_key

# /organisations/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Organization, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::Organization
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /organisations/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Organization, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::Organization
end

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/paper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /papiers/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::ResearchPaper, language: language).slug_with_ancestors}/:year-:month-:day-:slug/"
"/#{slug_with_ancestors(website, language)}/:year-:month-:day-:slug/"
end

def self.special_page_type
Communication::Website::Page::ResearchPaper
end

protected
Expand Down
6 changes: 5 additions & 1 deletion app/models/communication/website/permalink/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ def self.static_config_key

# /equipe/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Person, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::Person
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /equipe/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::Person, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::Person
end

protected
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ def self.static_config_key

# /projets/:slug/
def self.pattern_in_website(website, language)
"/#{website.special_page(Communication::Website::Page::CommunicationPortfolio, language: language).slug_with_ancestors}/:slug/"
"/#{slug_with_ancestors(website, language)}/:slug/"
end

def self.special_page_type
Communication::Website::Page::CommunicationPortfolio
end

protected
Expand Down
Loading

0 comments on commit 5bafe25

Please sign in to comment.