Code Monkey home page Code Monkey logo

Comments (1)

mreq avatar mreq commented on June 11, 2024

Here's a concern implementing b) https://github.com/sinfin/folio/blob/master/test/models/concerns/folio/friendly_id/history_test.rb

module Folio::FriendlyId::History
  extend ActiveSupport::Concern

  included do
    extend FriendlyId

    before_save :remove_conflicting_history_slugs
  end

  private
    def remove_conflicting_history_slugs
      if slug.present?
        scope_names = if self.class.friendly_id_config.uses?(:scoped)
          self.class.friendly_id_config.scope_columns.sort.map { |column| "#{column}:#{send(column)}" }.join(",")
        else
          nil
        end

        existing_scope = FriendlyId::Slug.where(sluggable_type: self.class.base_class.to_s,
                                                slug:,
                                                scope: scope_names)
                                         .where.not(sluggable_id: id)

        existing_scope.each do |slug|
          last_slug_for_record = FriendlyId::Slug.where(sluggable_type: slug.sluggable_type,
                                                        sluggable_id: slug.sluggable_id,
                                                        scope: slug.scope)
                                                 .order(id: :asc)
                                                 .last

          slug.destroy if slug.id != last_slug_for_record.id
        end
      end
    end
end

and a simple test showcasing the fix https://github.com/sinfin/folio/blob/master/test/models/concerns/folio/friendly_id/history_test.rb

# frozen_string_literal: true

require "test_helper"

class Folio::FriendlyId::HistoryTest < ActiveSupport::TestCase
  test "remove_conflicting_history_slugs" do
    page = create(:folio_page, slug: "foo")
    page.update!(slug: "bar")

    assert FriendlyId::Slug.exists?(slug: "foo",
                                    sluggable_id: page.id,
                                    sluggable_type: "Folio::Page",
                                    scope: "site_id:")

    assert FriendlyId::Slug.exists?(slug: "bar",
                                    sluggable_id: page.id,
                                    sluggable_type: "Folio::Page",
                                    scope: "site_id:")

    # the following line fails without the fix
    new_page = create(:folio_page, slug: "foo")

    assert FriendlyId::Slug.exists?(slug: "foo",
                                    sluggable_id: new_page.id,
                                    sluggable_type: "Folio::Page",
                                    scope: "site_id:")

    assert_not FriendlyId::Slug.exists?(slug: "foo",
                                        sluggable_id: page.id,
                                        sluggable_type: "Folio::Page",
                                        scope: "site_id:")

    assert FriendlyId::Slug.exists?(slug: "bar",
                                    sluggable_id: page.id,
                                    sluggable_type: "Folio::Page",
                                    scope: "site_id:")
  end
end

from friendly_id.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.