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

Merge pull request #649 from noesya/fix/duplicate-permalinks

Eviter les doublons de permalinks
parents 0db6b093 57e228c5
No related branches found
No related tags found
No related merge requests found
......@@ -88,10 +88,14 @@ class Communication::Website::Permalink < ApplicationRecord
def save_if_needed
current_permalink = about.current_permalink_in_website(website)
# If the object had no permalink or if its path changed, we create a new permalink
if computed_path.present? && (current_permalink.nil? || current_permalink.path != computed_path)
self.path = computed_path
current_permalink&.update(is_current: false) if save
return unless computed_path.present? && (current_permalink.nil? || current_permalink.path != computed_path)
# If the object had no permalink or if its path changed, we create a new permalink and delete old with same path
existing_permalinks_for_path = self.class.unscoped.where(website_id: website_id, about_id: about_id, about_type: about_type, path: computed_path, is_current: false)
self.path = computed_path
if save
existing_permalinks_for_path.find_each(&:destroy)
current_permalink&.update(is_current: false)
end
end
......
class RemoveDuplicatePermalinks < ActiveRecord::Migration[7.0]
def change
attributes = [:about_id, :about_type, :website_id, :path]
duplicates_per_input = Communication::Website::Permalink.select(*attributes).group(*attributes).having("COUNT(*) > 1").size
duplicates_per_input.keys.each do |key|
about_id, about_type, website_id, path = key
permalinks = Communication::Website::Permalink.where(about_id: about_id, about_type: about_type, website_id: website_id, path: path).order(:created_at)
current_permalink = permalinks.detect(&:is_current?)
if current_permalink.present?
# Remove all non-current permalinks with this path
permalinks.where(is_current: false).each(&:destroy)
else
# Remove all permalinks with this path except the most recent one
permalinks.where.not(id: permalinks.last.id).each(&:destroy)
end
end
end
end
This diff is collapsed.
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