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.

Examples:

Setting up the slug:

class SluggableDocument
  include Mongoid::Document
  include Mongoid::SleepingKingStudios::Sluggable

  field :title, :type => String

  slugify :title
end # class

See Also:

Since:

  • 0.1.0

Defined Under Namespace

Modules: ClassMethods Classes: Metadata

Instance Attribute Summary collapse

Class Method Summary collapse

Methods included from Concern

characterize, relate, valid_options, validate_options

Instance Attribute Details

#slugString (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.

Returns:

  • (String)

    The value of the stored slug.



# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 131

#slug_lockBoolean

(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.

Returns:

  • (Boolean)

    True if the slug is locked; otherwise false.



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

Parameters:

  • base (Class)

    The base class into which the concern is mixed in.

  • attribute (String, Symbol)

    The base field used to determine the value of the slug. When this field is changed via its writer method, the slug will be updated.

  • options (Hash)

    The options for the relation.

Since:

  • 0.6.0



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, options
  name = :sluggable
  validate_options    name, options
  meta = characterize name, options, Metadata
  meta[:attribute] = attribute

  relate base, name, meta

  define_fields      base, meta
  define_accessors   base, meta
  define_validations base, meta
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.

Parameters:

  • base (Class)

    The base class into which the concern is mixed in.

  • metadata (Metadata)

    The metadata for the relation.

Since:

  • 0.6.0



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.

Parameters:

  • base (Class)

    The base class into which the concern is mixed in.

  • metadata (Metadata)

    The metadata for the relation.

Since:

  • 0.6.0



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 “-”).

Parameters:

  • base (Class)

    The base class into which the concern is mixed in.

  • metadata (Metadata)

    The metadata for the relation.

Since:

  • 0.6.0



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_optionsArray<Symbol>

Returns a list of options that are valid for this concern.

Returns:

  • (Array<Symbol>)

    The list of valid options.

Since:

  • 0.6.0



125
126
127
128
129
# File 'lib/mongoid/sleeping_king_studios/sluggable.rb', line 125

def self.valid_options
  super + %i(
    lockable
  ) # end array
end