diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bb83ee..b080ef5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * [#277](https://github.com/mongoid/mongoid-slug/pull/277): Migrate Danger to use danger-pr-comment workflow - [@dblock](https://github.com/dblock). * [#274](https://github.com/mongoid/mongoid-slug/pull/274): Added support for scoping slugs by multiple fields - [@mikekosulin](https://github.com/mikekosulin). * [#279](https://github.com/mongoid/mongoid-slug/pull/279): Replace deprecated `__find_args__` with `Array#flatten` - [@samiFakhfakhScalingo](https://github.com/samiFakhfakhScalingo), [@aurelien-reeves-scalingo](https://github.com/aurelien-reeves-scalingo). +* [#280](https://github.com/mongoid/mongoid-slug/pull/280): Remove deprecated method multi_arged? - [@johnnyshields](https://github.com/johnnyshields), [@aurelien-reeves-scalingo](https://github.com/aurelien-reeves-scalingo). * Your contribution here. ## 7.0.0 (2023/09/18) diff --git a/lib/mongoid/slug/criteria.rb b/lib/mongoid/slug/criteria.rb index 656439b..80ca7fd 100644 --- a/lib/mongoid/slug/criteria.rb +++ b/lib/mongoid/slug/criteria.rb @@ -26,11 +26,13 @@ class Criteria < Mongoid::Criteria # @example Find by multiple slugs. # criteria.find([ 'some-slug', 'some-other-slug' ]) # - # @param [ Array ] args The ids or slugs to search for. + # @param [ Array ] slugs The ids or slugs to search for. # # @return [ Array, Document ] The matching document(s). - def find(*args) - look_like_slugs?(args.flatten) ? find_by_slug!(*args) : super + def find(*slugs) + return find_by_slug!(*slugs) if look_like_slugs?(*slugs) + + super end # Find the matchind document(s) in the criteria for the provided slugs. @@ -41,21 +43,22 @@ def find(*args) # @example Find by multiple slugs. # criteria.find([ 'some-slug', 'some-other-slug' ]) # - # @param [ Array ] args The slugs to search for. + # @param [ Array... ] slugs The slugs to search for. # # @return [ Array, Document ] The matching document(s). - def find_by_slug!(*args) - slugs = args.flatten + def find_by_slug!(*slugs) + slugs = find_args(slugs) raise_invalid if slugs.any?(&:nil?) - for_slugs(slugs).execute_or_raise_for_slugs(slugs, args.multi_arged?) + for_slugs(slugs).execute_or_raise_for_slugs(slugs) end - def look_like_slugs?(args) - return false unless args.all?(String) + def look_like_slugs?(*slugs) + slugs = find_args(slugs) + return false unless slugs.all?(String) id_field = @klass.fields['_id'] @slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type) - args.none? { |id| @slug_strategy.call(id) } + slugs.none? { |slug| @slug_strategy.call(slug) } end protected @@ -94,10 +97,16 @@ def for_slugs(slugs) end end - def execute_or_raise_for_slugs(slugs, multi) + def find_args(args) + args = args.flatten + args.uniq!(&:to_s) + args + end + + def execute_or_raise_for_slugs(slugs) result = uniq check_for_missing_documents_for_slugs!(result, slugs) - multi ? result : result.first + slugs.size == 1 ? result.first : result end def check_for_missing_documents_for_slugs!(result, slugs) diff --git a/lib/mongoid/slug/unique_slug.rb b/lib/mongoid/slug/unique_slug.rb index 3132129..f79bd8f 100644 --- a/lib/mongoid/slug/unique_slug.rb +++ b/lib/mongoid/slug/unique_slug.rb @@ -44,9 +44,8 @@ def highest_existing_counter def sort_existing_slugs # remove the slug part and leave the absolute integer part and sort - re = /^#{Regexp.escape(@slug)}/ @sorted_existing = existing_slugs.map do |s| - s.sub(re, '').to_i.abs + s.delete_prefix(@slug).to_i.abs end.sort end