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.

Examples:

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,
 :default_locale => :en
 # 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          => "--",
  :default_locale              => :en
}

Instance Attribute Summary collapse

Class Method 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:



84
85
86
87
88
89
90
91
# File 'lib/friendly_id/configuration.rb', line 84

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.



38
39
40
# File 'lib/friendly_id/configuration.rb', line 38

def allow_nil
  @allow_nil
end

#approximate_asciiObject

Strip diacritics from Western characters.



42
43
44
# File 'lib/friendly_id/configuration.rb', line 42

def approximate_ascii
  @approximate_ascii
end

#ascii_approximation_optionsObject

Locale-type options for ASCII approximations.



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

def ascii_approximation_options
  @ascii_approximation_options
end

#configured_classObject (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_localeObject

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

#localeObject

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_lengthObject

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

#methodObject (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_messageObject

The message shown when a reserved word is used.

See Also:



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

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.



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

def reserved_words
  @reserved_words
end

#scopeObject

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_separatorObject

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_asciiObject

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_slugObject 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?

Returns:

  • (Boolean)


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?

Returns:

  • (Boolean)

See Also:

  • Configuration.scoped_used=


149
150
151
# File 'lib/friendly_id/configuration.rb', line 149

def scopes_used?
  @scopes_used
end

Instance Method Details

#babosa_optionsObject



169
170
171
172
173
174
175
176
# File 'lib/friendly_id/configuration.rb', line 169

def babosa_options
  {
    :to_ascii         => strip_non_ascii?,
    :transliterate    => approximate_ascii?,
    :transliterations => ascii_approximation_options,
    :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.

Returns:

  • (Boolean)


102
103
104
# File 'lib/friendly_id/configuration.rb', line 102

def cache_column?
  false
end

#reserved?(word) ⇒ Boolean

Returns:

  • (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 reserved_error_message(word)
  [method, reserved_message % word] if reserved? word
end