1

Out of the box, Friendly_ID adds "--2", "--3"... to the end of the slug when an similar entry exists.

I see where I can change the "sequence_separator" but am having having trouble finding what I would call the "sequencer." My posts will only duplicate once per year, so I would like to add the year instead of a random number at the end.

Update 1 Yannick,thanks for pointing me in the right direction. Did some research on that method and found documentation and a good question to help me put this together.

  extend FriendlyId
  friendly_id :race_name_and_year, use: :slugged
  before_validation :race_name_and_year

  def resolve_friendly_id_conflict(candidates)
    candidates.first + friendly_id_config.sequence_separator + SecureRandom.uuid
  end

  def normalize_friendly_id(value)
    value.to_s.parameterize
  end

  def should_generate_new_friendly_id?
    slug.blank?
  end  

  def race_name_and_year
    self.slug ||= normalize_friendly_id("#{start.year}-#{name}")
  end

While this works, it seems pretty complicated and messy. It also doesn't generate a slug when I add records (soon to upload) through Active Admin. I'm thinking it might just be best to leave the record ID in the URL and add text at the end that isn't actually used to find the record.

timroman
  • 1,384
  • 1
  • 10
  • 18

1 Answers1

1

It seems to not be possible by configuration. Now it uses uuid in fact to facilitate the sequence generation.

You might try a monkey patch like this one

module FriendlyId
  module Slugged

    def resolve_friendly_id_conflict(candidates)
      candidates.first + friendly_id_config.sequence_separator + Date.today.year
    end
  end
end

Untested but seems to be at the right place in last version Which version are you using?

yannick
  • 661
  • 4
  • 9