From 47ff69e26f14578debcba1078a6c1dd7b8019619 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 22 Oct 2023 18:07:21 +0000 Subject: [PATCH] refactor array iterations --- CHANGELOG.md | 7 +++---- lib/mongoid/slug.rb | 31 +++++++++++-------------------- lib/mongoid/slug/unique_slug.rb | 8 ++------ 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 237a50a..3afffd0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,7 @@ -## 7.1.1 (Next) +## 7.1.0 (Next) -## 7.1.0 (2023/10/20) - -* [#274](https://github.com/mongoid/mongoid-slug/pull/274): Added support for scoping slugs by multiple fields +* [#274](https://github.com/mongoid/mongoid-slug/pull/274): Added support for scoping slugs by multiple fields - [@mikekosulin](https://github.com/mikekosulin) +* Your contribution here. ## 7.0.0 (2023/09/18) diff --git a/lib/mongoid/slug.rb b/lib/mongoid/slug.rb index f74f57f..cdf23b5 100644 --- a/lib/mongoid/slug.rb +++ b/lib/mongoid/slug.rb @@ -91,17 +91,13 @@ def slug(*fields, &block) # Set indexes if slug_index && !embedded? - # Check if scope is an array and handle accordingly. - if slug_scope.is_a?(Array) - slug_scope.each do |individual_scope| - # Here, build indexes for each scope in the array. - # This assumes `Mongoid::Slug::IndexBuilder.build_indexes` can handle individual scope items. - # If not, `build_indexes` may need modification to support this. - Mongoid::Slug::IndexBuilder.build_indexes(self, individual_scope, slug_by_model_type, options[:localize]) - end - else - # For a single scope, it remains unchanged. - Mongoid::Slug::IndexBuilder.build_indexes(self, slug_scope_key, slug_by_model_type, options[:localize]) + # Even if the slug_scope is nil, we need to proceed. + slug_scopes = slug_scope.nil? ? [slug_scope_key] : Array(slug_scope) + + # Here, build indexes for each scope in the array. + slug_scopes.each do |individual_scope| + Mongoid::Slug::IndexBuilder.build_indexes(self, individual_scope, slug_by_model_type, + options[:localize]) end end @@ -128,16 +124,11 @@ def look_like_slugs?(*args) # # @return [ Array, Document ] def slug_scope_key - return nil unless slug_scope - - # If slug_scope is an array, we return an array of keys. - if slug_scope.is_a?(Array) - slug_scope.map do |individual_scope| - reflect_on_association(individual_scope).try(:key) || individual_scope - end - else - reflect_on_association(slug_scope).try(:key) || slug_scope + keys = Array(slug_scope).map do |individual_scope| + reflect_on_association(individual_scope).try(:key) || individual_scope end + + keys.empty? ? nil : keys end # Find documents by slugs. diff --git a/lib/mongoid/slug/unique_slug.rb b/lib/mongoid/slug/unique_slug.rb index 55710a4..b903a1f 100644 --- a/lib/mongoid/slug/unique_slug.rb +++ b/lib/mongoid/slug/unique_slug.rb @@ -101,8 +101,7 @@ def find_unique(attempt = nil) where_hash[:_id.ne] = model._id if (scope = slug_scope) - scopes = scope.is_a?(Array) ? scope : [scope] # Wrap single scope into an array for uniform handling - scopes.each do |individual_scope| + Array(scope).each do |individual_scope| next unless reflect_on_association(individual_scope).nil? # scope is not an association, so it's scoped to a local field @@ -150,13 +149,10 @@ def regex_for_slug def uniqueness_scope # If slug_scope is present, we need to handle whether it's a single scope or multiple scopes. if slug_scope - # Convert the scope to an array if it's a single symbol for uniform processing. - scopes = slug_scope.is_a?(Array) ? slug_scope : [slug_scope] - # We'll track individual scope results in an array. scope_results = [] - scopes.each do |individual_scope| + Array(slug_scope).each do |individual_scope| next unless (metadata = reflect_on_association(individual_scope)) # For each scope, we identify its association metadata and fetch the parent record.