diff --git a/app/models/communication/website/category.rb b/app/models/communication/website/category.rb index 677e7f8ccd1d4dc99eb6b25bb7880cbb92701dc2..f6d210fb52403228522b7f5ce278699b78d1bf5a 100644 --- a/app/models/communication/website/category.rb +++ b/app/models/communication/website/category.rb @@ -44,9 +44,8 @@ class Communication::Website::Category < ApplicationRecord include WithMenuItemTarget include WithSlug # We override slug_unavailable? method include WithTree + include WithPermalinkInWebsite include WithPosition - include WithWebsitePermalink - include WithWebsitePreviousLinks has_one :imported_category, class_name: 'Communication::Website::Imported::Category', diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index 3c57eba398b2eeb110d22e07ae0d45653da0339b..b07d183eb89c34fe010e4936adb79a9d68996935 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -53,8 +53,7 @@ class Communication::Website::Page < ApplicationRecord include WithPosition include WithTree include WithPath - include WithWebsitePermalink - include WithWebsitePreviousLinks + include WithPermalinkInWebsite has_summernote :text diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index 06f2653eb9b8b2ff858f1cbe02ee94df2d0ee64c..5bebc101d1336c98612e5789f4a5a2db76a3aaaf 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -42,9 +42,8 @@ class Communication::Website::Post < ApplicationRecord include WithBlobs include WithBlocks include WithMenuItemTarget + include WithPermalinkInWebsite include WithSlug # We override slug_unavailable? method - include WithWebsitePermalink - include WithWebsitePreviousLinks has_summernote :text diff --git a/app/models/concerns/with_permalink_in_website.rb b/app/models/concerns/with_permalink_in_website.rb new file mode 100644 index 0000000000000000000000000000000000000000..78e107e43d8703bd4adc831410d36ebd382bbc98 --- /dev/null +++ b/app/models/concerns/with_permalink_in_website.rb @@ -0,0 +1,52 @@ +module WithPermalinkInWebsite + 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] + + end + + def permalink_in_website(website) + computed_permalink = computed_permalink_in_website(website) + computed_permalink.present? ? Static.clean_path(computed_permalink) : nil + end + + def previous_permalink_in_website(website) + computed_permalink = previous_computed_permalink_in_website(website) + computed_permalink.present? ? Static.clean_path(computed_permalink) : nil + end + + def computed_permalink_in_website(website) + raw_permalink_in_website(website)&.gsub(':slug', self.slug) + end + + def previous_computed_permalink_in_website(website) + raw_permalink_in_website(website)&.gsub(':slug', self.slug_was) + end + + def manage_previous_links + websites_for_self.each do |website| + old_permalink = previous_permalink_in_website(website) + new_permalink = permalink_in_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 + + protected + + def raw_permalink_in_website(website) + website.config_permalinks.permalinks_data[permalink_config_key] + end + + def permalink_config_key + raise NotImplementedError + end +end diff --git a/app/models/concerns/with_website_permalink.rb b/app/models/concerns/with_website_permalink.rb deleted file mode 100644 index 69eb50b8d709bd94e140722cd32c33359ef3159b..0000000000000000000000000000000000000000 --- a/app/models/concerns/with_website_permalink.rb +++ /dev/null @@ -1,35 +0,0 @@ -module WithWebsitePermalink - extend ActiveSupport::Concern - - included do - - def permalink_in_website(website) - computed_permalink = computed_permalink_in_website(website) - computed_permalink.present? ? Static.clean_path(computed_permalink) : nil - end - - def previous_permalink_in_website(website) - computed_permalink = previous_computed_permalink_in_website(website) - computed_permalink.present? ? Static.clean_path(computed_permalink) : nil - end - - def computed_permalink_in_website(website) - raw_permalink_in_website(website)&.gsub(':slug', self.slug) - end - - def previous_computed_permalink_in_website(website) - raw_permalink_in_website(website)&.gsub(':slug', self.slug_was) - end - - protected - - def raw_permalink_in_website(website) - website.config_permalinks.permalinks_data[permalink_config_key] - end - - def permalink_config_key - raise NotImplementedError - end - - end -end diff --git a/app/models/concerns/with_website_previous_links.rb b/app/models/concerns/with_website_previous_links.rb deleted file mode 100644 index 42a343448ddd1082c221e8fa6e4302a417c34116..0000000000000000000000000000000000000000 --- a/app/models/concerns/with_website_previous_links.rb +++ /dev/null @@ -1,24 +0,0 @@ -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_in_website(website) - new_permalink = permalink_in_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 diff --git a/app/models/university/person.rb b/app/models/university/person.rb index 0585737af091341856bf573751788237518dc67d..7310fc80e5327d280ac18b7c5f735307190a9f54 100644 --- a/app/models/university/person.rb +++ b/app/models/university/person.rb @@ -56,8 +56,7 @@ class University::Person < ApplicationRecord include WithPicture include WithRoles include WithBlocks - include WithWebsitePermalink - include WithWebsitePreviousLinks + include WithPermalinkInWebsite LIST_OF_ROLES = [ :administration,