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
-
#base(*value) ⇒ Object
The base column or method used by FriendlyId as the basis of a friendly id or slug.
-
#defaults ⇒ Object
readonly
The default configuration options.
-
#dependent ⇒ Object
The value used for the slugged association's dependent option.
-
#finder_methods ⇒ Object
The module to use for finders.
-
#model_class ⇒ Object
The model class that this configuration belongs to.
-
#modules ⇒ Object
readonly
The modules in use.
-
#routes ⇒ Object
Route generation preferences.
Instance Method Summary collapse
- #get_module(object) ⇒ Object private
-
#initialize(model_class, values = nil) ⇒ Configuration
constructor
A new instance of Configuration.
-
#query_field ⇒ Object
The column that FriendlyId will use to find the record when querying by friendly id.
- #set(values) ⇒ Object private
-
#use(*modules) ⇒ Object
Lets you specify the addon modules to use with FriendlyId.
-
#uses?(mod) ⇒ Boolean
Returns whether the given module is in use.
Constructor Details
#initialize(model_class, values = nil) ⇒ Configuration
Returns a new instance of Configuration.
26 27 28 29 30 31 32 33 34 |
# File 'lib/friendly_id/configuration.rb', line 26 def initialize(model_class, values = nil) @base = nil @model_class = model_class @defaults = {} @modules = [] @finder_methods = FriendlyId::FinderMethods self.routes = :friendly set values end |
Instance Attribute Details
#base(*value) ⇒ Object
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.
93 94 95 96 97 98 99 |
# File 'lib/friendly_id/configuration.rb', line 93 def base(*value) if value.empty? @base else self.base = value.first end end |
#defaults ⇒ Object (readonly)
The default configuration options.
8 9 10 |
# File 'lib/friendly_id/configuration.rb', line 8 def defaults @defaults end |
#dependent ⇒ Object
The value used for the slugged association's dependent option
21 22 23 |
# File 'lib/friendly_id/configuration.rb', line 21 def dependent @dependent end |
#finder_methods ⇒ Object
The module to use for finders
18 19 20 |
# File 'lib/friendly_id/configuration.rb', line 18 def finder_methods @finder_methods end |
#model_class ⇒ Object
The model class that this configuration belongs to.
15 16 17 |
# File 'lib/friendly_id/configuration.rb', line 15 def model_class @model_class end |
#modules ⇒ Object (readonly)
The modules in use
11 12 13 |
# File 'lib/friendly_id/configuration.rb', line 11 def modules @modules end |
#routes ⇒ Object
Route generation preferences
24 25 26 |
# File 'lib/friendly_id/configuration.rb', line 24 def routes @routes end |
Instance Method Details
#get_module(object) ⇒ Object (private)
103 104 105 |
# File 'lib/friendly_id/configuration.rb', line 103 def get_module(object) Module === object ? object : FriendlyId.const_get(object.to_s.titleize.camelize.gsub(/\s+/, "")) end |
#query_field ⇒ Object
The column that FriendlyId will use to find the record when querying by friendly id.
This method is generally only used internally by FriendlyId.
70 71 72 |
# File 'lib/friendly_id/configuration.rb', line 70 def query_field base.to_s end |
#set(values) ⇒ Object (private)
107 108 109 |
# File 'lib/friendly_id/configuration.rb', line 107 def set(values) values&.each { |name, value| send "#{name}=", value } end |
#use(*modules) ⇒ Object
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.
52 53 54 55 56 57 58 |
# File 'lib/friendly_id/configuration.rb', line 52 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 |
#uses?(mod) ⇒ Boolean
Returns whether the given module is in use.
61 62 63 |
# File 'lib/friendly_id/configuration.rb', line 61 def uses?(mod) @model_class < get_module(mod) end |