Module: FriendlyId

Included in:
ActiveRecord::Base
Defined in:
lib/friendly_id.rb,
lib/friendly_id/tasks.rb,
lib/friendly_id/helpers.rb,
lib/friendly_id/version.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Helpers, NonSluggableClassMethods, NonSluggableInstanceMethods, SluggableClassMethods, SluggableInstanceMethods, Version Classes: SlugGenerationError, Tasks

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for has_friendly_id.

{
  :max_length       => 255,
  :reserved         => ["new", "index"],
  :reserved_message => 'can not be "%s"'
}.freeze
VALID_OPTIONS =

The names of all valid configuration options.

(DEFAULT_OPTIONS.keys + [
  :cache_column,
  :scope,
  :strip_diacritics,
  :strip_non_ascii,
  :use_slug
]).freeze

Instance Method Summary collapse

Instance Method Details

#has_friendly_id(method, options = {}, &block) ⇒ Object

Set up an ActiveRecord model to use a friendly_id.

The column argument can be one of your model's columns, or a method you use to generate the slug.

Options:

  • :use_slug - Defaults to nil. Use slugs when you want to use a non-unique text field for friendly ids.
  • :max_length - Defaults to 255. The maximum allowed length for a slug.
  • :cache_column - Defaults to nil. Use this column as a cache for generating to_param (experimental) Note that if you use this option, any calls to attr_accessible must be made BEFORE any calls to has_friendly_id in your class.
  • :strip_diacritics - Defaults to nil. If true, it will remove accents, umlauts, etc. from western characters.
  • :strip_non_ascii - Defaults to nil. If true, it will remove all non-ASCII characters.
  • :reserved - Array of words that are reserved and can't be used as friendly_id's. For sluggable models, if such a word is used, it will raise a FriendlyId::SlugGenerationError. Defaults to ["new", "index"].
  • :reserved_message - The validation message that will be shown when a reserved word is used as a frindly_id. Defaults to '"%s" is reserved'.

You can also optionally pass a block if you want to use your own custom slug normalization routines rather than the default ones that come with friendly_id:

require "stringex"
class Post < ActiveRecord::Base
has_friendly_id :title, :use_slug => true do |text|
  # Use stringex to generate the friendly_id rather than the baked-in methods
  text.to_url
end
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/friendly_id.rb', line 64

def has_friendly_id(method, options = {}, &block)
  options.assert_valid_keys VALID_OPTIONS
  options = DEFAULT_OPTIONS.merge(options).merge(:method => method)
  write_inheritable_attribute :friendly_id_options, options
  class_inheritable_accessor :friendly_id_options
  class_inheritable_reader :slug_normalizer_block
  write_inheritable_attribute(:slug_normalizer_block, block) if block_given?
  if friendly_id_options[:use_slug]
    extend SluggableClassMethods
    include SluggableInstanceMethods
  else
    extend NonSluggableClassMethods
    include NonSluggableInstanceMethods
  end
end