From a64402c1db663bf867f3eb414eb623c8c6a6f591 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:02:17 +0900 Subject: [PATCH 01/10] Remove deprecated methods __find_args__ and multi_arged? --- lib/mongoid/slug/criteria.rb | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/mongoid/slug/criteria.rb b/lib/mongoid/slug/criteria.rb index 09b7de4..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.__find_args__) ? 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.__find_args__ + 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? { |id| id.is_a?(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) From 92674fa1c6f2b08a42cfd1749dfc72874610c3c1 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:08:55 +0900 Subject: [PATCH 02/10] Fix rubocop and update test matrix --- .github/workflows/test.yml | 25 ++++++++++++++++--------- lib/mongoid/slug.rb | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d6dc685..b67495e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,6 +3,20 @@ name: CI RSpec Test on: [push, pull_request] jobs: + rubocop: + runs-on: ubuntu-latest + env: + CI: true + steps: + - uses: actions/checkout@v3 + - name: Set up Ruby 3.4 + uses: ruby/setup-ruby@v1 + with: + ruby-version: 3.4 + bundler-cache: true + - name: Run RuboCop + run: bundle exec rubocop --parallel + build: name: >- ${{ matrix.ruby }} @@ -14,8 +28,8 @@ jobs: strategy: fail-fast: false matrix: - ruby: [2.7, 3.0, 3.1, 3.2, truffleruby] - mongoid: [7, 8] + ruby: [2.7, 3.0, 3.1, 3.2, 3.3, 3.4, truffleruby] + mongoid: [7, 8, 9] experimental: [false] include: - ruby: 3.2 @@ -58,13 +72,6 @@ jobs: env: MONGOID_VERSION: ${{ matrix.mongoid }} - - name: rubocop - timeout-minutes: 5 - run: bundle exec rubocop - continue-on-error: ${{ matrix.experimental }} - env: - MONGOID_VERSION: ${{ matrix.mongoid }} - - name: test timeout-minutes: 10 run: bundle exec rake spec diff --git a/lib/mongoid/slug.rb b/lib/mongoid/slug.rb index 1f34b49..81234d3 100644 --- a/lib/mongoid/slug.rb +++ b/lib/mongoid/slug.rb @@ -79,7 +79,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_index = options[:index].nil? || 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] From 115c37918875372487897b9df3bbb803e9943c0b Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:11:27 +0900 Subject: [PATCH 03/10] Add Mongoid 8.1 to matrix --- .github/workflows/test.yml | 6 +++--- Gemfile | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b67495e..1dd3ab6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,17 +29,17 @@ jobs: fail-fast: false matrix: ruby: [2.7, 3.0, 3.1, 3.2, 3.3, 3.4, truffleruby] - mongoid: [7, 8, 9] + mongoid: [7, 8, 8.1, 9] experimental: [false] include: - ruby: 3.2 - mongoid: HEAD + mongoid: head experimental: true - ruby: head mongoid: 7 experimental: true - ruby: head - mongoid: HEAD + mongoid: head experimental: true - ruby: jruby mongoid: 8 diff --git a/Gemfile b/Gemfile index 17ccaec..6237bac 100644 --- a/Gemfile +++ b/Gemfile @@ -4,8 +4,8 @@ source 'https://rubygems.org' gemspec name: 'mongoid-slug' -case (version = ENV['MONGOID_VERSION'] || '8') -when 'HEAD' +case (version = ENV['MONGOID_VERSION'] || '8').downcase +when 'head' gem 'mongoid', github: 'mongodb/mongoid' when /\A\d+\z/ gem 'mongoid', "~> #{version}.0" From 4d4563db21ce4df68e1447a2cd4f067df589ce3d Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:14:48 +0900 Subject: [PATCH 04/10] Cleanup CI --- .github/workflows/test.yml | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1dd3ab6..a3fc4b3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -19,7 +19,7 @@ jobs: build: name: >- - ${{ matrix.ruby }} + "${{ matrix.ruby }} mongoid-${{ matrix.mongoid }}" env: CI: true TESTOPTS: -v @@ -28,17 +28,28 @@ jobs: strategy: fail-fast: false matrix: - ruby: [2.7, 3.0, 3.1, 3.2, 3.3, 3.4, truffleruby] - mongoid: [7, 8, 8.1, 9] + ruby: + - ruby-2.7 + - ruby-3.0 + - ruby-3.1 + - ruby-3.2 + - ruby-3.3 + - ruby-3.4 + - truffleruby + mongoid: + - 7.0 + - 8.0 + - 8.1 + - 9.0 experimental: [false] include: - - ruby: 3.2 + - ruby: ruby-3.2 mongoid: head experimental: true - - ruby: head + - ruby: ruby-head mongoid: 7 experimental: true - - ruby: head + - ruby: ruby-head mongoid: head experimental: true - ruby: jruby From e29aa8e085b1213260a3991aac8273335b8bcb8f Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:15:26 +0900 Subject: [PATCH 05/10] Fix CI name --- .github/workflows/test.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a3fc4b3..20c53e7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,8 +18,7 @@ jobs: run: bundle exec rubocop --parallel build: - name: >- - "${{ matrix.ruby }} mongoid-${{ matrix.mongoid }}" + name: "${{ matrix.ruby }} mongoid-${{ matrix.mongoid }}" env: CI: true TESTOPTS: -v From 8c0fd5ce06fc0355c9a209e8f327bd64618bb15c Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:19:31 +0900 Subject: [PATCH 06/10] Fix mongoid versions --- .github/workflows/test.yml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 20c53e7..cb8bf9c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,10 +36,9 @@ jobs: - ruby-3.4 - truffleruby mongoid: - - 7.0 - - 8.0 - - 8.1 - - 9.0 + - '8.0' + - '8.1' + - '9.0' experimental: [false] include: - ruby: ruby-3.2 From db6229361e4eed27b795518943c350e9a08dfc22 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:21:51 +0900 Subject: [PATCH 07/10] Allow major.minor version --- Gemfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile b/Gemfile index 6237bac..93b4e50 100644 --- a/Gemfile +++ b/Gemfile @@ -7,7 +7,7 @@ gemspec name: 'mongoid-slug' case (version = ENV['MONGOID_VERSION'] || '8').downcase when 'head' gem 'mongoid', github: 'mongodb/mongoid' -when /\A\d+\z/ +when /\A\d+(?:\.\d+)?\z/ gem 'mongoid', "~> #{version}.0" else gem 'mongoid', version From f340439c18a0000cfc2b53d0f89791f7b5610cff Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:28:39 +0900 Subject: [PATCH 08/10] Install latest Mongoid locally --- Gemfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 93b4e50..c4faa6b 100644 --- a/Gemfile +++ b/Gemfile @@ -4,12 +4,12 @@ source 'https://rubygems.org' gemspec name: 'mongoid-slug' -case (version = ENV['MONGOID_VERSION'] || '8').downcase +case (version = ENV['MONGOID_VERSION'])&.downcase when 'head' gem 'mongoid', github: 'mongodb/mongoid' when /\A\d+(?:\.\d+)?\z/ gem 'mongoid', "~> #{version}.0" -else +when String gem 'mongoid', version end From 46846e8453bcc9e65b9f59904e83b114ff9b41e0 Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:36:15 +0900 Subject: [PATCH 09/10] Cleanup code --- lib/mongoid/slug/unique_slug.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) 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 From dcf51833e9fa0b6bc288b535c5edff9fa3408f7a Mon Sep 17 00:00:00 2001 From: johnnyshields <27655+johnnyshields@users.noreply.github.com> Date: Wed, 7 May 2025 05:38:02 +0900 Subject: [PATCH 10/10] Fix rubocop --- .rubocop.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.rubocop.yml b/.rubocop.yml index d9984b6..411beaf 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -29,6 +29,9 @@ Style/Documentation: Style/DoubleNegation: Enabled: false +Style/FetchEnvVar: + Enabled: false + Style/ModuleFunction: EnforcedStyle: extend_self