Class: FriendlyId::Configuration

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

Overview

The configuration paramters passed to friendly_id will be stored in this object.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, values = nil) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(model_class, values = nil)
  @model_class = model_class
  @defaults    = {}
  set values
end

Instance Attribute Details

#baseObject

The base column or method used by FriendlyId as the basis of a friendly id or slug.

For models that don’t use FriendlyId::Slugged, the base is the column that is used as the FriendlyId directly. For models using FriendlyId::Slugged, the base is a column or method whose value is used as the basis of the slug.

For example, if you have a model representing blog posts and that uses slugs, you likely will want to use the “title” attribute as the base, and FriendlyId will take care of transforming the human-readable title into something suitable for use in a URL.

Examples:

class Book < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name
end

Parameters:

  • A (Symbol)

    symbol referencing a column or method in the model. This value is usually set by passing it as the first argument to friendly_id:



28
29
30
# File 'lib/friendly_id/configuration.rb', line 28

def base
  @base
end

#defaultsObject (readonly)

The default configuration options.



31
32
33
# File 'lib/friendly_id/configuration.rb', line 31

def defaults
  @defaults
end

#model_classObject

The model class that this configuration belongs to.

Returns:

  • ActiveRecord::Base



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

def model_class
  @model_class
end

Instance Method Details

#query_fieldObject

The column that FriendlyId will use to find the record when querying by friendly id.

This method is generally only used internally by FriendlyId.

Returns:

  • String



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

def query_field
  base.to_s
end

#set(values) ⇒ Object (private)



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

def set(values)
  values and values.each {|name, value| self.send "#{name}=", value}
end

#use(*modules) ⇒ Object

Lets you specify the modules to use with FriendlyId.

This method is invoked by friendly_id when passing the :use option, or when using friendly_id with a block.

Examples:

class Book < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name, :use => :slugged
end

Parameters:

  • *modules (#to_s, Module)

    Arguments should be Modules, or symbols or strings that correspond with the name of a module inside the FriendlyId namespace. By default FriendlyId provides :slugged, :history, :simple_i18n, :globalize, and :scoped.



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

def use(*modules)
  modules.to_a.flatten.compact.map do |object|
    mod = object.kind_of?(Module) ? object : FriendlyId.const_get(object.to_s.classify)
    model_class.send(:include, mod)
  end
end