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

Apparently FriendlyId changed earlier the default method of adding numeric sequence for duplicate bullet (which is what I want) to 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 prefer if the original method would result in cleaner URLs. I found a similar question where someone provided the below code to override the friendlyId method normalize_friendly_id

in order to access the functionality I am using, but throwing an error ( wrong number of arguments (given 1, expected 0)

) using it :

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

      

I tried to "convert" this to a friendly "candidate" but I really don't know what I'm doing and the following doesn't work. Any thoughts on how I could change the name_candidate method to get the result I am 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

  # # /questions/2157880/change-the-unique-generated-title-names-of-friendly-id/5864568#5864568
  # # 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 using :scoped

friendlyId functions , so checks for existing folder names should be bound correctly to :account_id

.

0


source to share


3 answers


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



you have to pass the name as a parameter when you call super

0


source


I came across this answer on another thread and since the module has since been injected into friendlyId, all I had to do was change use: :slugged

to use: sequentially_slugged

to create the functionality I was using.



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

      

0


source


Friendly_id 5 now has slug_candidates which allows customization of slug.

Thus, to create a consistent 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 next issue: https://github.com/norman/friendly_id/issues/480

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

0


source







All Articles