Class: FriendlyId::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/friendly_id/configuration.rb

Overview

This class is not intended to be used on its own, it is used internally by ‘has_friendly_id` to store a model’s configuration and configuration-related methods.

The arguments accepted by has_friendly_id correspond to the writeable instance attributes of this class; please see the description of the attributes below for information on the possible options.

has_friendly_id :name,

:use_slug => true,
:max_length => 150,
:approximate_ascii => true,
:ascii_approximation_options => :german,
:sequence_separator => ":",
:reserved_words => ["reserved", "words"],
:scope => :country,
:cache_column => :my_cache_column_name
# etc.

Direct Known Subclasses

ActiveRecordAdapter::Configuration

Constant Summary collapse

DEFAULTS =
{
  :allow_nil                   => false,
  :ascii_approximation_options => [],
  :max_length                  => 255,
  :reserved_words              => ["index", "new"],
  :reserved_message            => 'can not be "%s"',
  :sequence_separator          => "--"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configured_class, method, options = nil) {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



80
81
82
83
84
85
86
87
# File 'lib/friendly_id/configuration.rb', line 80

def initialize(configured_class, method, options = nil, &block)
  @configured_class = configured_class
  @method = method.to_sym
  DEFAULTS.merge(options || {}).each do |key, value|
    self.send "#{key}=".to_sym, value
  end
  yield self if block_given?
end

Instance Attribute Details

#allow_nilObject Also known as: allow_nil?

Whether to allow friendly_id and/or slugs to be nil. This is not generally useful on its own, but may allow you greater flexibility to customize your application.



36
37
38
# File 'lib/friendly_id/configuration.rb', line 36

def allow_nil
  @allow_nil
end

#approximate_asciiObject

Strip diacritics from Western characters.



40
41
42
# File 'lib/friendly_id/configuration.rb', line 40

def approximate_ascii
  @approximate_ascii
end

#ascii_approximation_optionsObject

Locale-type options for ASCII approximations. These can be any of the values supported by SlugString#approximate_ascii!.



44
45
46
# File 'lib/friendly_id/configuration.rb', line 44

def ascii_approximation_options
  @ascii_approximation_options
end

#configured_classObject (readonly)

The class that’s using the configuration.



47
48
49
# File 'lib/friendly_id/configuration.rb', line 47

def configured_class
  @configured_class
end

#max_lengthObject

The maximum allowed length for a friendly_id string. This is checked after a string is processed by FriendlyId to remove spaces, special characters, etc.



51
52
53
# File 'lib/friendly_id/configuration.rb', line 51

def max_length
  @max_length
end

#methodObject (readonly) Also known as: column

The method or column that will be used as the basis of the friendly_id string.



54
55
56
# File 'lib/friendly_id/configuration.rb', line 54

def method
  @method
end

#reserved_messageObject

The message shown when a reserved word is used.

See Also:



59
60
61
# File 'lib/friendly_id/configuration.rb', line 59

def reserved_message
  @reserved_message
end

#reserved_wordsObject

Array of words that are reserved and can’t be used as friendly_id strings. If a listed word is used in a sluggable model, it will raise a FriendlyId::SlugGenerationError. For Rails applications, you are recommended to include “index” and “new”, which used as the defaults unless overridden.



65
66
67
# File 'lib/friendly_id/configuration.rb', line 65

def reserved_words
  @reserved_words
end

#scopeObject

The method or relation to use as the friendly_id’s scope.



68
69
70
# File 'lib/friendly_id/configuration.rb', line 68

def scope
  @scope
end

#sequence_separatorObject

The string that separates slug names from slug sequences. Defaults to “–”.



71
72
73
# File 'lib/friendly_id/configuration.rb', line 71

def sequence_separator
  @sequence_separator
end

#strip_non_asciiObject

Strip non-ASCII characters from the friendly_id string.



74
75
76
# File 'lib/friendly_id/configuration.rb', line 74

def strip_non_ascii
  @strip_non_ascii
end

#use_slugObject Also known as: use_slugs=

Use slugs for storing the friendly_id string.



77
78
79
# File 'lib/friendly_id/configuration.rb', line 77

def use_slug
  @use_slug
end

Instance Method Details

#reserved?(word) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/friendly_id/configuration.rb', line 93

def reserved?(word)
  reserved_words.include? word.to_s
end

#reserved_error_message(word) ⇒ Object



97
98
99
# File 'lib/friendly_id/configuration.rb', line 97

def reserved_error_message(word)
  [method, reserved_message % word] if reserved? word
end