Skip to content

Commit

Permalink
refactor array iterations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikekosulin committed Oct 22, 2023
1 parent b283822 commit 6ede5de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 30 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
29 changes: 9 additions & 20 deletions lib/mongoid/slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,11 @@ 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])
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
end

Expand All @@ -128,16 +122,11 @@ def look_like_slugs?(*args)
#
# @return [ Array<Document>, 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.
Expand Down
8 changes: 2 additions & 6 deletions lib/mongoid/slug/unique_slug.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6ede5de

Please sign in to comment.