Class: FriendlyId::Configuration
- Inherits:
-
Object
- Object
- FriendlyId::Configuration
- Defined in:
- lib/friendly_id/configuration.rb
Overview
The configuration parameters passed to Base#friendly_id will be stored in this object.
Instance Attribute Summary (collapse)
-
- (Object) base(*value)
The base column or method used by FriendlyId as the basis of a friendly id or slug.
-
- (Object) defaults
readonly
The default configuration options.
-
- (Object) finder_methods
The module to use for finders.
-
- (Object) model_class
The model class that this configuration belongs to.
-
- (Object) modules
readonly
The modules in use.
Instance Method Summary (collapse)
- - (Object) get_module(object) private
-
- (Configuration) initialize(model_class, values = nil)
constructor
A new instance of Configuration.
-
- (Object) query_field
The column that FriendlyId will use to find the record when querying by friendly id.
- - (Object) set(values) private
-
- (Object) use(*modules)
Lets you specify the addon modules to use with FriendlyId.
-
- (Boolean) uses?(mod)
Returns whether the given module is in use.
Constructor Details
- (Configuration) initialize(model_class, values = nil)
Returns a new instance of Configuration
21 22 23 24 25 26 27 |
# File 'lib/friendly_id/configuration.rb', line 21 def initialize(model_class, values = nil) @model_class = model_class @defaults = {} @modules = [] @finder_methods = FriendlyId::FinderMethods set values end |
Instance Attribute Details
- (Object) base(*value)
The base column or method used by FriendlyId as the basis of a friendly id or slug.
For models that don't use Slugged, this is the column that is used to store the friendly id. For models using 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.
If you pass an argument, it will be used as the base. Otherwise the current value is returned.
86 87 88 89 90 91 92 |
# File 'lib/friendly_id/configuration.rb', line 86 def base(*value) if value.empty? @base else self.base = value.first end end |
- (Object) defaults (readonly)
The default configuration options.
9 10 11 |
# File 'lib/friendly_id/configuration.rb', line 9 def defaults @defaults end |
- (Object) finder_methods
The module to use for finders
19 20 21 |
# File 'lib/friendly_id/configuration.rb', line 19 def finder_methods @finder_methods end |
- (Object) model_class
The model class that this configuration belongs to.
16 17 18 |
# File 'lib/friendly_id/configuration.rb', line 16 def model_class @model_class end |
- (Object) modules (readonly)
The modules in use
12 13 14 |
# File 'lib/friendly_id/configuration.rb', line 12 def modules @modules end |
Instance Method Details
- (Object) get_module(object) (private)
96 97 98 |
# File 'lib/friendly_id/configuration.rb', line 96 def get_module(object) Module === object ? object : FriendlyId.const_get(object.to_s.titleize.camelize.gsub(/\s+/, '')) end |
- (Object) query_field
The column that FriendlyId will use to find the record when querying by friendly id.
This method is generally only used internally by FriendlyId.
63 64 65 |
# File 'lib/friendly_id/configuration.rb', line 63 def query_field base.to_s end |
- (Object) set(values) (private)
100 101 102 |
# File 'lib/friendly_id/configuration.rb', line 100 def set(values) values and values.each {|name, value| self.send "#{name}=", value} end |
- (Object) use(*modules)
Lets you specify the addon 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.
45 46 47 48 49 50 51 |
# File 'lib/friendly_id/configuration.rb', line 45 def use(*modules) modules.to_a.flatten.compact.map do |object| mod = get_module(object) mod.setup(@model_class) if mod.respond_to?(:setup) @model_class.send(:include, mod) unless uses? object end end |
- (Boolean) uses?(mod)
Returns whether the given module is in use.
54 55 56 |
# File 'lib/friendly_id/configuration.rb', line 54 def uses?(mod) @model_class < get_module(mod) end |