Class: FriendlyId::Configuration
- Inherits:
-
Object
- Object
- FriendlyId::Configuration
- 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
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
-
#allow_nil ⇒ Object
(also: #allow_nil?)
Whether to allow friendly_id and/or slugs to be nil.
-
#approximate_ascii ⇒ Object
Strip diacritics from Western characters.
-
#ascii_approximation_options ⇒ Object
Locale-type options for ASCII approximations.
-
#configured_class ⇒ Object
readonly
The class that’s using the configuration.
-
#max_length ⇒ Object
The maximum allowed length for a friendly_id string.
-
#method ⇒ Object
(also: #column)
readonly
The method or column that will be used as the basis of the friendly_id string.
-
#reserved_message ⇒ Object
The message shown when a reserved word is used.
-
#reserved_words ⇒ Object
Array of words that are reserved and can’t be used as friendly_id strings.
-
#scope ⇒ Object
The method or relation to use as the friendly_id’s scope.
-
#sequence_separator ⇒ Object
The string that separates slug names from slug sequences.
-
#strip_non_ascii ⇒ Object
Strip non-ASCII characters from the friendly_id string.
-
#use_slug ⇒ Object
(also: #use_slugs=)
Use slugs for storing the friendly_id string.
Instance Method Summary collapse
-
#initialize(configured_class, method, options = nil) {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
- #reserved?(word) ⇒ Boolean
- #reserved_error_message(word) ⇒ Object
Constructor Details
#initialize(configured_class, method, options = nil) {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
80 81 82 83 84 85 86 87 |
# File 'lib/friendly_id/configuration.rb', line 80 def initialize(configured_class, method, = nil, &block) @configured_class = configured_class @method = method.to_sym DEFAULTS.merge( || {}).each do |key, value| self.send "#{key}=".to_sym, value end yield self if block_given? end |
Instance Attribute Details
#allow_nil ⇒ Object 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_ascii ⇒ Object
Strip diacritics from Western characters.
40 41 42 |
# File 'lib/friendly_id/configuration.rb', line 40 def approximate_ascii @approximate_ascii end |
#ascii_approximation_options ⇒ Object
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 end |
#configured_class ⇒ Object (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_length ⇒ Object
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 |
#method ⇒ Object (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_message ⇒ Object
The message shown when a reserved word is used.
59 60 61 |
# File 'lib/friendly_id/configuration.rb', line 59 def @reserved_message end |
#reserved_words ⇒ Object
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 |
#scope ⇒ Object
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_separator ⇒ Object
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_ascii ⇒ Object
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_slug ⇒ Object 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
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 (word) [method, % word] if reserved? word end |