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
 # etc.

Direct Known Subclasses

ActiveRecordAdapter::Configuration

Constant Summary

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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Returns a new instance of Configuration

Yields:

  • (_self)

Yield Parameters:



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

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

- (Object) allow_nil Also known as: allow_nil?

Whether to allow friendly_id and/or slugs to be nil. If this is true then blank slugs will automatically be converted to nil, allowing for item names that lack sluggable characters.



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

def allow_nil
  @allow_nil
end

- (Object) approximate_ascii

Strip diacritics from Western characters.



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

def approximate_ascii
  @approximate_ascii
end

- (Object) ascii_approximation_options

Locale-type options for ASCII approximations.



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

def ascii_approximation_options
  @ascii_approximation_options
end

- (Object) configured_class (readonly)

The class that's using the configuration.



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

def configured_class
  @configured_class
end

- (Object) max_length

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.



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

def max_length
  @max_length
end

- (Object) method (readonly) Also known as: column

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



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

def method
  @method
end

- (Object) reserved_message

The message shown when a reserved word is used.

See Also:



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

def reserved_message
  @reserved_message
end

- (Object) reserved_words

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.



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

def reserved_words
  @reserved_words
end

- (Object) scope

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



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

def scope
  @scope
end

- (Object) sequence_separator

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



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

def sequence_separator
  @sequence_separator
end

- (Object) strip_non_ascii

Strip non-ASCII characters from the friendly_id string.



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

def strip_non_ascii
  @strip_non_ascii
end

- (Object) use_slug Also known as: use_slugs=

Use slugs for storing the friendly_id string.



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

def use_slug
  @use_slug
end

Class Method Details

+ (Object) scopes_used=(val)

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.



137
138
139
# File 'lib/friendly_id/configuration.rb', line 137

def self.scopes_used=(val)
  @scopes_used = !!val
end

+ (Boolean) scopes_used?

Are scoped slugs being used by any model?

Returns:

  • (Boolean)

See Also:

  • Configuration.scoped_used=


143
144
145
# File 'lib/friendly_id/configuration.rb', line 143

def self.scopes_used?
  @scopes_used
end

Instance Method Details

- (Object) babosa_options



157
158
159
160
161
162
163
164
# File 'lib/friendly_id/configuration.rb', line 157

def babosa_options
  {
    :to_ascii         => strip_non_ascii?,
    :transliterate    => approximate_ascii?,
    :transliterations => ascii_approximation_options,
    :max_length       => max_length
  }
end

- (Object) cache_column=(value)



88
89
90
91
92
93
94
# File 'lib/friendly_id/configuration.rb', line 88

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

- (Boolean) cache_column?

This should be overridden by adapters that implement caching.

Returns:

  • (Boolean)


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

def cache_column?
  false
end

- (Boolean) reserved?(word)

Returns:

  • (Boolean)


109
110
111
112
113
114
115
116
# File 'lib/friendly_id/configuration.rb', line 109

def reserved?(word)
  word = word.to_s
  if reserved_words.kind_of?(Regexp)
    reserved_words =~ word
  else
    reserved_words.include?(word)
  end
end

- (Object) reserved_error_message(word)



118
119
120
# File 'lib/friendly_id/configuration.rb', line 118

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