Skip to content
Snippets Groups Projects
Unverified Commit e6474424 authored by Sébastien Gaya's avatar Sébastien Gaya
Browse files

create previous links

parent 4f5cfe6a
No related branches found
No related tags found
No related merge requests found
......@@ -46,6 +46,7 @@ class Communication::Website::Category < ApplicationRecord
include WithTree
include WithPosition
include WithWebsitePermalink
include WithWebsitePreviousLinks
has_one :imported_category,
class_name: 'Communication::Website::Imported::Category',
......@@ -111,6 +112,10 @@ class Communication::Website::Category < ApplicationRecord
raw_permalink_for_website(website).gsub(':slug', self.path)
end
def previous_computed_permalink_for_website(website)
raw_permalink_for_website(website).gsub(':slug', self.path_was)
end
protected
def last_ordered_element
......
......@@ -54,6 +54,7 @@ class Communication::Website::Page < ApplicationRecord
include WithTree
include WithPath
include WithWebsitePermalink
include WithWebsitePreviousLinks
has_summernote :text
......@@ -145,6 +146,10 @@ class Communication::Website::Page < ApplicationRecord
path
end
def previous_computed_permalink_for_website(website)
path_was
end
protected
def check_accessibility
......
......@@ -44,6 +44,7 @@ class Communication::Website::Post < ApplicationRecord
include WithMenuItemTarget
include WithSlug # We override slug_unavailable? method
include WithWebsitePermalink
include WithWebsitePreviousLinks
has_summernote :text
......@@ -150,6 +151,13 @@ class Communication::Website::Post < ApplicationRecord
.gsub(':year/:month/:day', published_at.strftime("%Y/%m/%d"))
end
def previous_computed_permalink_for_website(website)
return unless website.id == communication_website_id && published_was && published_at_was
raw_permalink_for_website(website)
.gsub(':slug', self.slug_was)
.gsub(':year/:month/:day', published_at_was.strftime("%Y/%m/%d"))
end
def to_s
"#{title}"
end
......
......@@ -30,15 +30,10 @@ class Communication::Website::PreviousLink < ApplicationRecord
belongs_to :about, polymorphic: true
before_validation :set_university, on: :create
after_destroy_commit :sync_about
private
def set_university
self.university_id = website.university_id
end
def sync_about
about.sync_with_git
end
end
......@@ -8,8 +8,17 @@ module WithWebsitePermalink
computed_permalink.present? ? Static.clean_path(computed_permalink) : nil
end
def previous_permalink_for_website(website)
computed_permalink = previous_computed_permalink_for_website(website)
computed_permalink.present? ? Static.clean_path(computed_permalink) : nil
end
def computed_permalink_for_website(website)
raw_permalink_for_website(website).gsub(':slug', self.slug)
raw_permalink_for_website(website)&.gsub(':slug', self.slug)
end
def previous_computed_permalink_for_website(website)
raw_permalink_for_website(website)&.gsub(':slug', self.slug_was)
end
protected
......
module WithWebsitePreviousLinks
extend ActiveSupport::Concern
included do
has_many :previous_links,
class_name: "Communication::Website::PreviousLink",
as: :about,
dependent: :destroy
after_validation :manage_previous_links, on: [:create, :update]
def manage_previous_links
websites_for_self.each do |website|
old_permalink = previous_permalink_for_website(website)
new_permalink = permalink_for_website(website)
# If the object had a permalink and now is different, we create a previous link
previous_links.create(website: website, link: old_permalink) if old_permalink.present? && new_permalink != old_permalink
end
end
end
end
......@@ -57,6 +57,7 @@ class University::Person < ApplicationRecord
include WithRoles
include WithBlocks
include WithWebsitePermalink
include WithWebsitePreviousLinks
LIST_OF_ROLES = [
:administration,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment