Module: Mongoid::SleepingKingStudios::Sluggable
- Extended by:
- ActiveSupport::Concern, Concern
- Defined in:
- lib/mongoid/sleeping_king_studios/sluggable.rb,
lib/mongoid/sleeping_king_studios/sluggable/metadata.rb
Overview
Adds a :slug field that stores a short, url-friendly reference string, useful for human-readable urls. By default, the slug field is automatically overwritten from the specified base attribute before validation. To enable setting the slug manually, use the :lockable option; otherwise, the :slug= writer is set to private.
Defined Under Namespace
Modules: ClassMethods Classes: Metadata
Instance Attribute Summary collapse
-
#slug ⇒ String
readonly
A url-friendly short string version of the specified base attribute.
-
#slug_lock ⇒ Boolean
(Lockable) A flag that indicates whether or not the slug is locked.
Class Method Summary collapse
-
.apply(base, attribute, options) ⇒ Object
private
Sets up the sluggable relation, creating fields, accessors and validations.
-
.define_accessors(base, metadata) ⇒ Object
private
Redefines the writer for the base attribute to overwrite the value of the slug field unless the slug is locked.
-
.define_fields(base, metadata) ⇒ Object
private
Creates a slug field of type String on the base class.
-
.define_validations(base, metadata) ⇒ Object
private
Sets a validation on the slug field that validates the presence of the slug, and that the value is of a valid format (lower-case characters a-z, digits 0-9, and hyphens “-”).
-
.valid_options ⇒ Array<Symbol>
Returns a list of options that are valid for this concern.
Methods included from Concern
characterize, relate, valid_options, validate_options
Instance Attribute Details
#slug ⇒ String (readonly)
A url-friendly short string version of the specified base attribute.
(Lockable) The slug value can be set directly using the #slug= method. This will set the :slug_lock flag, preventing the slug from being updated by a change to the base field until :slug_lock is cleared.
|
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 131
|
#slug_lock ⇒ Boolean
(Lockable) A flag that indicates whether or not the slug is locked. If the flag is set, updating the base field will not change the value of the slug.
|
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 140
|
Class Method Details
.apply(base, attribute, options) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets up the sluggable relation, creating fields, accessors and validations.
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 42 def self.apply base, attribute, name = :sluggable name, = characterize name, , Metadata [:attribute] = attribute relate base, name, define_fields base, define_accessors base, define_validations base, end |
.define_accessors(base, metadata) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Redefines the writer for the base attribute to overwrite the value of the slug field unless the slug is locked. If the Lockable option is selected, redefines the writer for the slug field to lock the slug when set manually; otherwise, makes the writer for the slug field private.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 66 def self.define_accessors base, base.re_define_method :"#{.attribute}=" do |value| self[.attribute.to_s] = value unless .lockable? && self['slug_lock'] self['slug'] = .value_to_slug value end # unless end # method if .lockable? base.re_define_method :slug= do |value| self['slug'] = value self['slug_lock'] = true end # method else base.send :private, :slug= end # if end |
.define_fields(base, metadata) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates a slug field of type String on the base class. If the Lockable option is selected, also creates a slug_lock field of type Boolean.
93 94 95 96 97 98 99 |
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 93 def self.define_fields base, base.send :field, :slug, :type => String if .lockable? base.send :field, :slug_lock, :type => Boolean, :default => false end # if end |
.define_validations(base, metadata) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Sets a validation on the slug field that validates the presence of the slug, and that the value is of a valid format (lower-case characters a-z, digits 0-9, and hyphens “-”).
111 112 113 114 115 116 117 118 |
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 111 def self.define_validations base, base.validates :slug, :presence => true, :format => { :with => /\A[a-z0-9\-]+\z/, :message => 'must be lower-case characters a-z, digits 0-9, and hyphens "-"' } # end format end |
.valid_options ⇒ Array<Symbol>
Returns a list of options that are valid for this concern.
125 126 127 128 129 |
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 125 def self. super + %i( lockable ) # end array end |