From 153889c17d989f539394c8e460ea53945bc75f95 Mon Sep 17 00:00:00 2001 From: Johnny Shields <27655+johnnyshields@users.noreply.github.com> Date: Mon, 18 Sep 2023 04:13:45 +0900 Subject: [PATCH] Add option for index: false (#271) --- lib/mongoid/slug.rb | 4 +++- spec/models/no_index.rb | 9 +++++++++ spec/mongoid/slug_spec.rb | 5 +++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 spec/models/no_index.rb diff --git a/lib/mongoid/slug.rb b/lib/mongoid/slug.rb index 1be4199..e7b6d6c 100644 --- a/lib/mongoid/slug.rb +++ b/lib/mongoid/slug.rb @@ -18,6 +18,7 @@ module Slug included do cattr_accessor :slug_reserved_words, :slug_scope, + :slug_index, :slugged_attributes, :slug_url_builder, :slug_history, @@ -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] @@ -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 diff --git a/spec/models/no_index.rb b/spec/models/no_index.rb new file mode 100644 index 0000000..b734c38 --- /dev/null +++ b/spec/models/no_index.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class NoIndex + include Mongoid::Document + include Mongoid::Slug + field :title + + slug :title, index: false +end diff --git a/spec/mongoid/slug_spec.rb b/spec/mongoid/slug_spec.rb index 2fbcaa5..46c46f0 100644 --- a/spec/mongoid/slug_spec.rb +++ b/spec/mongoid/slug_spec.rb @@ -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