module FriendlyId::I18n

Public Class Methods

included(model_class) click to toggle source
# File lib/para/i18n/friendly_id.rb, line 10
def included(model_class)
  model_class.extend(ClassMethods)

  # Support for I18n finder methods on ActiveRecord::Relation of the including
  # model. Example :
  model_class.instance_eval do
    relation.class.send(:include, ClassMethods)
    if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) || ActiveRecord::VERSION::MAJOR >= 5
      model_class.send(:extend, ClassMethods)
    end
  end

  # Support for friendly finds on associations for Rails 4.0.1 and above.
  #
  # Borrowed from FriendlyId::Finders module
  #
  if ::ActiveRecord.const_defined?('AssociationRelation')
    association_relation_delegate_class = model_class.relation_delegate_class(::ActiveRecord::AssociationRelation)
    association_relation_delegate_class.send(:include, ClassMethods)
  end
end
setup(model_class) click to toggle source
# File lib/para/i18n/friendly_id.rb, line 6
def setup(model_class)
  model_class.friendly_id_config.use :slugged
end

Public Instance Methods

set_friendly_id(text, locale = nil) click to toggle source
# File lib/para/i18n/friendly_id.rb, line 33
def set_friendly_id(text, locale = nil)
  ::I18n.with_locale(locale || ::I18n.locale) do
    set_slug(normalize_friendly_id(text))
  end
end
set_slug(normalized_slug = nil) click to toggle source
# File lib/para/i18n/friendly_id.rb, line 43
def set_slug(normalized_slug = nil)
  return unless should_generate_new_friendly_id?

  candidates = FriendlyId::Candidates.new(self, normalized_slug || send(friendly_id_config.base))
  slug = slug_generator.generate(candidates) || resolve_friendly_id_conflict(candidates)
  self.slug = slug
end
should_generate_new_friendly_id?() click to toggle source
# File lib/para/i18n/friendly_id.rb, line 39
def should_generate_new_friendly_id?
  translation_for(::I18n.locale)[friendly_id_config.slug_column].blank?
end