Module: Mongoid::Slug::ClassMethods

Defined in:
lib/mongoid/slug.rb

Instance Method Summary collapse

Instance Method Details

#find_by_slug!(*args) ⇒ Array<Document>, Document

Find documents by slugs.

A document matches if any of its slugs match one of the supplied params.

A document matching multiple supplied params will be returned only once.

If any supplied param does not match a document a Mongoid::Errors::DocumentNotFound will be raised.

Examples:

Find by a slug.

Model.find_by_slug!('some-slug')

Find by multiple slugs.

Model.find_by_slug!('some-slug', 'some-other-slug')

Parameters:

  • args (Array<Object>)

    The slugs to search for.

Returns:

  • (Array<Document>, Document)

    The matching document(s).



140
141
142
# File 'lib/mongoid/slug.rb', line 140

def find_by_slug!(*args)
  with_default_scope.find_by_slug!(*args)
end

#is_paranoid_doc?Array<Document>, Document

Indicates whether or not the document includes Mongoid::Paranoia

This can be replaced with .paranoid? method once the following PRs are merged:

Returns:

  • (Array<Document>, Document)

    Whether the document is paranoid



155
156
157
# File 'lib/mongoid/slug.rb', line 155

def is_paranoid_doc?
  !!(defined?(::Mongoid::Paranoia) && self < ::Mongoid::Paranoia)
end

#look_like_slugs?(*args) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/mongoid/slug.rb', line 111

def look_like_slugs?(*args)
  with_default_scope.look_like_slugs?(*args)
end

#queryableObject



144
145
146
# File 'lib/mongoid/slug.rb', line 144

def queryable
  current_scope || Criteria.new(self) # Use Mongoid::Slug::Criteria for slugged documents.
end

#slug(*fields) { ... } ⇒ Object #slug(*fields, options) { ... } ⇒ Object

Examples:

A custom builder

class Person
  include Mongoid::Document
  include Mongoid::Slug

  field :names, :type => Array
  slug :names do |doc|
    doc.names.join(' ')
  end
end

Overloads:

  • #slug(*fields) { ... } ⇒ Object

    Sets one ore more fields as source of slug.

    Parameters:

    • fields (Array)

      One or more fields the slug should be based on.

    Yields:

    • If given, the block is used to build a custom slug.

  • #slug(*fields, options) { ... } ⇒ Object

    Sets one ore more fields as source of slug. the slug should be retained. When searched by slug, the document now matches both past and present slugs. immutable. Defaults to ‘false`. scope the slug by. Embedded documents are, by default, scoped by their parent.

    Parameters:

    • fields (Array)

      One or more fields the slug should be based on.

    • options (Hash)
    • options (Boolean)

      :history Whether a history of changes to

    • options (Boolean)

      :permanent Whether the slug should be

    • options (Array)

      :reserve` A list of reserved slugs

    • options

      :scope [Symbol] a reference association or field to

    • options

      :max_length [Integer] the maximum length of the text portion of the slug

    Yields:

    • If given, a block is used to build a slug.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mongoid/slug.rb', line 64

def slug(*fields, &block)
  options = fields.extract_options!

  self.slug_scope            = options[:scope]
  self.reserved_words        = options[:reserve] || Set.new(%w(new edit))
  self.slugged_attributes    = fields.map(&:to_s)
  self.history               = options[:history]
  self.by_model_type         = options[:by_model_type]
  self.slug_max_length       = options.key?(:max_length) ? options[:max_length] : MONGO_INDEX_KEY_LIMIT_BYTES - 32

  field :_slugs, type: Array, default: [], localize: options[:localize]
  alias_attribute :slugs, :_slugs

  # Set index
  unless embedded?
    index(*Mongoid::Slug::Index.build_index(slug_scope_key, by_model_type))
  end

  #-- Why is it necessary to customize the slug builder?
  default_url_builder = lambda do |cur_object|
    cur_object.slug_builder.to_url
  end

  self.url_builder = block_given? ? block : default_url_builder

  #-- always create slug on create
  #-- do not create new slug on update if the slug is permanent
  if options[:permanent]
    set_callback :create, :before, :build_slug
  else
    set_callback :save, :before, :build_slug, if: :slug_should_be_rebuilt?
  end

  # If paranoid document:
  # - include shim to add callbacks for restore method
  # - unset the slugs on destroy
  # - recreate the slug on restore
  # - force reset the slug when saving a destroyed paranoid document, to ensure it stays unset in the database
  if is_paranoid_doc?
    send(:include, Mongoid::Slug::Paranoia) unless self.respond_to?(:before_restore)
    set_callback :destroy, :after,  :unset_slug!
    set_callback :restore, :before, :set_slug!
    set_callback :save,    :before, :reset_slug!, if: :paranoid_deleted?
    set_callback :save,    :after,  :clear_slug!, if: :paranoid_deleted?
  end
end

#slug_scope_keyArray<Document>, Document

Returns the scope key for indexing, considering associations

Returns:

  • (Array<Document>, Document)

    Whether the document is paranoid



118
119
120
121
# File 'lib/mongoid/slug.rb', line 118

def slug_scope_key
  return nil unless slug_scope
  reflect_on_association(slug_scope).try(:key) || slug_scope
end