Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

ruby - Using Rails 5, how can I make FriendlyId append a -"count+1" to duplicate slugs instead of a UUID?

Apparently FriendlyId has changed it's previously default method of appending a numeric sequence to duplicate slugs (which is what I want) to now use UUID:

Previous versions of FriendlyId appended a numeric sequence to make slugs unique, but this was removed to simplify using FriendlyId in concurrent code.

This functionality is not something I'm interested in at this time and would much prefer to have the original method that results in a cleaner URL. I found a similar question where someone provided the below code to override the friendlyId normalize_friendly_id method to get to the functionality I'm after, but using it results in an error (wrong number of arguments (given 1, expected 0)):

def normalize_friendly_id
  count = self.count "name = #{name}"
  super + "-" + count if name > 0
end

I attempted to "convert" this into a friendlyId "candidate" but I don't really know what I'm doing and the below doesn't work. Any thoughts on how I could tweak the name_candidate method to produce the result I'm afer?

class Folder < ApplicationRecord
  extend FriendlyId
  friendly_id :name_candidates, use: [ :slugged, :scoped ], scope: :account_id

  has_ancestry

  belongs_to :account
  has_many :notes, dependent: :destroy

  validates :name, presence: true

  # # https://stackoverflow.com/a/25380607/523051
  # # overrride friendlyId to append -number to duplicate folders instead of uuid's
  # def normalize_friendly_id
  #   count = self.count "name = #{name}"
  #   super + "-" + count if name > 0
  # end

  def name_candidates
    append_number = self.count "name = #{name}" if name > 0
    [
      :name,
      :name, append_number
    ]
  end
end

Note I am utilizing the :scoped functionality of friendlyId, so checks for existing folder names should be correctly scoped to :account_id.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

friendly_id 5 now has a slug_candidates that lets you customize the slug.

So to generate a sequential slug you could do:

friendly_id :slug_candidates, use: :slugged

def slug_candidates
  [:name, :name_and_sequence]
end

def name_and_sequence
  slug = normalize_friendly_id(name)
  sequence = Model.where("slug like '#{slug}--%'").count + 2
  "#{slug}--#{sequence}"
end

This is discussed in the following issue: https://github.com/norman/friendly_id/issues/480

According to the author, sequential slugs are bad for performance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...