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

3

2

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://mcmap.net/q/1456542/-change-the-unique-generated-title-names-of-friendly-id
  # # 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.

Exchange answered 15/4, 2018 at 16:12 Comment(0)
W
4

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.

Woolgathering answered 4/12, 2018 at 10:34 Comment(1)
The answer by @Exchange is cleaner unless you need to customize the sequential numbering.Bingaman
E
2

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
Exchange answered 15/4, 2018 at 18:6 Comment(1)
friendly_id :name, use: :sequentially_slugged, thanks!Mier
C
0

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

Calvo answered 15/4, 2018 at 16:32 Comment(3)
thanks. still am stuck though as to where/what I pass in. if i add name as a parameter like def normalize_friendly_id(name) then i get past the wrong number of arguments error and get "undefined method `count' for #<Folder:0x00007f97fc812680>"Exchange
Note, that inside normalize_friendly_id you are scoped to the instance of Folder. Try self.class.count.Surgeonfish
@SergiiK changing the above normalize_friendly_id self.count to self.class.count generates this error: PG::UndefinedColumn: ERROR: column "test" does not exist LINE 1: SELECT COUNT(name = test) FROM "folders" ^ : SELECT COUNT(name = test) FROM "folders" with "test" being the string I input into the form that it is attempting to slugExchange

© 2022 - 2024 — McMap. All rights reserved.