Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Slug
included do
cattr_accessor :slug_reserved_words,
:slug_scope,
:slug_index,
:slugged_attributes,
:slug_url_builder,
:slug_history,
Expand Down Expand Up @@ -77,6 +78,7 @@ def slug(*fields, &block)
options = fields.extract_options!

self.slug_scope = options[:scope]
self.slug_index = options[:index].nil? ? true : options[:index]
self.slug_reserved_words = options[:reserve] || Set.new(%w[new edit])
self.slugged_attributes = fields.map(&:to_s)
self.slug_history = options[:history]
Expand All @@ -87,7 +89,7 @@ def slug(*fields, &block)
alias_attribute :slugs, :_slugs

# Set indexes
unless embedded?
if slug_index && !embedded?
Mongoid::Slug::IndexBuilder.build_indexes(self, slug_scope_key, slug_by_model_type,
options[:localize])
end
Expand Down
9 changes: 9 additions & 0 deletions spec/models/no_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class NoIndex
include Mongoid::Document
include Mongoid::Slug
field :title

slug :title, index: false
end
5 changes: 5 additions & 0 deletions spec/mongoid/slug_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ class Person
it_should_behave_like 'has an index', { _slugs: 1 }, unique: true, sparse: true
end

context 'when slug index is skipped' do
subject { NoIndex }
it_should_behave_like 'does not have an index', _slugs: 1
end

context 'when slug is scoped by a reference association' do
subject { Author }
it_should_behave_like 'does not have an index', _slugs: 1
Expand Down