norman / friendly_id

FriendlyId is the “Swiss Army bulldozer” of slugging and permalink plugins for ActiveRecord. It allows you to create pretty URL’s and work with human-friendly strings as if they were numeric ids for ActiveRecord models.
http://norman.github.io/friendly_id/
MIT License
6.13k stars 589 forks source link

normalize_friendly_id not working in a concern #1003

Closed alienxp03 closed 1 year ago

alienxp03 commented 1 year ago

Related to previous issue, but in a different way. Couldn't find a workaround yet at the moment. https://github.com/norman/friendly_id/issues/825

module Sluggable
  extend ActiveSupport::Concern

  included do
    extend FriendlyId
    friendly_id :custom_method
  end

  def custom_method
    SecureRandom.urlsafe_base64(12)
  end

  def normalize_friendly_id(string)
    super
  end
end

For this code, custom_method is being called, but normalize_friendly_id is not.

In the previous issue, the workaround works because they're not using any custom method, or rather have the same candidate column name for each of their class. In this case, I couldn't use friendly_id :name, use: :slugged since not all of my tables have the column name.

I've to use friendly_id :id, use: :slugged since id exist in all tables but doesn't seems to be working as well.

Using friendly_id 5.5.0, Ruby 3.2.0, Rails 7.0.4

clairity commented 1 year ago

you should stick the normalize_friendly_id method inside your concern's included block so that it runs in the context of the model, not the concern.

alienxp03 commented 1 year ago

Thanks. Overthinking and overlooked that part. Thanks for your help!