Class: 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.
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 => "--", :default_locale => :en }
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.
-
#default_locale ⇒ Object
Allows setting the default locale when locale column is present.
-
#locale ⇒ Object
Allows setting the default locale when locale column is present.
-
#max_length ⇒ Object
The maximum allowed byte 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.
Class Method Summary collapse
-
.locales_used? ⇒ Boolean
Are localed used by the slugs model?.
-
.scopes_used=(val) ⇒ Object
This will be set if FriendlyId’s scope feature is used in any model.
-
.scopes_used? ⇒ Boolean
Are scoped slugs being used by any model?.
Instance Method Summary collapse
- #babosa_options ⇒ Object
- #cache_column=(value) ⇒ Object
-
#cache_column? ⇒ Boolean
This should be overridden by adapters that implement caching.
-
#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.
84 85 86 87 88 89 90 91 |
# File 'lib/friendly_id/configuration.rb', line 84 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.
38 39 40 |
# File 'lib/friendly_id/configuration.rb', line 38 def allow_nil @allow_nil end |
#approximate_ascii ⇒ Object
Strip diacritics from Western characters.
42 43 44 |
# File 'lib/friendly_id/configuration.rb', line 42 def approximate_ascii @approximate_ascii end |
#ascii_approximation_options ⇒ Object
Locale-type options for ASCII approximations.
45 46 47 |
# File 'lib/friendly_id/configuration.rb', line 45 def @ascii_approximation_options end |
#configured_class ⇒ Object (readonly)
The class that’s using the configuration.
48 49 50 |
# File 'lib/friendly_id/configuration.rb', line 48 def configured_class @configured_class end |
#default_locale ⇒ Object
Allows setting the default locale when locale column is present.
82 83 84 |
# File 'lib/friendly_id/configuration.rb', line 82 def default_locale @default_locale end |
#locale ⇒ Object
Allows setting the default locale when locale column is present.
82 83 84 |
# File 'lib/friendly_id/configuration.rb', line 82 def locale @locale end |
#max_length ⇒ Object
The maximum allowed byte length for a friendly_id string. This is checked after a string is processed by FriendlyId to remove spaces, special characters, etc.
52 53 54 |
# File 'lib/friendly_id/configuration.rb', line 52 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.
55 56 57 |
# File 'lib/friendly_id/configuration.rb', line 55 def method @method end |
#reserved_message ⇒ Object
The message shown when a reserved word is used.
60 61 62 |
# File 'lib/friendly_id/configuration.rb', line 60 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.
66 67 68 |
# File 'lib/friendly_id/configuration.rb', line 66 def reserved_words @reserved_words end |
#scope ⇒ Object
The method or relation to use as the friendly_id’s scope.
69 70 71 |
# File 'lib/friendly_id/configuration.rb', line 69 def scope @scope end |
#sequence_separator ⇒ Object
The string that separates slug names from slug sequences. Defaults to “–”.
72 73 74 |
# File 'lib/friendly_id/configuration.rb', line 72 def sequence_separator @sequence_separator end |
#strip_non_ascii ⇒ Object
Strip non-ASCII characters from the friendly_id string.
75 76 77 |
# File 'lib/friendly_id/configuration.rb', line 75 def strip_non_ascii @strip_non_ascii end |
#use_slug ⇒ Object Also known as: use_slugs=
Use slugs for storing the friendly_id string.
78 79 80 |
# File 'lib/friendly_id/configuration.rb', line 78 def use_slug @use_slug end |
Class Method Details
.locales_used? ⇒ Boolean
Are localed used by the slugs model?
154 155 156 |
# File 'lib/friendly_id/configuration.rb', line 154 def locales_used? @locales_used ||= ::Slug.table_exists? && ::Slug.column_names.map(&:to_sym).include?(:locale) end |
.scopes_used=(val) ⇒ Object
This will be set if FriendlyId’s scope feature is used in any model. It is here to provide a way to avoid invoking costly scope lookup methods when the scoped slug feature is not being used by any models.
143 144 145 |
# File 'lib/friendly_id/configuration.rb', line 143 def scopes_used=(val) @scopes_used = !!val end |
.scopes_used? ⇒ Boolean
Are scoped slugs being used by any model?
149 150 151 |
# File 'lib/friendly_id/configuration.rb', line 149 def scopes_used? @scopes_used end |
Instance Method Details
#babosa_options ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'lib/friendly_id/configuration.rb', line 169 def { :to_ascii => strip_non_ascii?, :transliterate => approximate_ascii?, :transliterations => , :max_length => max_length } end |
#cache_column=(value) ⇒ Object
93 94 95 96 97 98 99 |
# File 'lib/friendly_id/configuration.rb', line 93 def cache_column=(value) @cache_column = value.to_s.strip.to_sym if value =~ /\s/ || [:slug, :slugs].include?(@cache_column) raise ArgumentError, "FriendlyId cache column can not be named '#{value}'" end @cache_column end |
#cache_column? ⇒ Boolean
This should be overridden by adapters that implement caching.
102 103 104 |
# File 'lib/friendly_id/configuration.rb', line 102 def cache_column? false end |
#reserved?(word) ⇒ Boolean
114 115 116 117 118 119 120 121 |
# File 'lib/friendly_id/configuration.rb', line 114 def reserved?(word) word = word.to_s if reserved_words.kind_of?(Regexp) reserved_words =~ word else reserved_words.include?(word) end end |
#reserved_error_message(word) ⇒ Object
123 124 125 |
# File 'lib/friendly_id/configuration.rb', line 123 def (word) [method, % word] if reserved? word end |