Skip to content

Commit d06cd1c

Browse files
Remove deprecated method multi_arged? (#280)
* Remove deprecated method multi_arged? Co-authored-by: johnnyshields <27655+johnnyshields@users.noreply.github.com> * Update CHANGELOG --------- Co-authored-by: johnnyshields <27655+johnnyshields@users.noreply.github.com>
1 parent eb79653 commit d06cd1c

3 files changed

Lines changed: 23 additions & 14 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* [#277](https://github.com/mongoid/mongoid-slug/pull/277): Migrate Danger to use danger-pr-comment workflow - [@dblock](https://github.com/dblock).
44
* [#274](https://github.com/mongoid/mongoid-slug/pull/274): Added support for scoping slugs by multiple fields - [@mikekosulin](https://github.com/mikekosulin).
55
* [#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).
6+
* [#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).
67
* Your contribution here.
78

89
## 7.0.0 (2023/09/18)

lib/mongoid/slug/criteria.rb

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ class Criteria < Mongoid::Criteria
2626
# @example Find by multiple slugs.
2727
# criteria.find([ 'some-slug', 'some-other-slug' ])
2828
#
29-
# @param [ Array<Object> ] args The ids or slugs to search for.
29+
# @param [ Array<Object> ] slugs The ids or slugs to search for.
3030
#
3131
# @return [ Array<Document>, Document ] The matching document(s).
32-
def find(*args)
33-
look_like_slugs?(args.flatten) ? find_by_slug!(*args) : super
32+
def find(*slugs)
33+
return find_by_slug!(*slugs) if look_like_slugs?(*slugs)
34+
35+
super
3436
end
3537

3638
# Find the matchind document(s) in the criteria for the provided slugs.
@@ -41,21 +43,22 @@ def find(*args)
4143
# @example Find by multiple slugs.
4244
# criteria.find([ 'some-slug', 'some-other-slug' ])
4345
#
44-
# @param [ Array<Object> ] args The slugs to search for.
46+
# @param [ Array<Object>... ] slugs The slugs to search for.
4547
#
4648
# @return [ Array<Document>, Document ] The matching document(s).
47-
def find_by_slug!(*args)
48-
slugs = args.flatten
49+
def find_by_slug!(*slugs)
50+
slugs = find_args(slugs)
4951
raise_invalid if slugs.any?(&:nil?)
50-
for_slugs(slugs).execute_or_raise_for_slugs(slugs, args.multi_arged?)
52+
for_slugs(slugs).execute_or_raise_for_slugs(slugs)
5153
end
5254

53-
def look_like_slugs?(args)
54-
return false unless args.all?(String)
55+
def look_like_slugs?(*slugs)
56+
slugs = find_args(slugs)
57+
return false unless slugs.all?(String)
5558

5659
id_field = @klass.fields['_id']
5760
@slug_strategy ||= id_field.options[:slug_id_strategy] || build_slug_strategy(id_field.type)
58-
args.none? { |id| @slug_strategy.call(id) }
61+
slugs.none? { |slug| @slug_strategy.call(slug) }
5962
end
6063

6164
protected
@@ -94,10 +97,16 @@ def for_slugs(slugs)
9497
end
9598
end
9699

97-
def execute_or_raise_for_slugs(slugs, multi)
100+
def find_args(args)
101+
args = args.flatten
102+
args.uniq!(&:to_s)
103+
args
104+
end
105+
106+
def execute_or_raise_for_slugs(slugs)
98107
result = uniq
99108
check_for_missing_documents_for_slugs!(result, slugs)
100-
multi ? result : result.first
109+
slugs.size == 1 ? result.first : result
101110
end
102111

103112
def check_for_missing_documents_for_slugs!(result, slugs)

lib/mongoid/slug/unique_slug.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,8 @@ def highest_existing_counter
4444

4545
def sort_existing_slugs
4646
# remove the slug part and leave the absolute integer part and sort
47-
re = /^#{Regexp.escape(@slug)}/
4847
@sorted_existing = existing_slugs.map do |s|
49-
s.sub(re, '').to_i.abs
48+
s.delete_prefix(@slug).to_i.abs
5049
end.sort
5150
end
5251

0 commit comments

Comments
 (0)