Skip to content

Commit

Permalink
Add option for index: false (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyshields authored Sep 17, 2023
1 parent 5891734 commit 153889c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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

0 comments on commit 153889c

Please sign in to comment.