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

a 夏天 提交于 2020-01-06 07:25:35

问题


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.


回答1:


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.




回答2:


Because normalize_friendly_id takes a parameter : http://www.rubydoc.info/github/norman/friendly_id/FriendlyId%2FSlugged%3Anormalize_friendly_id

you must pass the name as a parameter when you call the super




回答3:


I came across this answer in a different thread and, because the module has since been implemented into friendlyId, all I had to do was change the use: :slugged to use: sequentially_slugged to produce the functionality i was after.

class Folder < ApplicationRecord
  extend FriendlyId
  friendly_id :name, use: [ :sequentially_slugged, :scoped ], scope: :account_id
end


来源:https://stackoverflow.com/questions/49844078/using-rails-5-how-can-i-make-friendlyid-append-a-count1-to-duplicate-slugs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!